This example illustrates how to write a proportional, integral controller for the NXT. On the front panel, the user inputs the desired position (in degrees) of the motor. The user can also adjust the control gains, Kp and Ki which affects the input power of the motor. Kp is a proportional gain, it scales the difference between the motor's current position and the desired position--this difference is known as the error. Ki is an integral gain, it scales the total accumulated error during the program. Play around with the gains to see what works the best.
You may notice that a NXT car with a steering value of 0 does not drive in a straight line. This problem can be resolved by implementing proportional control, where the motor steering input is a function of the difference between the motor rotation readings.
This program turns the motor on until the touch sensor is pressed, then displays "Hello, World!" on the NXT screen.
In this program, motors A and B rotate 720 degrees (two revolutions), the NXT brick plays the note C for 1 second, and then motor A rotates 90 degrees.
This example enables a NXT car with a light sensor to detect when it has reached the edge of a table, back up, and turn in a random direction, this continues until the program is stopped.
This program uses multiple motors and multiple touch sensors. When it begins motor A runs forward until touch sensor 1 is pressed, motor B then begins to run until touch sensor 2 is pressed.
This program turns the car for a set amount of time. The turning time, steering, power, and ports are all controls rather than constants, which means that they can be adjusted on the front panel of the program.
This program runs the motor until the touch sensor is pressed and stops the while loop causing the motor to brake.
You may notice that a NXT car with independently controlled wheels might not be able drive in a straight line even though the power setting on the motors are the same. Slight differences in the motors and wheels cause them to rotate at different rates. This problem can be resolved by implementing proportional control, where the power setting of the motors is a function of the difference between the motor rotation readings.
motor A power = constant + gain*(encoder A - encoder C)
motor C power = constant - gain*(encoder A - encoder C)
This program makes the car turn until the touch sensor is pressed. To control the degree and direction of steering, input a number between 100 and -100.