-
Notifications
You must be signed in to change notification settings - Fork 21
/
motorctrl.c
280 lines (237 loc) · 9.14 KB
/
motorctrl.c
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <stdio.h>
#include "motorctrl.h"
#include "common.h"
#include "global.h"
#include "timers.h"
int16_t setPWMBlade, setPWMLeft, setPWMRight = 0;
int16_t curPWMBlade, curPWMLeft, curPWMRight = 0;
int32_t tiltCounter = 0;
void setpwm(uint16_t blade, uint16_t left, uint16_t right) {
if (blade > 2047) blade=2047;
if (left > 2047) left=2047;
if (right > 2047) right=2047;
if (blade < 0) blade=0;
if (left < 0) left=0;
if (right < 0) right=0;
LPC_PWM1->MR1 = blade;
LPC_PWM1->MR4 = left;
LPC_PWM1->MR5 = right;
LPC_PWM1->LER |= (1<<1) | (1<<4) | (1<<5);
}
const char *MotorRequestStrings[] = {
"MOTORREQ_IDLE",
"MOTORREQ_ENABLE",
"MOTORREQ_DISABLE",
"MOTORREQ_SETSPEED",
"MOTORREQ_BRAKEON",
"MOTORREQ_BRAKEOFF",
"MOTORREQ_EMGSTOP",
"MOTORREQ_RESETEMG"
};
void motionSensor_Timer(TimerHandle_t xTimer) {
if (!GPIO_CHK_PIN(SENSOR_STUCK) || !GPIO_CHK_PIN(SENSOR_STUCK2) || !GPIO_CHK_PIN(SENSOR_LIFT) || !GPIO_CHK_PIN(SENSOR_COLLISION))
sensorMsg.blockForward = 1;
// If robot tilted too much / upside down and blade on emergancy stop!
if (sensorMsg.accelZ < 5000 && sensorMsg.currentPWMBlade > 0) {
tiltCounter++;
if (tiltCounter > 500) {
//debug(" TILT EMG STOP");
xMotorMsgType MotorMsg;
MotorMsg.action = MOTORREQ_EMGSTOP;
MotorMsg.pwm.left = 0;
MotorMsg.pwm.right = 0;
MotorMsg.pwm.blade = 0;
xQueueSend(xMotorMsgQueue, &MotorMsg, xDelay500);
}
} else {
tiltCounter=0;
}
}
void allStop() {
setpwm(0,0,0);
setPWMBlade = setPWMLeft = setPWMRight = 0;
curPWMBlade = curPWMLeft = curPWMRight = 0;
}
#define INCREASE_RATE 300
#define DECREASE_RATE 500
// More work to be done if deaccel should be faster than accel
void ramp(int16_t* target_speed, int16_t* current_speed) {
if (*current_speed < *target_speed) {
*current_speed += INCREASE_RATE;
if (*current_speed > *target_speed) *current_speed = *target_speed;
}
if (*current_speed > *target_speed) {
*current_speed -= INCREASE_RATE;
if (*current_speed < *target_speed) *current_speed = *target_speed;
}
}
void motorcontrol_timer(TimerHandle_t xTimer) {
uint16_t PWMBlade, PWMLeft, PWMRight = 0;
// Quick ramp to setpwm
ramp(&setPWMLeft, &curPWMLeft);
ramp(&setPWMRight, &curPWMRight);
ramp(&setPWMBlade, &curPWMBlade);
/*curPWMLeft = setPWMLeft;
curPWMRight = setPWMRight;
*/
PWMBlade = curPWMBlade;
// Make PWM value positive and correct the wheel direction
if (curPWMLeft < 0) {
PWMLeft = -curPWMLeft;
GPIO_CLR_PIN(MOTOR_LEFT_FORWARD);
} else {
PWMLeft = curPWMLeft;
GPIO_SET_PIN(MOTOR_LEFT_FORWARD);
}
if (curPWMRight < 0) {
PWMRight = -curPWMRight;
GPIO_SET_PIN(MOTOR_RIGHT_FORWARD);
} else {
PWMRight = curPWMRight;
GPIO_CLR_PIN(MOTOR_RIGHT_FORWARD);
}
// if pwm = 0 brake on (wheels only, blade can freespin to stop since its going much faster)
if (curPWMLeft == 0) {
GPIO_CLR_PIN(MOTOR_LEFT_BRAKE);
} else {
GPIO_SET_PIN(MOTOR_LEFT_BRAKE);
}
if (curPWMRight == 0) {
GPIO_CLR_PIN(MOTOR_RIGHT_BRAKE);
} else {
GPIO_SET_PIN(MOTOR_RIGHT_BRAKE);
}
setpwm(PWMBlade, PWMLeft, PWMRight);
}
/*
motorcontrol_timer
* fast ramp curpwm -> setpwm
* if setpwm 0, curpwm 0 but ticks increasing => brake on (?time? / until stopped?)
future:
We have to measure pulse/time (10ms? 100ms?) for some setpwm so we know speed curve on flat grass.
* check actual speed > set speed => lower setpwm temporarily
* check actual speed >much> set speed => software pwm brake on
* handle softwarepwm brake? (freertos timer or hardware timer?? we have TIMER2_IRQHandler in sensors - move this timer here?)
Also check current to motors and blade pulses/time if it is bogged down or not
Initial test seems that reversing direction bit will brake less than full brake.
*/
void motorCtrl_Task(void *pvParameters) {
xMotorRequestType lastState = -1;
xMotorMsgType MotorMsg;
MotorCtrl_Init();
allStop();
MotorMsg.action = MOTORREQ_IDLE;
MotorMsg.pwm.left = 0;
MotorMsg.pwm.right = 0;
MotorMsg.pwm.blade = 0;
xQueueSend(xMotorMsgQueue, &MotorMsg, xDelay25);
// We need to wait for sensor to start.
vTaskDelay(xDelay1000);
TimerHandle_t timer1 = xTimerCreate("Motion sensors", TicksPerMS, pdTRUE, NULL, motionSensor_Timer);
xTimerStart(timer1, 0);
TimerHandle_t timer2 = xTimerCreate("PWM Handler", pdMS_TO_TICKS(50), pdTRUE, NULL, motorcontrol_timer);
xTimerStart(timer2, 0);
for (;;) {
if (sensorMsg.watchdogSPI > 5000) {
allStop();
//xScreenMsgType screenMsg;
//screenMsg.time=50;
//sprintf(screenMsg.text, "ROS comms lost!");
//xQueueSend(xScreenMsgQueue, &screenMsg, (TickType_t)0);
// Queue emgstop?
}
if (xQueueReceive(xMotorMsgQueue, &MotorMsg, xDelay25) == pdTRUE) {
if (MotorMsg.action != lastState) {
lastState = MotorMsg.action;
xJSONMessageType JSONMsg = {"motorState", {0}};
strcpy(JSONMsg.value, MotorRequestStrings[MotorMsg.action]);
xQueueSend(xJSONMessageQueue, &JSONMsg, xDelay25);
}
switch (MotorMsg.action) {
case MOTORREQ_IDLE:
wdt_disable();
GPIO_CLR_PIN(MOTOR_MOSFET);
break;
case MOTORREQ_ENABLE:
wdt_init();
// this need investigation... I tried a couple of different order of sequence:
// enable controller, brake on, brake off (signals inverted I know)
// This seem to work for all 3 controllers.
GPIO_SET_PIN(MOTOR_MOSFET);
vTaskDelay(xDelay200);
GPIO_CLR_PIN(MOTOR_BLADE_ENABLE);
vTaskDelay(xDelay50);
GPIO_CLR_PIN(MOTOR_BLADE_BRAKE);
vTaskDelay(xDelay50);
GPIO_SET_PIN(MOTOR_BLADE_BRAKE);
GPIO_CLR_PIN(MOTOR_LEFT_ENABLE);
GPIO_CLR_PIN(MOTOR_RIGHT_ENABLE);
vTaskDelay(xDelay50);
GPIO_CLR_PIN(MOTOR_LEFT_BRAKE);
GPIO_CLR_PIN(MOTOR_RIGHT_BRAKE);
vTaskDelay(xDelay50);
GPIO_SET_PIN(MOTOR_LEFT_BRAKE);
GPIO_SET_PIN(MOTOR_RIGHT_BRAKE);
break;
case MOTORREQ_DISABLE:
wdt_disable();
allStop();
GPIO_CLR_PIN(MOTOR_MOSFET);
GPIO_SET_PIN(MOTOR_BLADE_BRAKE);
GPIO_SET_PIN(MOTOR_LEFT_BRAKE);
GPIO_SET_PIN(MOTOR_RIGHT_BRAKE);
break;
case MOTORREQ_SETSPEED:
if (sensorMsg.blockForward) {
if (MotorMsg.pwm.left > 0) MotorMsg.pwm.left = 0;
if (MotorMsg.pwm.right > 0) MotorMsg.pwm.right = 0;
if (MotorMsg.pwm.left < 0 || MotorMsg.pwm.right < 0) sensorMsg.blockForward = 0;
}
//setpwm(MotorMsg.pwm.blade, MotorMsg.pwm.left, MotorMsg.pwm.right);
setPWMBlade = MotorMsg.pwm.blade;
setPWMLeft = MotorMsg.pwm.left;
setPWMRight = MotorMsg.pwm.right;
break;
case MOTORREQ_BRAKEON:
GPIO_CLR_PIN(MOTOR_BLADE_BRAKE);
GPIO_CLR_PIN(MOTOR_LEFT_BRAKE);
GPIO_CLR_PIN(MOTOR_RIGHT_BRAKE);
break;
case MOTORREQ_BRAKEOFF:
GPIO_SET_PIN(MOTOR_BLADE_BRAKE);
GPIO_SET_PIN(MOTOR_LEFT_BRAKE);
GPIO_SET_PIN(MOTOR_RIGHT_BRAKE);
break;
case MOTORREQ_EMGSTOP:
wdt_disable();
allStop();
GPIO_CLR_PIN(MOTOR_BLADE_BRAKE);
GPIO_CLR_PIN(MOTOR_LEFT_BRAKE);
GPIO_CLR_PIN(MOTOR_RIGHT_BRAKE);
sensorMsg.emergancyStop = 1;
vTaskDelay(xDelay500);
GPIO_CLR_PIN(MOTOR_MOSFET);
// Queue idle?
break;
case MOTORREQ_RESETEMG:
sensorMsg.emergancyStop = 0;
GPIO_CLR_PIN(MOTOR_MOSFET);
GPIO_SET_PIN(MOTOR_BLADE_BRAKE);
GPIO_SET_PIN(MOTOR_LEFT_BRAKE);
GPIO_SET_PIN(MOTOR_RIGHT_BRAKE);
break;
}
}
}
}
/*
case EMGSTOP:
setpwm(0, 0, 0);
xScreenMsgType screenMsg;
screenMsg.time=50;
sprintf(screenMsg.text, "EMERGENCY STOP!");
xQueueSend(xScreenMsgQueue, &screenMsg, (TickType_t)0);
vTaskDelay(xDelay250);
GPIO_CLR_PIN(MOTOR_MOSFET); //<- set state power down motor instead
break;
*/