-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesc_driver.ino
110 lines (79 loc) · 2.14 KB
/
esc_driver.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
#include <Servo.h>
boolean debug = false;
Servo ESC; // create servo object to control the ESC
int throttle_pin = 0;
int esc_pin = 2;
int pos = 0;
int throttle = 0;
int output_min = 35;
int output_max = 160;
int throttle_min = 90;
int throttle_max = 950;
int sweep_delay = 12;
int sweep_min = 0;
int sweep_max = output_max;
int sweep_step = 2;
int mapped_throttle;
int stale_throttle;
/*
* Initialization function needed with some ESCs
*/
void sweep() {
if (debug) Serial.println("beginning sweep");
for(pos = sweep_min; pos < sweep_max; pos += sweep_step) {
ESC.write(pos);
if (debug) {Serial.print("sweeping up.. ");Serial.println(pos);}
delay(sweep_delay);
}
for(pos = sweep_max; pos >= sweep_min; pos-=sweep_step) {
ESC.write(pos);
if (debug) {Serial.print("sweeping down.. "); Serial.println(pos);}
delay(sweep_delay);
}
}
/*
* Normalzie a throttle input, to avoid crazyness from a dirty pot.
*/
int normalize_throttle(int throttle_val, int last_throttle) {
// Change throttle
if (throttle_val > last_throttle) {
throttle_val = last_throttle + 2;
}
if (throttle_val < last_throttle) {
throttle_val = last_throttle - 4;
}
// Keep the throttle within bounds
if (throttle_val > output_max) {
throttle_val = output_max;
}
if (throttle_val < output_min) {
throttle_val = output_min;
}
return throttle_val;
}
void setup() {
if (debug) Serial.begin(9600);
ESC.attach(esc_pin);
ESC.write(pos);
sweep();
delay(500);
}
void loop() {
//if (debug) Serial.println("loop iteration");
throttle = analogRead(throttle_pin);
//if (debug) Serial.println(throttle);
mapped_throttle = map(
throttle,
throttle_min,
throttle_max,
output_min,
output_max
);
throttle = normalize_throttle(mapped_throttle, stale_throttle);
if (debug) {Serial.print(mapped_throttle);Serial.print(" - ");Serial.println(throttle);}
if (stale_throttle != throttle) {
ESC.write(throttle);
}
stale_throttle = throttle;
delay(15);
}