-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathDualMC33926MotorShield.cpp
158 lines (141 loc) · 3.88 KB
/
DualMC33926MotorShield.cpp
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
#include "DualMC33926MotorShield.h"
// Constructors ////////////////////////////////////////////////////////////////
DualMC33926MotorShield::DualMC33926MotorShield()
{
//Pin map
_nD2 = 4;
_M1DIR = 7;
_M1PWM = 9;
_M2DIR = 8;
_M2PWM = 10;
_nSF = 12;
_M1FB = A0;
_M2FB = A1;
}
DualMC33926MotorShield::DualMC33926MotorShield(unsigned char M1DIR,
unsigned char M1PWM,
unsigned char M1FB,
unsigned char M2DIR,
unsigned char M2PWM,
unsigned char M2FB,
unsigned char nD2,
unsigned char nSF)
{
_nD2 = nD2;
_M1DIR = M1DIR;
_M1PWM = M1PWM;
_M2DIR = M2DIR;
_M2PWM = M2PWM;
_nSF = nSF;
_M1FB = M1FB;
_M2FB = M2FB;
}
// Public Methods //////////////////////////////////////////////////////////////
void DualMC33926MotorShield::init()
{
// Define pinMode for the pins and set the frequency for timer1.
pinMode(_M1DIR,OUTPUT);
pinMode(_M1PWM,OUTPUT);
pinMode(_M1FB,INPUT);
pinMode(_M2DIR,OUTPUT);
pinMode(_M2PWM,OUTPUT);
pinMode(_M2FB,INPUT);
pinMode(_nD2,OUTPUT);
digitalWrite(_nD2,HIGH); // default to on
pinMode(_nSF,INPUT);
#ifdef DUALMC33926MOTORSHIELD_TIMER1_AVAILABLE
if (_M1PWM == _M1PWM_TIMER1_PIN && _M2PWM == _M2PWM_TIMER1_PIN)
{
// Timer 1 configuration
// prescaler: clockI/O / 1
// outputs enabled
// phase-correct PWM
// top of 400
//
// PWM frequency calculation
// 16MHz / 1 (prescaler) / 2 (phase-correct) / 400 (top) = 20kHz
TCCR1A = 0b10100000;
TCCR1B = 0b00010001;
ICR1 = 400;
}
#endif
}
// Set speed for motor 1, speed is a number betwenn -400 and 400
void DualMC33926MotorShield::setM1Speed(int speed)
{
unsigned char reverse = 0;
if (speed < 0)
{
speed = -speed; // Make speed a positive quantity
reverse = 1; // Preserve the direction
}
if (speed > 400) // Max PWM dutycycle
speed = 400;
#ifdef DUALMC33926MOTORSHIELD_TIMER1_AVAILABLE
if (_M1PWM == _M1PWM_TIMER1_PIN && _M2PWM == _M2PWM_TIMER1_PIN)
{
OCR1A = speed;
}
else
{
analogWrite(_M1PWM,speed * 51 / 80); // map 400 to 255
}
#else
analogWrite(_M1PWM,speed * 51 / 80); // map 400 to 255
#endif
if (reverse)
digitalWrite(_M1DIR,HIGH);
else
digitalWrite(_M1DIR,LOW);
}
// Set speed for motor 2, speed is a number betwenn -400 and 400
void DualMC33926MotorShield::setM2Speed(int speed)
{
unsigned char reverse = 0;
if (speed < 0)
{
speed = -speed; // Make speed a positive quantity
reverse = 1; // Preserve the direction
}
if (speed > 400) // Max PWM dutycycle
speed = 400;
#ifdef DUALMC33926MOTORSHIELD_TIMER1_AVAILABLE
if (_M1PWM == _M1PWM_TIMER1_PIN && _M2PWM == _M2PWM_TIMER1_PIN)
{
OCR1B = speed;
}
else
{
analogWrite(_M2PWM,speed * 51 / 80); // map 400 to 255
}
#else
analogWrite(_M2PWM,speed * 51 / 80); // map 400 to 255
#endif
if (reverse)
digitalWrite(_M2DIR,HIGH);
else
digitalWrite(_M2DIR,LOW);
}
// Set speed for motor 1 and 2
void DualMC33926MotorShield::setSpeeds(int m1Speed, int m2Speed)
{
setM1Speed(m1Speed);
setM2Speed(m2Speed);
}
// Return motor 1 current value in milliamps.
unsigned int DualMC33926MotorShield::getM1CurrentMilliamps()
{
// 5V / 1024 ADC counts / 525 mV per A = 9 mA per count
return analogRead(_M1FB) * 9;
}
// Return motor 2 current value in milliamps.
unsigned int DualMC33926MotorShield::getM2CurrentMilliamps()
{
// 5V / 1024 ADC counts / 525 mV per A = 9 mA per count
return analogRead(_M2FB) * 9;
}
// Return error status
unsigned char DualMC33926MotorShield::getFault()
{
return !digitalRead(_nSF);
}