NXT Files

INTRODUCTION

The following files allow a user to direct a 2-motor NXT car to drive forward or backwards at a quantitatively accurate constant acceleration. The units I’ve used are cm and seconds, however the program is very flexible and could be easily converted to other units. The main algorithm used is called a PID algorithm which creates a correction factor for each motor independently based on how far off the motor is from its target velocity (the error) 20 times each second the car is moving. Warning: all I know about PID controllers I learned this summer from the interwebs so I’m sure there’s a lot that could be fixed!

GETTING STARTED

Build a 2-motor car where the right wheel is controlled by a motor plugged into the B port, and the left wheel is controlled by a motor plugged into the C port. The car I am using is this 2-motor car. Click here for a PDF version of the building instructions. WARNING: the PDF is a LARGE file.

There are many important files that need to be installed as myblocks. Click here to download them in the file “NXTkinematicsFiles.rbtx”. Click “Download”. Find the file “NXTkinematicsFiles.rbtx” wherever your internet downloads go on your computer and open it. The Mindstorms software will open up, and you will see the “code” for “NXTkinematicsFiles.rbt”. This is a dummy file meant to contain each myblock you need. You can safely close it.

The first myblock you need to run on every NXT car you plan on using is called "GenerateTextFiles". This file creates text files on the NXT which store important set up variables such as wheel diameter, and lowest power setting for zero speed.

If you are using different wheels than the ones I am, you will need to run "CalibrateCar" next. The first part of the calibration finds your car's effective wheel diameter. It is VITAL that the effective wheel diameter be determined as accurately as possible. This affects the accuracy of PID motion the most by far. You can run this multiple times to ensure that you indeed have the most accurate effective wheel diameter possible. Follow the instructions on the screen.

Once the program has determined the car's effective wheel diameter, it will calculate important values used to help the "MoveCar" myblock determine an appropriate initial power level for the motors based on the initial velocity given to it. The car will try to start moving, move at 100% and then move at a motor speed of 75. In total, it only moves around half a meter. Ideally the motors shouldn't be that different from each other so if you see that the values for each motor are very different, try running this part of the calibration again. If the difference persists, it may very well be that one of your motors is much stronger than the other.

The car also displays its top speed on the NXT screen. This is an important value to be aware of. When setting the parameters of the "MoveCar" and "CalibrateCar" myblocks, care must be taken to not top out the car's speed any time during a run. This throws kinematics out the window.

USEFUL PROGRAMS

"MoveCar" is a myblock which you can use to tell the car to move at a specific initial velocity for a specific time at a specific acceleration. It can simply be dragged out onto a program, or even cooler, you can drag many of them out and set them to run differently which lets the car move in several distinct motions in a row. Connect a data wire from the final velocity of one MoveCar myblock to the next MoveCar myblock's initial velocity input to smoothly have the car go from one motion to the next. WARNING: You must use a move block which stops motors B and C in any program you write containing "MoveCar" because I didn't build a stopping feature into "MoveCar". Otherwise the car will keep moving after the programmed time is up.

MoveCarT.rbt is a file you can preload onto an NXT which allows a student to input directly on the NXT a time for the car to run. You can set a hidden velocity or even acceleration by opening the file and modifying the MoveCar myblock's settings at the end of the program. The BEST feature of this is that students don't need to use computers at all to use the NXT's.

MoveCarTV.rbt is a file you can preload onto an NXT which allows a student to input directly on the NXT a time and a velocity. You can set a hidden acceleration by opening the file and modifying the MoveCar myblock's settings at the end of the program.

MoveCarTVA.rbt is a file you can preload onto an NXT which allows a student to input directly on the NXT a time, a velocity and an acceleration. This is great for practicing and checking kinematic equations practice problems.

MoveCarTVAR.rbt is a file you can preload onto an NXT which allows a student to input directly on the NXT a time, an initial linear velocity, a linear acceleration, and a turn radius. This is great for practicing and checking angular kinematic equations practice problems.

MoveCarTAVAAR.rbt is a file you can preload onto an NXT which allows a student to input directly on the NXT a time, an initial angular velocity (in radians per second), an angular acceleration (rad/s/s), and a turn radius. This is great for practicing and checking angular kinematic equations practice problems.

"RunCar" is a myblock which you can use to tell the car to move several different moves in a row using a text file! This is wonderful because downloading the entire "MoveCar" or "RunCar" myblock can take up to a minute, but downloading a text file called "RunCar.txt" telling the car how to move takes a fraction of the time. The way I set it up is that the first 3 lines contain the instructions for the car to do its first move, the next 3 lines contain the instructions for the car to do its second move, and so on. In the 3 lines, you simply write a number per line. The first number is stored as the Time for the run. The second number is the initial velocity of the run, and the third line is the acceleration of the run. An example text file called "RunCar.txt" is loaded onto the NXT when you run "GenerateTextFiles". You can upload that text file to see the example. Note: All that is necessary per line is a number. I wrote in the descriptors of the numbers in the example text file to make it understandable to humans.

"DisplayPosition" and "DisplayVelocity" are two handy myblocks which you can insert into the "MoveCar" myblock in order to have a position vs time and velocity vs time respectively show up on the display of the NXT as the car moves. These have not been tinkered with in a while and I may have broken them by updating other things but try them out!

TUNING PID PARAMETERS

Currently Kp is set to 1 inside a text file on the NXT called Kp.txt. It can be modified easily by downloading a text file called Kp.txt with a single number in the file. Having Kp = 1 means the motor power is multiplied by 1 times (1 - the percent error of the velocity) as the car moves. Ki is currently set to 0, but in case you want to turn on the integral term, it is defined as (1 - the percent error of the distance the motors have moved). If you want to use it, turn Kp down to .7sih, and turn Ki up to .1ish. Likewise Kd is set to 0, and is defined as (1 - the percent error of the acceleration of the car). If you want to add derivative control, set Kd to .01 or .001). There is some modification of the velocity and acceleration terms when their values are close to 0 because a percent error calculation would divide by 0 in that case.

THE MYCAR MYBLOCK HAS FINALLY BEEN COMMENTED!