-
Notifications
You must be signed in to change notification settings - Fork 0
/
rc_car_v5.ino
243 lines (209 loc) · 6.1 KB
/
rc_car_v5.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Project: Arduino RC Car with iBUS Receiver and iBUS battery tension feedback
* Author: Pedro Igo Sousa Lucas
*/
#include <Servo.h>
#include <AFMotor.h> // By Adafruit (Access Mar 10th 2020 https://github.com/adafruit/Adafruit-Motor-Shield-library)
#include <IBusBM.h> // By bmellink (Access Jul 30th 2020 https://github.com/bmellink/IBusBM)
#include <iBUSTelemetry.h> // By adis1313 (Access Jul 30th https://github.com/adis1313/iBUSTelemetry-Arduino)
/*
* OBS1: IBusBM.h also has telemetry, but requires hardware mods (including a registor), so used another for telemetry reading
* OBS2: Originally used telemetry by Hrastovc (Access Mar 10th 2020 https://github.com/Hrastovc/iBUStelemetry), but changed
* over due to incompatibilities with IBusBM. New telemetry library has more sensors with more data types, but is a bit fussier
* with delays. Still works fine.
*/
#define channelAmount 5 // Number of channels
/******************************************
GLOBAL VARIABLES
******************************************/
// Motors declaration. AFMotor library
AF_DCMotor motorL(1);
AF_DCMotor motorR(2);
double inputChannel [channelAmount];
double vl, vr; // Motor velocities
// Motor Factors. Adjust to compensate for weaker motors. Max = 200, Min = 0
// setSpeed can receive 0 - 255, but i limited to 200 to avoid motor strain
// OBS: Only necessary if your controller doesn't have trim
double vlf = 250;
double vrf = 250;
int ledPin = 54; // Eye led's pin
int buzzer = 18;
int servoPin = 9;
// Variables for calculating tension
int tensionSensor = A1; // Reading from voltage sensor
float tension; // Calulated tension
double r1 = 30000; // Voltage sensor resistor 1
double r2 = 7500; // Voltage sensor resistor 2
Servo head;
iBUSTelemetry telemetry(50); // Sets telemetry output pin. On my mega, only worked on SPI pins
IBusBM IBus; // Declares IBus object for Rx inputs
// Channels
double lr; // Left / Right
double fb; // Forward / Back
// double bz; // Buzzer
double sv; // Servo
double ts; // Turn style
/******************************************
MAIN PROGRAM
******************************************/
//Receiver input assignment
void setup()
{
pinMode (tensionSensor, INPUT);
pinMode(ledPin, OUTPUT); //---pinMode(buzzer, OUTPUT);
head.attach(servoPin);
Serial.begin(115200);
// Data receiving
IBus.begin(Serial1); // Needs to be associated to a serial port
telemetry.begin(); // Sets telemetry
telemetry.addSensor(IBUS_MEAS_TYPE_EXTV); // Adds an external voltage sensor to telemetry output
}
void loop()
{
// input receiving (iBus reading)
// OBS: Pin A0 / 14 has weak solder and keeps losing contact
telemetry.run(); // Starts telemetry reading
for (int channel = 0; channel < channelAmount; ++channel)
{
inputChannel[channel] = IBus.readChannel(channel);
}
lr = inputChannel [0];
fb = inputChannel [1];
// bz = inputChannel [2];
sv = inputChannel [3];
ts = inputChannel [4];
// voice();
if (lr > 1)
{
Movement();
headMovement();
}
// Calculates tension
tension = printTension(analogRead(tensionSensor), r1, r2);
telemetry.setSensorValueFP(1, tension); // Sets float value to telemetry
// Console feedback
Serial.print("Channel 1 (lr):");
Serial.print(lr); Serial.print("\t");
Serial.print("Channel 2 (fb):");
Serial.print(fb); Serial.print("\t");
Serial.print("Channel 4 (sv):");
Serial.print(sv); Serial.print("\t");
Serial.print("Channel 5 (ts):");
Serial.print(ts); Serial.print("\n");
Serial.print("Tension:");
Serial.print(tension); Serial.print("\n");
}
/******************************************
FUNCTIONS
******************************************/
double top (double a, double b)
{
if (a > b)
{
return a;
}
return b;
}
void Movement ()
{
if (fb > 1550) // Moves FORWARD. Bigger than positive threshold
{
vl = vlf * ((fb - 1550)/(1990 - 1550));
vr = vrf * ((fb - 1550)/(1990 - 1550));
if (lr < 1450)
{
vl = top ((vl - (vlf * ((1450 - lr)/(1450 - 990)))), 0);
}
if (lr > 1550)
{
vr = top ((vr - (vrf * ((lr - 1550)/(1990 - 1550)))), 0);
}
motorL.setSpeed(vl);
motorL.run(FORWARD);
motorR.setSpeed(vr);
motorR.run(FORWARD);
}
else if (fb < 1450) // Moves BACKWARD. Lower than positive threshold
{
vl = vlf * ((1450 - fb)/(1450 - 990));
vr = vrf * ((1450 - fb)/(1450 - 990));
if (lr < 1450)
{
vl = top ((vl - (vlf * ((1450 - lr)/(1450 - 990)))), 0);
}
if (lr > 1550)
{
vr = top ((vr - (vrf * ((lr - 1550)/(1990 - 1550)))), 0);
}
motorL.setSpeed(vl);
motorL.run(BACKWARD);
motorR.setSpeed(vr);
motorR.run(BACKWARD);
}
else // Stops
{
if (ts < 1450) // Normal mode
{
digitalWrite(ledPin, 0); // Set eyes to not glow
vl = 0;
vr = 0;
motorL.setSpeed(vl);
motorL.run(FORWARD);
motorR.setSpeed(vr);
motorR.run(FORWARD);
}
else // Crazy Turnz
{
digitalWrite(ledPin, 255); // Set eyes to glow
if (lr < 1450)
{
vl = vlf * ((1450 - lr)/(1450 - 990));
vr = vrf * ((1450 - lr)/(1450 - 990));
motorL.setSpeed(vl);
motorL.run(BACKWARD);
motorR.setSpeed(vr);
motorR.run(FORWARD);
}
else if (lr > 1550)
{
vl = vlf * ((lr - 1550)/(1990 - 1550));
vr = vrf * ((lr - 1550)/(1990 - 1550));
motorL.setSpeed(vl);
motorL.run(FORWARD);
motorR.setSpeed(vr);
motorR.run(BACKWARD);
}
else
{
vl = 0;
vr = 0;
motorL.setSpeed(vl);
motorL.run(FORWARD);
motorR.setSpeed(vr);
motorR.run(FORWARD);
}
}
}
}
void headMovement()
{
int aux;
aux = (180.0*(sv - 990))/1000.0;
aux = top(aux, 0);
head.write(aux);
}
// Calculates tension reading
double printTension(int reading, double resistor_1, double resistor_2)
{
return ((reading * 5)/((1024 * resistor_2)/(resistor_1 + resistor_2)));
}
/*
void voice()
{
int aux;
aux = bz - 990;
aux = top(aux, 0);
//tone(buzzer, 500);
//noTone(buzzer);
}
*/