-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServo.cpp
executable file
·226 lines (181 loc) · 5.81 KB
/
Servo.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
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
/*
Servo.cpp - Interrupt driven Servo library for the CC3200 using 16 bit timers
Copyright (c) 2009 Michael Margolis. All right reserved.
2014: Modified by Robert Wessels for the CC3200
2021: bug fixes for MSP432
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "Servo.h"
#include <stdlib.h>
/** variables and functions common to all Servo instances **/
volatile unsigned long ticksPerMicrosecond; // Holds the calculated value
unsigned int servoAssignedMask;
static servo_t servos[SERVOS_PER_TIMER];
unsigned int remainderPulseWidth;
volatile int currentServo;
bool servoInitialized = false;
Timer_Params timerParams;
Timer_Handle timerHandle;
// Calculate the new period remainder
static void calculatePeriodRemainder(void)
{
unsigned long servoPeriodSum = 0;
for (int i = 0; i < SERVOS_PER_TIMER; i++){
servoPeriodSum += servos[i].pulse_width;
}
remainderPulseWidth = REFRESH_INTERVAL - servoPeriodSum;
}
void initServo(void) {
// Initialize global variables
ticksPerMicrosecond = 0;
servoAssignedMask = 0;
remainderPulseWidth = 0;
currentServo = 0;
for(int i = 0; i < SERVOS_PER_TIMER; i++)
{
servos[i].pin_number = 0;
servos[i].pulse_width = DEFAULT_SERVO_PULSE_WIDTH;
servos[i].enabled = false;
}
calculatePeriodRemainder();
Error_Block eb;
Error_init(&eb);
Timer_Params_init(&timerParams);
timerParams.period = 10;
//timerParams.clockSource = Timer_Source_SMCLK;
timerParams.runMode = Timer_RunMode_ONESHOT;
timerParams.periodType = Timer_PeriodType_MICROSECS;
timerHandle = Timer_create(Timer_ANY, ServoIntHandler, &timerParams, &eb);
}
/** end of static functions **/
/*
* When a new servo is created:
* Initialize the servo module if it has not been initialized already.
* Add the servo to the assigned servos mask with a new index.
*/
Servo::Servo()
{
this->index = INVALID_SERVO;
// Look for a free servo index.
for (int i = 0; i < SERVOS_PER_TIMER; i++)
{
if (((servoAssignedMask >> i) & 1) == 0)
{
// Save the index for this instance of Servo.
this->index = i;
// Mark the spot in the mask.
servoAssignedMask |= (1 << i);
// Stop searching for free slots.
break;
}
}
}
//! Write a pulse width of the given number of microseconds to the Servo's pin
void Servo::writeMicroseconds(int value)
{
if(value < this->min) value = this->min;
if(value > this->max) value = this->max;
servos[this->index].pulse_width = value;
calculatePeriodRemainder();
}
//! Write a pulse width of the given degrees (if in the appropriate range to be degrees)
//! or of the specified number of microseconds (if in the appropriate range to be microseconds)
void Servo::write(int value)
{
// treat values less than the min pulse width as angles in degrees (valid values in microseconds are handled as microseconds)
if(value < MIN_SERVO_PULSE_WIDTH)
{
if(value < 0) value = 0;
if(value > 180) value = 180;
value = map(value, 0, 180, this->min, this->max);
}
this->writeMicroseconds(value);
}
//! Returns the current pulse width of the Servo's signal, in microseconds
int Servo::readMicroseconds()
{
return servos[this->index].pulse_width;
}
//! Returns the current position of the Servo, in degrees
int Servo::read() // return the value as degrees
{
return map( this->readMicroseconds()+1, this->min, this->max, 0, 180);
}
//! Attach the Servo to the given pin (and, if specified, with the given range of legal pulse widths)
unsigned int Servo::attach(unsigned int pin, int min, int max)
{
// If the module has not been initialized
if(!servoInitialized)
{
// Initialize it.
initServo();
// It has been initialized, prevent further calls to initServo().
servoInitialized = true;
}
this->min = min;
this->max = max;
servos[this->index].pin_number = pin;
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
calculatePeriodRemainder();
servos[this->index].enabled = true;
return this->index;
}
//! Detach the Servo from its pin
void Servo::detach()
{
// Disable, clean up
servos[this->index].enabled = false;
servos[this->index].pulse_width = DEFAULT_SERVO_PULSE_WIDTH;
calculatePeriodRemainder();
digitalWrite(servos[this->index].pin_number, LOW);
}
//! ISR for generating the pulse widths
Void ServoIntHandler(UArg arg0)
{
//Timer_stop(timerHandle);
// Get the pulse width value for the current servo from the array
// and reload the timer with the new pulse width count value
// if we have already serviced all servos (currentServo = MAX_SERVO_NO)
// then this value should be the 20ms period value
if(currentServo < SERVOS_PER_TIMER)
{
Timer_setPeriodMicroSecs(timerHandle, servos[currentServo].pulse_width);
}
else
{
Timer_setPeriodMicroSecs(timerHandle, remainderPulseWidth);
}
// End the servo pulse set previously (if any)
if(currentServo > 0) // If not the 1st Servo....
{
if (servos[currentServo - 1].enabled)
{
digitalWrite(servos[currentServo - 1].pin_number, LOW);
}
}
// Set the current servo pin HIGH
if(currentServo < SERVOS_PER_TIMER)
{
if (servos[currentServo].enabled)
{
digitalWrite(servos[currentServo].pin_number, HIGH);
}
currentServo++; // Advance to next servo for processing next time
}
else
{
currentServo = 0; // Start all over again
}
Timer_start(timerHandle);
}