-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotor.c
More file actions
287 lines (253 loc) · 8.28 KB
/
motor.c
File metadata and controls
287 lines (253 loc) · 8.28 KB
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
281
282
283
284
285
286
287
/*
* motor.c
*
* Created on: Mar 18, 2014
* Author: Anthony Merlino and Brad Ebinger
*/
#include <stdint.h>
#include <stdbool.h>
#include <inc/hw_memmap.h>
#include <inc/hw_gpio.h>
#include <inc/hw_types.h>
#include <driverlib/gpio.h>
#include <driverlib/pin_map.h>
#include <driverlib/pwm.h>
#include <driverlib/sysctl.h>
#include "system.h"
#include "subsys.h"
#include "motor.h"
static inline calc_cycles(uint32_t us) {return (PWM_TIMER_FREQ/1000000.0)*us;}
static inline calc_usec(uint32_t cycles) {return (1000000.0/PWM_TIMER_FREQ)*cycles;}
version_t MOTOR_VERSION;
motor_state_t motors[4];
// Callback function definition
void MotorLogCallback(char * cmd);
void MotorsInit(void) {
uint8_t pin_mask;
// Allow register access to PWM1
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);
// Allow register access to GPIO
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
// Configure PF0, PF1, PF2, and PF3 as PWM outputs
GPIOPinConfigure(GPIO_PF1_M1PWM5);
GPIOPinConfigure(GPIO_PF2_M1PWM6);
GPIOPinConfigure(GPIO_PF3_M1PWM7);
GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
/* PF0 requires unlocking before configuration */
HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTF_BASE + GPIO_O_CR) |= GPIO_PIN_0;
GPIOPinConfigure(GPIO_PF0_M1PWM4);
GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_0);
HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_M;
// Configure the direction and standby pins as outputs
GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_1 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_7);
GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_2 | GPIO_PIN_3);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_4);
// Set the clock to be
SysCtlPWMClockSet(SYSCTL_PWMDIV_64);
// Setup Front Left Motor
PWMGenConfigure(PWM1_BASE, PWM_GEN_3, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3, calc_cycles(MOTOR_PERIOD));
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6, 0);
PWMGenEnable(PWM1_BASE, PWM_GEN_3);
pin_mask = 1 << (0x0000000F & PWM_OUT_6);
PWMOutputState(PWM1_BASE, pin_mask, 0);
// Setup Front Right Motor
PWMGenConfigure(PWM1_BASE, PWM_GEN_3, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3, calc_cycles(MOTOR_PERIOD));
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7, 0);
PWMGenEnable(PWM1_BASE, PWM_GEN_3);
pin_mask = 1 << (0x0000000F & PWM_OUT_7);
PWMOutputState(PWM1_BASE, pin_mask, 0);
// Setup Back Left Motor
PWMGenConfigure(PWM1_BASE, PWM_GEN_2, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2, calc_cycles(MOTOR_PERIOD));
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_4, 0);
PWMGenEnable(PWM1_BASE, PWM_GEN_2);
pin_mask = 1 << (0x0000000F & PWM_OUT_4);
PWMOutputState(PWM1_BASE, pin_mask, 0);
// Setup Back Right Motor
PWMGenConfigure(PWM1_BASE, PWM_GEN_2, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2, calc_cycles(MOTOR_PERIOD));
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5, 0);
PWMGenEnable(PWM1_BASE, PWM_GEN_2);
pin_mask = 1 << (0x0000000F & PWM_OUT_5);
PWMOutputState(PWM1_BASE, pin_mask, 0);
// Add debugging option
MOTOR_VERSION.word = 0x14110800LU;
SubsystemInit(MOTOR, MESSAGE, "MOTOR", MOTOR_VERSION);
RegisterCallback(MOTOR, MotorLogCallback);
}
void MotorsEnableFront(){
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_1, ON);
}
void MotorsEnableBack(){
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_5, ON);
}
void MotorsDisableFront(){
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_1, OFF);
}
void MotorsDisableBack(){
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_5, OFF);
}
void MotorLogCallback(char * cmd) {
// LogMsg(MOTOR, MESSAGE, "CMD Received: %s", cmd);
switch(*cmd) {
case 'y':
MotorsEnableFront();
MotorsEnableBack();
break;
case 'n':
MotorsDisableFront();
MotorsDisableBack();
break;
case '1':
MotorsDisableFront();
MotorsDisableBack();
break;
case '2':
MotorsDisableFront();
MotorsDisableBack();
break;
case '3':
MotorsDisableFront();
MotorsDisableBack();
break;
}
}
void MotorsUpdate(){
uint8_t pin_mask;
uint32_t cycles;
// Update Front Left Motor
switch(motors[FRONTLEFT_MOTOR].direction){
case CW:
// Set the pins so that the direction is Clockwise
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, ON);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_7, OFF);
break;
case CCW:
// Set the pins so that the direction is Counter-Clockwise
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, OFF);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_7, ON);
break;
case BRAKE:
// Set the pins so that we brake
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, ON);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_7, ON);
break;
case STOP:
// Set the pins so we stop
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, OFF);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_7, OFF);
break;
}
cycles = calc_cycles(MOTOR_PERIOD*motors[FRONTLEFT_MOTOR].duty_tenths_perc/1000);
pin_mask = 1 << (0x0000000F & PWM_OUT_6);
if(cycles <= 0){
PWMOutputState(PWM1_BASE, pin_mask, 0);
}
else{
PWMOutputState(PWM1_BASE, pin_mask, 1);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6, cycles);
}
// Update Front Right Motor
switch(motors[FRONTRIGHT_MOTOR].direction){
case CW:
// Set the pins so that the direction is Clockwise
GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, ON);
GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_2, OFF);
break;
case CCW:
// Set the pins so that the direction is Counter-Clockwise
GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, OFF);
GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_2, ON);
break;
case BRAKE:
// Set the pins so that we brake
GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, ON);
GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_2, ON);
break;
case STOP:
// Set the pins so we stop
GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, OFF);
GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_2, OFF);
break;
}
cycles = calc_cycles(MOTOR_PERIOD*motors[FRONTRIGHT_MOTOR].duty_tenths_perc/1000);
pin_mask = 1 << (0x0000000F & PWM_OUT_7);
if(cycles <= 0){
PWMOutputState(PWM1_BASE, pin_mask, 0);
}
else{
PWMOutputState(PWM1_BASE, pin_mask, 1);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7, cycles);
}
// Update Back Left Motor
switch(motors[BACKLEFT_MOTOR].direction){
case CW:
// Set the pins so that the direction is Clockwise
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4, ON);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, OFF);
break;
case CCW:
// Set the pins so that the direction is Counter-Clockwise
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4, OFF);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, ON);
break;
case BRAKE:
// Set the pins so that we brake
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4, ON);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, ON);
break;
case STOP:
// Set the pins so we stop
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4, OFF);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_3, OFF);
break;
}
cycles = calc_cycles(MOTOR_PERIOD*motors[BACKLEFT_MOTOR].duty_tenths_perc/1000);
pin_mask = 1 << (0x0000000F & PWM_OUT_4);
if(cycles <= 0){
PWMOutputState(PWM1_BASE, pin_mask, 0);
}
else{
PWMOutputState(PWM1_BASE, pin_mask, 1);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_4, cycles);
}
// Update Back Right Motor
switch(motors[BACKRIGHT_MOTOR].direction){
case CW:
// Set the pins so that the direction is Clockwise
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, ON);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_7, OFF);
break;
case CCW:
// Set the pins so that the direction is Counter-Clockwise
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, OFF);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_7, ON);
break;
case BRAKE:
// Set the pins so that we brake
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, ON);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_7, ON);
break;
case STOP:
// Set the pins so we stop
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, OFF);
GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_7, OFF);
break;
}
cycles = calc_cycles(MOTOR_PERIOD*motors[BACKRIGHT_MOTOR].duty_tenths_perc/1000);
pin_mask = 1 << (0x0000000F & PWM_OUT_5);
if(cycles <= 0){
PWMOutputState(PWM1_BASE, pin_mask, 0);
}
else{
PWMOutputState(PWM1_BASE, pin_mask, 1);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5, cycles);
}
}