forked from QUT-EESS/libRobotDev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRDMotor.h
131 lines (113 loc) · 2.91 KB
/
RDMotor.h
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
/*
* libRobotDev
* RDMotor.h
* Purpose: Abstracts all motor control functions.
* Created: 25/07/2014
* Author(s): Jerry Luck, Jeremy Pearson, Shaun Karran
* Status: UNTESTED
*/
#include <avr/io.h>
#include <stdint.h>
#include "RDPinDefs.h"
#include "RDUtil.h"
#ifndef RDMOTOR_H_
/**
* Robot Development Motor Header.
*/
#define RDMOTOR_H_
/**
* Motor1 Timer/Counter Output Compare Register A.
*/
#define M1_OCRA OCR1A
/**
* Motor1 Timer/Counter Output Compare Register B.
*/
#define M1_OCRB OCR1B
/**
* Motor2 Timer/Counter Output Compare Register A.
*/
#define M2_OCRA OCR3A
/**
* Motor2 Timer/Counter Output Compare Register B.
*/
#define M2_OCRB OCR3B
/**
* Initialises Timer1 and Timer3.
*/
void RDTimerInit(void) {
set_bit(TCCR1A, COM1A1); // Compare Match A: Clear on match, Set at TOP.
set_bit(TCCR1A, COM1B1); // Compare Match B: Clear on match, Set at TOP.
set_bit(TCCR1B, WGM13); set_bit(TCCR1B, WGM12); set_bit(TCCR1A, WGM11); // Set 16bit fast PWM. TOP at ICR.
set_bit(TCCR1B, CS11); set_bit(TCCR1B, CS10); // /64 prescaler.
ICR1 = 0x0FFF; // Set TOP value for 12bit resolution.
TCNT1 = 0x00; // Set counter to 0.
set_bit(TCCR3A, COM3A1); // Compare Match A: Clear on match, Set at TOP.
set_bit(TCCR3A, COM3B1); // Compare Match B: Clear on match, Set at TOP.
set_bit(TCCR3B, WGM33); set_bit(TCCR3B, WGM32); set_bit(TCCR3A, WGM31); // Set 16bit fast PWM. TOP at ICR.
set_bit(TCCR3B, CS31); set_bit(TCCR3B, CS30); // /64 prescaler.
ICR3 = 0x0FFF; // Set TOP value for 12bit resolution.
TCNT3 = 0x00; // Set counter to 0.
}
/**
* Converts a double from range 0-100 to a int from 0-4095.
*
* @param percent
* A value from 0-100.
*
* @return
* An equivalent value on a scale of 0-4095.
*/
uint16_t RDDutyCycle(double percent) {
return (uint16_t)((percent * 4095) / 100);
}
/**
* Initialise motors.
*/
void RDMotorInit(void) {
RDTimerInit();
// Motor pins to output.
set_bit(DDRB, MCTRL_IN1);
set_bit(DDRB, MCTRL_IN2);
set_bit(DDRC, MCTRL_IN3);
set_bit(DDRC, MCTRL_IN4);
// All pins to HIGH to brake motors.
M1_OCRA = RDDutyCycle(100);
M1_OCRB = RDDutyCycle(100);
M2_OCRA = RDDutyCycle(100);
M2_OCRB = RDDutyCycle(100);
}
/**
* Sets Motor1 speed.
*
* @param speed
* The speed of the motor as percentage. Negative values for reverse.
*/
void RDSetM1Speed(double speed) {
M1_OCRA = (speed < 0) ? 0:RDDutyCycle(speed);
M1_OCRB = (speed < 0) ? RDDutyCycle(-speed):0;
}
/**
* Sets Motor2 speed.
*
* @param speed
* The speed of the motor as percentage. Negative values for reverse.
*/
void RDSetM2Speed(double speed) {
M2_OCRA = (speed < 0) ? 0:RDDutyCycle(speed);
M2_OCRB = (speed < 0) ? RDDutyCycle(-speed):0;
}
/*
* Sets Motor1 to brake.
*/
void RDSetM1Brake(void) {
M1_OCRA = RDDutyCycle(100);
M1_OCRB = RDDutyCycle(100);
}
/*
* Sets Motor2 to brake.
*/
void RDSetM2Brake(void) {
M2_OCRA = RDDutyCycle(100);
M2_OCRB = RDDutyCycle(100);
}
#endif //RDMOTOR_H_