Basic Line Following Program motors: p25, p10 inputs: p15, p16 Current settings: lineToSpeedFactor = 10 / 3 speedModifier = -4
Dependencies: mbed
Fork of RenBuggyLineFollower by
This small program lets a buggy follow a line (black line on white background)
main.cpp
- Committer:
- DanArgust
- Date:
- 2016-07-14
- Revision:
- 2:2439f5a4a93d
- Parent:
- 0:466ee63955df
- Child:
- 3:936111f70e37
File content as of revision 2:2439f5a4a93d:
/********************************************************* *RenBuggyLineFollower * *Author: Dan Argust * * * *This program demonstates the use of two sensors to * *detect and follow a thin black line. * *********************************************************/ #include "mbed.h" AnalogIn ain0(p15); AnalogIn ain1(p16); PwmOut pwm0(p25); PwmOut pwm1(p10); float Lmotor; float Rmotor; float newLine; float oldLine; bool searching; int counter; void getMotorSpeeds(float a,float b){ float inputs[2] = {a,b}; float max_speed = 1.0; Lmotor = 0; Rmotor = 0; newLine = 0; float line_to_speed_factor = max_speed/0.3; for(int i = 0;i<2;++i){ newLine += inputs[i]*(i+1); } float sum = 0; for (int i = 0;i<2;++i){ sum += inputs[i]; } newLine = newLine / sum; if((a<0.2)&&(b<0.2)){ if(oldLine>1.5) newLine = 2; else newLine = 1; searching = true } if(searching){ if(abs(a - b)<0.2){ newLine = oldLine; } counter++ if(counter>300){ newLine = 3 - oldLine; counter = 0; } else{ searching = false; } } oldLine = newLine; Lmotor = newLine*line_to_speed_factor-4; Rmotor = 2-(newLine*line_to_speed_factor-4); if(Lmotor>max_speed){ Lmotor = max_speed; } if(Rmotor>max_speed){ Rmotor = max_speed; } } int main() { searching = false counter = 0 while(true){ getMotorSpeeds(ain0.read(),ain1.read()); pwm0 = Lmotor; pwm1 = Rmotor; wait_ms(10); } }