-
Notifications
You must be signed in to change notification settings - Fork 1
/
03.2_Automatic_Tracking_Line.ino
65 lines (54 loc) · 2 KB
/
03.2_Automatic_Tracking_Line.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "Freenove_4WD_Car_for_Arduino.h"
#define TK_STOP_SPEED 0
#define TK_FORWARD_SPEED (90 + tk_VoltageCompensationToSpeed )
//define different speed levels
#define TK_TURN_SPEED_LV4 (180 + tk_VoltageCompensationToSpeed )
#define TK_TURN_SPEED_LV3 (150 + tk_VoltageCompensationToSpeed )
#define TK_TURN_SPEED_LV2 (-140 + tk_VoltageCompensationToSpeed )
#define TK_TURN_SPEED_LV1 (-160 + tk_VoltageCompensationToSpeed )
int tk_VoltageCompensationToSpeed; //define Voltage Speed Compensation
void setup() {
Serial.begin(9600);
Serial.setTimeout(200);
pinsSetup(); //set up pins
getTrackingSensorVal();//Calculate Voltage speed Compensation
}
void loop() {
//while (Serial.available() == 0) { }
String trackingSensorVal = Serial.readString();
switch (trackingSensorVal.toInt())
{
case 4: //000
motorRun(TK_FORWARD_SPEED, TK_FORWARD_SPEED); //car move forward
break;
case 0: //111
motorRun(TK_STOP_SPEED, TK_STOP_SPEED); //car stop
break;
case 10: //001
motorRun(TK_TURN_SPEED_LV4, TK_TURN_SPEED_LV1); //car turn
break;
case 2: //011
motorRun(TK_TURN_SPEED_LV3, TK_TURN_SPEED_LV2); //car turn right
delay(6000);
break;
case 1: //010
motorRun(TK_TURN_SPEED_LV2, TK_TURN_SPEED_LV3); //car turn left
delay(2000);
break;
default:
motorRun(TK_TURN_SPEED_LV2, TK_TURN_SPEED_LV3); //car turn left
delay(2000);
break;
}
}
void tk_CalculateVoltageCompensation() {
getBatteryVoltage();
float voltageOffset = 7 - batteryVoltage;
tk_VoltageCompensationToSpeed = 30 * voltageOffset;
}
//when black line on one side is detected, the value of the side will be 0, or the value is 1
u8 getTrackingSensorVal() {
u8 trackingSensorVal = 0;
trackingSensorVal = (digitalRead(PIN_TRACKING_LEFT) == 1 ? 1 : 0) << 2 | (digitalRead(PIN_TRACKING_CENTER) == 1 ? 1 : 0) << 1 | (digitalRead(PIN_TRACKING_RIGHT) == 1 ? 1 : 0) << 0;
return trackingSensorVal;
}