|
| 1 | +#include "Arduino_Robot_Firmware.h" |
| 2 | +#include "sensor_line.h" |
| 3 | + |
| 4 | +Arduino_Robot_Firmware robot; |
| 5 | +SensorLine line(EXT_A2,EXT_A1,EXT_A0); |
| 6 | + |
| 7 | +HardwareSerial serial(PA10,PA9); |
| 8 | + |
| 9 | +unsigned long tled = 0; |
| 10 | +bool led_value = false; |
| 11 | + |
| 12 | +void setup() { |
| 13 | + serial.begin(115200); |
| 14 | + robot.begin(); |
| 15 | + line.begin(); |
| 16 | + robot.disableIlluminator(); |
| 17 | + tled = millis(); |
| 18 | +} |
| 19 | + |
| 20 | +void loop() { |
| 21 | + robot.updateTouch(); |
| 22 | + if (robot.getTouchEnter()){ |
| 23 | + robot.setLedBuiltin(HIGH); |
| 24 | + simpleMotors(); |
| 25 | + } |
| 26 | + if (robot.getTouchOk()){ |
| 27 | + robot.setLedBuiltin(HIGH); |
| 28 | + line_follower(); |
| 29 | + } |
| 30 | + if (millis()-tled>500){ |
| 31 | + robot.setLedBuiltin(led_value); |
| 32 | + led_value=!led_value; |
| 33 | + tled=millis(); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +void simpleMotors(){ |
| 38 | + int status=0; |
| 39 | + unsigned long tmotor=millis(); |
| 40 | + while(!robot.getTouchDelete()){ |
| 41 | + switch (status){ |
| 42 | + case 0: |
| 43 | + robot.motor_left->setSpeed(4095); |
| 44 | + robot.motor_right->setSpeed(4095); |
| 45 | + robot.setLedLeft(COLOR_BLUE); |
| 46 | + robot.setLedRight(COLOR_RED); |
| 47 | + if (millis()-tmotor>1000){ |
| 48 | + status++; |
| 49 | + tmotor=millis(); |
| 50 | + serial.println("forward"); |
| 51 | + } |
| 52 | + break; |
| 53 | + case 1: |
| 54 | + robot.motor_left->setSpeed(4095); |
| 55 | + robot.motor_right->setSpeed(-4095); |
| 56 | + robot.setLedLeft(COLOR_RED); |
| 57 | + robot.setLedRight(COLOR_BLUE); |
| 58 | + if (millis()-tmotor>1000){ |
| 59 | + status++; |
| 60 | + status=0; |
| 61 | + tmotor=millis(); |
| 62 | + serial.println("turn"); |
| 63 | + } |
| 64 | + break; |
| 65 | + } |
| 66 | + robot.updateTouch(); |
| 67 | + } |
| 68 | + robot.motor_left->setSpeed(0); |
| 69 | + robot.motor_right->setSpeed(0); |
| 70 | + robot.setLeds(COLOR_BLACK); |
| 71 | +} |
| 72 | + |
| 73 | +void line_follower(){ |
| 74 | + float e=0; |
| 75 | + float sum_e=0; |
| 76 | + float kp = 100.0; |
| 77 | + float ki = 0.1; |
| 78 | + float control; |
| 79 | + unsigned long tline = millis(); |
| 80 | + |
| 81 | + while(!robot.getTouchDelete()){ |
| 82 | + robot.updateTouch(); |
| 83 | + if (millis()-tline>10){ |
| 84 | + tline=millis(); |
| 85 | + line.update(); |
| 86 | + line.updateCentroid(); |
| 87 | + control = kp*line.getCentroid(); |
| 88 | + serial.println(control); |
| 89 | + |
| 90 | + if ((control<1)&&(control>-1)){ |
| 91 | + robot.setLeds(COLOR_GREEN); |
| 92 | + } |
| 93 | + else{ |
| 94 | + if (control>=1){ |
| 95 | + robot.setLedLeft(COLOR_ORANGE); |
| 96 | + robot.setLedRight(COLOR_BLACK); |
| 97 | + } |
| 98 | + else{ |
| 99 | + robot.setLedLeft(COLOR_BLACK); |
| 100 | + robot.setLedRight(COLOR_ORANGE); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + float control_left=2000-control*400; |
| 105 | + float control_right=2000+control*400; |
| 106 | + |
| 107 | + if (control_left>4095){ |
| 108 | + control_left=4095; |
| 109 | + } |
| 110 | + |
| 111 | + if (control_left<-4095){ |
| 112 | + control_left=-4095; |
| 113 | + } |
| 114 | + |
| 115 | + if (control_right>4095){ |
| 116 | + control_right=4095; |
| 117 | + } |
| 118 | + |
| 119 | + if (control_right<-4095){ |
| 120 | + control_right=-4095; |
| 121 | + } |
| 122 | + |
| 123 | + robot.motor_left->setSpeed(control_left); |
| 124 | + robot.motor_right->setSpeed(control_right); |
| 125 | + } |
| 126 | + } |
| 127 | + robot.motor_left->setSpeed(0); |
| 128 | + robot.motor_right->setSpeed(0); |
| 129 | + robot.setLeds(COLOR_BLACK); |
| 130 | +} |
0 commit comments