-
Notifications
You must be signed in to change notification settings - Fork 1
/
esc-control.cpp
138 lines (113 loc) · 3.45 KB
/
esc-control.cpp
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
#include "main.h"
#include "wirish.h"
#include "utils.h"
#include "esc-control.h"
// Servo constants
#define SERVO_MIN 3430
#define SERVO_MAX 6800
#define PPM_CNTS_TO_DEG 0.09
#define SERVO_ANGLE_TO_DUTY 37.44444
uint8 gpio_state[BOARD_NR_GPIO_PINS];
const char* dummy_data = ("qwertyuiopasdfghjklzxcvbnmmmmmm,./1234567890-="
"qwertyuiopasdfghjklzxcvbnm,./1234567890");
void ppm_decode(void){
SerialUSB.println("Decoding DIY Drones PPM encoder on pin D27");
int pin = 27;
uint8 prev_state;
int time_elapsed = 0;
int time_start = 0;
int i = 0;
int channels[8];
float angle;
pinMode(pin, INPUT);
prev_state = (uint8)digitalRead(pin);
while(!SerialUSB.available()){
uint8 current_state = (uint8)digitalRead(pin);
if (current_state != prev_state) {
if (current_state) {
time_elapsed = (micros() - time_start);
time_start = micros();
}else{
#ifdef USB_VERBOSE
SerialUSB.print(i);
SerialUSB.print(":");
SerialUSB.print(time_elapsed);
#endif
if(time_elapsed < 2500 && i < 8){
if(i==2){
if(time_elapsed < 1000){
angle = 0.0;
}else if(time_elapsed > 2000){
angle = 90.0;
}else{
angle = (float)((time_elapsed-1000)*PPM_CNTS_TO_DEG);
}
SerialUSB.print(":");
SerialUSB.print(angle);
set_servo_angle(angle);
}
channels[i++] = time_elapsed;
}else{
#ifdef USB_VERBOSE
SerialUSB.println("");
#endif
i=0;
}
#ifdef USB_VERBOSE
SerialUSB.print("\t");
#endif
time_elapsed = 0;
}
prev_state = current_state;
}
}
SerialUSB.println("Done!");
}
void set_servo_angle(float angle)
{
//SERVO_MIN = 3430
//SERVO_MAX = 6800
//SERVO_CNTS_TO_DEG=0.0267
int duty;
disable_usarts();
duty = SERVO_MIN + (int)(angle * SERVO_ANGLE_TO_DUTY);
if(duty > SERVO_MAX) duty = SERVO_MAX;
pwmWrite(ROTOR1_PIN, duty);
}
void cmd_servo_sweep(void) {
SerialUSB.println("Testing D16 PWM header with a servo sweep. "
"Press any key to stop.");
SerialUSB.println();
disable_usarts();
init_all_timers(21);
for (uint32 i = 0; i < BOARD_NR_PWM_PINS; i++) {
if (boardUsesPin(i))
continue;
pinMode(boardPWMPins[i], PWM);
pwmWrite(boardPWMPins[i], 4000);
}
// 1.25ms = 4096counts = 0deg
// 1.50ms = 4915counts = 90deg
// 1.75ms = 5734counts = 180deg
//int rate = 4096;
int rate = 3430; // 1 ms
while (!SerialUSB.available()) {
rate += 20;
if (rate > 6800)//5734) 6800 = 2ms
rate = 3430;//4096;
//for (uint32 i = 0; i < BOARD_NR_PWM_PINS; i++) {
// if (boardUsesPin(i))
// continue;
// pwmWrite(boardPWMPins[i], rate);
//}
pwmWrite(16, rate);
delay(20);
}
for (uint32 i = 0; i < BOARD_NR_PWM_PINS; i++) {
if (boardUsesPin(i))
continue;
pinMode(boardPWMPins[i], OUTPUT);
}
init_all_timers(1);
enable_usarts();
}