-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwm_motor_setup.ino
176 lines (142 loc) · 4.87 KB
/
pwm_motor_setup.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
/*
* Yuriy Movchan 16/08/2020
*
* Utility mehods to help determine when bidirectional motor stop/start, etc...
*/
// ---------------------------------------------------------------------------
// Customize here pulse lengths as needed
#define MIN_PULSE_LENGTH 1000 // Minimum pulse length in µs
#define MID_PULSE_LENGTH 1500 // Middle pulse length in µs
#define MAX_PULSE_LENGTH 2000 // Maximum pulse length in µs
// ---------------------------------------------------------------------------
uint16_t pwmMotorSetupLeftPWM, pwmMotorSetupRightPWM;
/**
Initialisation routine
*/
void setup_PwmMotorSetup() {
setPWM_PwmMotorSetup(MID_PULSE_LENGTH, MID_PULSE_LENGTH);
displayInstructions_PwmMotorSetup();
}
/**
Main function
*/
void loop_PwmMotorSetup() {
if (Serial.available()) {
switch (Serial.read()) {
case 'q' : Serial.println("Increasing left PWM");
setPWM_PwmMotorSetup(++pwmMotorSetupLeftPWM, pwmMotorSetupRightPWM);
break;
case 'a' : Serial.println("Decreasing left PWM");
setPWM_PwmMotorSetup(--pwmMotorSetupLeftPWM, pwmMotorSetupRightPWM);
break;
case 'w' : Serial.println("Increasing right PWM");
setPWM_PwmMotorSetup(pwmMotorSetupLeftPWM, ++pwmMotorSetupRightPWM);
break;
case 's' : Serial.println("Decreasing right PWM");
setPWM_PwmMotorSetup(pwmMotorSetupLeftPWM, --pwmMotorSetupRightPWM);
break;
case '0' : Serial.println("Sending minimum throttle");
setPWM_PwmMotorSetup(MIN_PULSE_LENGTH, MIN_PULSE_LENGTH);
break;
case '`' : Serial.println("Sending middle throttle");
setPWM_PwmMotorSetup(MID_PULSE_LENGTH, MID_PULSE_LENGTH);
break;
case '2' : Serial.println("Sending maximum throttle");
setPWM_PwmMotorSetup(MAX_PULSE_LENGTH, MAX_PULSE_LENGTH);
break;
case '3' : Serial.print("Running test in 3");
delay(1000);
Serial.print(" 2");
delay(1000);
Serial.println(" 1...");
delay(1000);
testNormal_PwmMotorSetup();
break;
case '4' : Serial.print("Running test in 3");
delay(1000);
Serial.print(" 2");
delay(1000);
Serial.println(" 1...");
delay(1000);
testBidirectionalDown_PwmMotorSetup();
break;
case '5' : Serial.print("Running test in 3");
delay(1000);
Serial.print(" 2");
delay(1000);
Serial.println(" 1...");
delay(1000);
testBidirectionalUp_PwmMotorSetup();
break;
}
}
}
/**
Test function: send min throttle to max throttle to each ESC.
*/
void testNormal_PwmMotorSetup() {
for (int i = MIN_PULSE_LENGTH; i <= MAX_PULSE_LENGTH; i += 5) {
Serial.print("Pulse length = ");
Serial.println(i);
setPWM_PwmMotorSetup(i, i);
delay(200);
}
Serial.println("STOP");
setPWM_PwmMotorSetup(MIN_PULSE_LENGTH, MIN_PULSE_LENGTH);
}
/**
Test function: send min throttle to max throttle to each ESC.
*/
void testBidirectionalDown_PwmMotorSetup() {
for (int i = MID_PULSE_LENGTH; i >= MIN_PULSE_LENGTH; i -= 5) {
Serial.print("Pulse length = ");
Serial.println(i);
setPWM_PwmMotorSetup(i, i);
delay(200);
}
Serial.println("STOP");
setPWM_PwmMotorSetup(MID_PULSE_LENGTH, MID_PULSE_LENGTH);
}
/**
Test function: send min throttle to max throttle to each ESC.
*/
void testBidirectionalUp_PwmMotorSetup() {
for (int i = MID_PULSE_LENGTH; i <= MAX_PULSE_LENGTH; i += 5) {
Serial.print("Pulse length = ");
Serial.println(i);
setPWM_PwmMotorSetup(i, i);
delay(200);
}
Serial.println("STOP");
setPWM_PwmMotorSetup(MID_PULSE_LENGTH, MID_PULSE_LENGTH);
}
void setPWM_PwmMotorSetup(uint16_t newPwmMotorSetupLeftPWM, uint16_t newPwmMotorSetupRightPWM) {
pwmMotorSetupLeftPWM = newPwmMotorSetupLeftPWM;
pwmMotorSetupRightPWM = newPwmMotorSetupRightPWM;
Serial.print("Left PWM: ");
Serial.println(pwmMotorSetupLeftPWM);
Serial.print("Right PWM: ");
Serial.println(pwmMotorSetupRightPWM);
#ifdef PWM_LEFT_MOTOR_ENABLE
writeMicroseconds_pwmMotor(RC_SERVO_LEFT_IDX, pwmMotorSetupLeftPWM);
#endif
#ifdef PWM_RIGHT_MOTOR_ENABLE
writeMicroseconds_pwmMotor(RC_SERVO_RIGHT_IDX, pwmMotorSetupRightPWM);
#endif
}
/**
Displays instructions to user
*/
void displayInstructions_PwmMotorSetup() {
Serial.println("READY - PLEASE SEND INSTRUCTIONS AS FOLLOWING :");
Serial.println("\tq : Increase left motor PWM");
Serial.println("\ta : Decrease left motor PWM");
Serial.println("\tw : Increase right motor PWM");
Serial.println("\ts : Decrease right motor PWM");
Serial.println("\t0 : Send min throttle");
Serial.println("\t1 : Send mid throttle");
Serial.println("\t2 : Send max throttle");
Serial.println("\t3 : Run test function from 1000 to 2000 PWM");
Serial.println("\t4 : Run test function from 1500 to 1000 PWM");
Serial.println("\t5 : Run test function from 1500 to 2000 PWM");
}