forked from srnilssen/Arduino-Robot-2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotor.c
308 lines (269 loc) · 10.5 KB
/
motor.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/************************************************************************/
// File: motor.c
// Author: Erlend Ese, NTNU Spring 2016
// Purpose: Register ticks from optical encoders mounted to
// cogwheels
//
// Note: Motor is active low
// ISR routines in main.c
/************************************************************************/
/* AVR INCLUDES */
#include <avr/io.h>
#include <avr/interrupt.h>
/* Custom includes */
#include "defines.h"
#include "motor.h"
#include "LED.h"
/* Function vMotorEncoderTickFromISR() uses global variables */
//extern volatile int16_t gLeftWheelTicks;
//extern volatile int16_t gRightWheelTicks;
/* Functions vMotorMove...() uses global variables */
//extern uint8_t gLeftWheelDirection;
//extern uint8_t gRightWheelDirection;
/************************************************************************/
// Initialize the motor encoder and control pins
// Trigger on rising edge
// Input: pins PD2 and PD3 (INT0 and INT1)
// Note: ISR Routines are in main.c
/************************************************************************/
void vMotor_init(){
/* Initialize motor pins as output */
DDRB |= (1<<motorLeftOn);
DDRB |= (1<<motorLeftBackward) | (1<<motorLeftForward);
DDRB |= (1<<motorRightOn);
DDRH |= (1<<motorRightForward) | (1<<motorRightBackward);
/* Set motor high and direction low to ensure zero movement */
PORTB &= ~(1<<motorLeftOn);
PORTB &= ~(1<<motorRightOn);
PORTH &= ~(1<<motorRightBackward) & ~(1<<motorRightForward);
PORTB &= ~(1<<motorLeftForward) & ~(1<<motorLeftBackward);
/* Initialize motor encoder pins as input */
DDRD &= ~((1<<encoderPinRight) & (1<<encoderPinLeft));
/* Enable pull up for encoder inputs */
PORTD |= (1<<encoderPinLeft) | (1<<encoderPinRight);
/* nRF dongle pin 19 as input pin */
DDRE &= ~(1<<nRF19); // dongle pin input
/* Clear interrupt enable bits to ensure no interrupts occur */
EIMSK &= ~((1<<INT2) & (1<<INT3) & (1<<INT4));
/* Set interrupt to trigger on rising edge for motor and any change for dongle */
/* Datasheet p110-11 table 15-1,3 */
EICRA |= (1<<ISC21) | (1<<ISC20) | (1<<ISC31) | (1<<ISC30); //MÅ KANKJSE SJEKKE UT DENNE NØYERE
EICRB |= (1<<ISC40);
/* Clear interrupt flag for INT2, 3 and 4 */
EIFR = (1<<INTF2) | (1<<INTF3) | (1<<INTF4);
/* Enable interrupts for INT2, 3 and 4 */
EIMSK |= (1<<INT2) | (1<<INT3) | (1<<INT4);
/* Set up PWM for left motor connected to OC0A (8bit PWM) and right motor */
/* connected to OC1B (16bit PWM)
/* Clear OCnA/OCnB on Compare Match, set */
/* OCnA/OCnB at BOTTOM (non-inverting mode) */
/* Datasheet p.132 Table 14-3 */
TCCR0A |= (1<<COM0A1) | (0<<COM0A0);
TCCR1A |= (1<<COM1B1) | (0<<COM1B1);
/* Waveform generation mode 3: Fast 8bit PWM */
/* Top 0x00FF, Update bottom, flag set on top */
/* Datasheet p.133 Table 14-5 */
TCCR0B |= (0<<WGM02);
TCCR0A |= (1<<WGM01) | (1<<WGM00);
TCCR1B |= (0<<WGM13) | (1<<WGM12);
TCCR1A |= (0<<WGM11) | (1<<WGM10);
/* Clock select bit description: */
/* clkI/O/8 (From prescaler) - Datasheet p.157 Table 17-6*/
TCCR0B |= (0<<CS02) | (1<<CS01) | (0<<CS00);
TCCR1B |= (0<<CS12) | (1<<CS11) | (0<<CS10);
/* Set other motorpins to normal operation mode (connected to PWM ports) */
/* Datasheet p.155 Table 17-3 */
TCCR1A |= (0<<COM1A1) | (0<<COM1A0); // MC_IN1 left backwards connected to OC1A
TCCR2A |= (0<<COM2A1) | (0<<COM2A0); // MC_IN1 left backwards connected to OC2A
TCCR2A |= (0<<COM2B1) | (0<<COM2B0); // MC_IN1 left backwards connected to OC2B
TCCR4A |= (0<<COM4C1) | (0<<COM4C0); // MC_IN1 left backwards connected to OC4C
}
void vMotorMoveLeftForward(uint8_t actuation, uint8_t *leftWheelDirection){
motorLeftPWM = actuation;
PORTB |= (1<<motorLeftForward);
PORTB &= ~(1<<motorLeftBackward);
*leftWheelDirection = motorLeftForward;
}
void vMotorMoveRightForward(uint8_t actuation, uint8_t *rightWheelDirection){
motorRightPWM = actuation;
PORTH |= (1<<motorRightForward);
PORTH &= ~(1<<motorRightBackward);
*rightWheelDirection = motorRightForward;
}
void vMotorMoveLeftBackward(uint8_t actuation, uint8_t *leftWheelDirection){
motorLeftPWM = actuation;
PORTB &= ~(1<<motorLeftForward);
PORTB |= (1<<motorLeftBackward);
*leftWheelDirection = motorLeftBackward;
}
void vMotorMoveRightBackward(uint8_t actuation, uint8_t *rightWheelDirection){
motorRightPWM = actuation;
PORTH &= ~(1<<motorRightForward);
PORTH |= (1<<motorRightBackward);
*rightWheelDirection = motorRightBackward;
}
void vMotorBrakeLeft(){
motorLeftPWM = 0;
//motorLeftPWM = 255; MIDLERTIDIG ENDRING: MÅ KANSKJE FIKSE DENNE IGJEN NÅR ROBOTEN FUNGERER. NÅ BREMSER DEN IKKE
//PORTE |= (1<<motorLeftOn);
PORTB &= ~(1<<motorLeftForward);
PORTB &= ~(1<<motorLeftBackward);
/* Do not set any direction to motor here, it can overshoot */
}
void vMotorGlideLeft(){
motorLeftPWM = 0;
//PORTE &= ~(1<<motorLeftOn);
PORTB &= ~(1<<motorLeftForward);
PORTB &= ~(1<<motorLeftBackward);
}
void vMotorBrakeRight(){
motorRightPWM = 0;
//motorRightPWM = 255; MIDLERTIDIG ENDRING:MÅ KANSKJE FIKSE DENNE IGJEN NÅR ROBOTEN FUNGERER. NÅ BREMSER DEN IKKE
//PORTH |= (1<<motorRightOn);
PORTH &= ~(1<<motorRightForward);
PORTH &= ~(1<<motorRightBackward);
/* Do not set any direction to motor here, it can overshoot */
}
void vMotorGlideRight(){
motorRightPWM = 0;
//PORTH &= ~(1<<motorRightOn);
PORTH &= ~(1<<motorRightForward);
PORTH &= ~(1<<motorRightBackward);
}
/* Switch for robot movement to abstract the logic away from main */
void vMotorMovementSwitch(int16_t leftSpeed, int16_t rightSpeed, uint8_t *leftWheelDirection, uint8_t *rightWheelDirection){
if (leftSpeed > 0){
vMotorMoveLeftForward(leftSpeed, leftWheelDirection);
}else if(leftSpeed < 0){
vMotorMoveLeftBackward(-leftSpeed,leftWheelDirection);
}else{
vMotorBrakeLeft();
}
if (rightSpeed > 0) {
vMotorMoveRightForward(rightSpeed,rightWheelDirection);
}else if (rightSpeed < 0) {
vMotorMoveRightBackward(-rightSpeed,rightWheelDirection);
}else {
vMotorBrakeRight();
}
/*
switch (movement){
case moveForward:{
if (tmp_leftWheelTicks > tmp_rightWheelTicks){
vMotorBrakeLeft();
vMotorMoveRightForward(rightOutput, rightWheelDirection);
}
else if (tmp_rightWheelTicks > tmp_leftWheelTicks){
vMotorBrakeRight();
vMotorMoveLeftForward(leftOutput, leftWheelDirection);
}
else{
vMotorMoveLeftForward(leftOutput, leftWheelDirection);
vMotorMoveRightForward(rightOutput, rightWheelDirection);
}
break;
}
case moveBackward:{
if (tmp_leftWheelTicks < tmp_rightWheelTicks){
vMotorBrakeLeft();
vMotorMoveRightBackward(rightOutput, rightWheelDirection);
}
else if (tmp_rightWheelTicks < tmp_leftWheelTicks){
vMotorBrakeRight();
vMotorMoveLeftBackward(leftOutput, leftWheelDirection);
}
else{
vMotorMoveLeftBackward(leftOutput, leftWheelDirection);
vMotorMoveRightBackward(rightOutput, rightWheelDirection);
}
break;
}
case moveClockwise:{
if(-tmp_rightWheelTicks > tmp_leftWheelTicks){
vMotorBrakeRight();
vMotorMoveLeftForward(leftOutput, leftWheelDirection);
}
else if (tmp_leftWheelTicks > -tmp_rightWheelTicks){
vMotorBrakeLeft();
vMotorMoveRightBackward(rightOutput, rightWheelDirection);
}
else{
vMotorMoveLeftForward(leftOutput, leftWheelDirection);
vMotorMoveRightBackward(rightOutput, rightWheelDirection);
}
break;
}
case moveCounterClockwise:{
if(-tmp_leftWheelTicks > tmp_rightWheelTicks){
vMotorBrakeLeft();
vMotorMoveRightForward(rightOutput, rightWheelDirection);
}
else if (tmp_rightWheelTicks > -tmp_leftWheelTicks){
vMotorBrakeRight();
vMotorMoveLeftBackward(leftOutput, leftWheelDirection);
}
else {
vMotorMoveLeftBackward(leftOutput, leftWheelDirection);
vMotorMoveRightForward(rightOutput, rightWheelDirection);
}
break;
}
case moveRightForward:{
vMotorMoveRightForward(rightOutput, rightWheelDirection);
vMotorBrakeLeft();
break;
}
case moveLeftForward:{
vMotorMoveLeftForward(leftOutput, leftWheelDirection);
vMotorBrakeRight();
break;
}
case moveRightBackward:{
vMotorMoveRightBackward(rightOutput, rightWheelDirection);
vMotorBrakeLeft();
break;
}
case moveLeftBackward:{
vMotorMoveLeftBackward(leftOutput, leftWheelDirection);
vMotorBrakeRight();
break;
}
default:{
vMotorBrakeLeft();
vMotorBrakeRight();
break;
}
}
*/
}
/* Handle ISR ticks from encoder, Please note that we are losing accuracy here due to division */
void vMotorEncoderLeftTickFromISR(uint8_t wheelDirection, int16_t *leftWheelTicks, uint8_t leftEncoderTicks){
switch (wheelDirection){
case motorLeftForward:{
*leftWheelTicks += leftEncoderTicks / 2;
break;
}
case motorLeftBackward:{
*leftWheelTicks -= leftEncoderTicks / 2;
break;
}
default:
// We have a count when the robot is supposedly not moving.
break;
}
}
void vMotorEncoderRightTickFromISR(uint8_t wheelDirection, int16_t *rightWheelTicks, uint8_t rightEncoderTicks){
switch (wheelDirection){
case motorRightForward:{
*rightWheelTicks += rightEncoderTicks / 2;
break;
}
case motorRightBackward:{
*rightWheelTicks -= rightEncoderTicks / 2;
break;
}
default:
// We have a count when the robot is supposedly not moving.
break;
}
}