Created
February 19, 2015 01:16
-
-
Save johnholbrook/f63a002bde9409bbff97 to your computer and use it in GitHub Desktop.
A proportional line follower written in ROBOTC for LEGO Mindstorms RCX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma config(Sensor, S1, touch, sensorTouch) | |
| #pragma config(Sensor, S3, light, sensorReflection) | |
| #pragma config(Motor, motorA, left, tmotorNormal, openLoop, reversed) | |
| #pragma config(Motor, motorC, right, tmotorNormal, openLoop) | |
| //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// | |
| task main() | |
| { | |
| int darkVal; | |
| int lightVal; | |
| int avg; | |
| int error; | |
| int constant; | |
| int turn; | |
| int power; | |
| bool sensorTouched; | |
| sensorTouched = false; | |
| while (sensorTouched == false) // set dark value | |
| { | |
| if (SensorValue[touch] == 1) | |
| { | |
| darkVal = SensorValue[light]; | |
| sensorTouched = true; | |
| } | |
| } | |
| SetUserDisplay(darkVal); // display dark value | |
| wait1Msec(1000); | |
| sensorTouched = false; | |
| while (sensorTouched == false) //set light value | |
| { | |
| if (SensorValue[touch] == 1) | |
| { | |
| lightVal = SensorValue[light]; | |
| sensorTouched = true; | |
| } | |
| } | |
| SetUserDisplay(lightVal); // display light value | |
| avg = ((darkVal+lightVal)/2); // calculate average of dark and light values | |
| constant = 7; // define constant of proportionality; changing this value will change the robot's | |
| // responsiveness/sensitivity | |
| power = 75; // set base motor power | |
| while (true) | |
| { | |
| error = (SensorValue[light] - avg); | |
| turn = (constant*error); | |
| motor[left] = (power + turn); | |
| motor[right] = (power - turn); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment