-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRPiArduBot.ino
129 lines (115 loc) · 3.03 KB
/
RPiArduBot.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <Servo.h>
#include <Motor.h>
/* A program to interface with python on rpi
*
*
*
* Motor:
*+--------+-----------+-------------+---------+
*| Device | byte 1 | byte 2 | byte 3 |
*+--------+-----------+-------------+---------+
*| Motor | A/B Motor | Foward/Back | Speed |
*| m | 0/1 | 0/1 | 0 - 255 |
*+--------+-----------+-------------+---------+
*
*
* Servo:
*+-------+----------+---------------+--------------------+
*| Servo | pan/tilt | position | up/left/down/right |
*+-------+----------+---------------+--------------------+
*| s | 0/1 | (-30) <-> 230 | 0 / 1 / 2 / 3 |
*+-------+----------+---------------+--------------------+
*
*
*
*/
#define debug 1
//communication buffer of 4 bytes
String getInfo;
boolean strComplete = false;
Motor motorA(Motor::MOTOR_A);
Motor motorB(Motor::MOTOR_B);
Servo pan;
Servo tilt;
void setup()
{
// Open Serial communication
Serial.begin(115200);
Serial.println("Motor shield DC motor Test:\n");
getInfo.reserve(5);
pan.attach(5);
tilt.attach(6);
}
void loop() {
if (strComplete)
{
String str = getInfo;
switch(str[0])
{
case 'm' : //Motor
handleMotor(str);
break;
case 'd' : //Distance sensor
break;
case 'c' : //Get Motor current consumption
break;
case 's' :
handleServo(str);
break;
default:
Serial.println("Error at:");
Serial.println(getInfo);
}
getInfo = ""; //Reset bufver and then bool flag below
strComplete = false;
}
}
void serialEvent()
{
while (Serial.available()) {
char inChar = (char)Serial.read();
getInfo += inChar;
if (getInfo.length() >= 4 || inChar == '\n') {
strComplete = true;
}
}
}
void handleMotor(String info)
{
int d; //direction
int v; //pwm value
int m; //which motor
v = getInt(info[3]);
// subtract zero to convert
d = info[2] - '0';
d = (d == 0) ? -1 : (d == 1) ? 1 : 0; //if direction is 0 then it is foward; if direction is 1 then backward, otherwise zero(no effect)
m = info[1] - '0';
// multiply to give direction (d is pos or neg)
(m == 0) ? motorA.move(v * d) : (m == 1) ? motorB.move(v * d) : doNothing(); //if motor is 0 then turn A motor; if motor is 1 then B motor, otherwise do nothing
#if debug
Serial.println("line: " + info);
Serial.println("Value: " + String(v));
Serial.println("Converted Value: " + String(v * d));
Serial.println("Direction: " + String(info[2]));
Serial.println("Converted Direction: " + String(d));
#endif
}
void handleServo(String info)
{
Serial.println("Servo: ");
int p; //postion of the servo 1-180
int m; // servo 0 -> pan | 1 -> tilt
p = getInt(info[2]);
m = (int)(info[1] - '0'); //get motor, pan or tilt
(m == 0) ? pan.write(p) : ((m == 1) ? tilt.write(p) : doNothing());
}
void doNothing()
{
//do absolutely nothing; void
}
//convert char to int
int getInt(char c)
{
int v = (int)c;
return v = v < 0 ? (v + 256) : v; //convert signed char to unsigned
}