-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrc_car_v3.ino
180 lines (161 loc) · 4.06 KB
/
rc_car_v3.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
/*
* Project: Arduino RC Car with PPM Receiver
* Autor: Pedro Igo Sousa Lucas
*/
#include <Servo.h>
#include <AFMotor.h>
#include <PPMReader.h> // By Nikkilae (https://github.com/Nikkilae/PPM-reader.git)
#include <InterruptHandler.h> // By zeitgeist87 (https://github.com/zeitgeist87/InterruptHandler.git)
#define ledPin 14 // Eye led's pin
#define channelAmount 5 // Number of channels
// Motors declaration. AFMotor library
AF_DCMotor motorL(1);
AF_DCMotor motorR(2);
double inputValue [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 = 190;
double vrf = 200;
int servoPin = 19;
Servo head;
//Receiver pin (must be an interrupt capable pin)
int interruptPin = 2;
PPMReader ppm(interruptPin, channelAmount);
//Channels
double lr; // Left / Right
double fb; // Forward / Back
double sv; // Servo
double ts; // Turn style
//Receiver input assignment
void setup()
{
pinMode(14, OUTPUT); pinMode(10, OUTPUT);
head.attach(servoPin);
Serial.begin(9600);
}
void loop()
{
// input receiving (PPM reading)
// OBS: Pin A0 / 14 has weak solder and keeps losing contact
for (int channel = 1; channel <= channelAmount; ++channel)
{
inputValue[channel - 1] = ppm.latestValidChannelValue (channel, 0);
}
lr = inputValue [0];
fb = inputValue [1];
sv = inputValue [3];
ts = inputValue [4];
if (lr > 1)
{
Movement();
headMovement();
}
// 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");
delay(100); // Here just to make the terminal
}
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
{
analogWrite(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
{
analogWrite(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);
}