wpi-32u4-library
servo32u4.cpp
Go to the documentation of this file.
1#include <servo32u4.h>
2
3uint16_t Servo32U4Base::setMinMaxMicroseconds(uint16_t min, uint16_t max)
4{
5 // swap if in the wrong place
6 if(min > max) {uint16_t temp = min; min = max; max = temp;}
7
8 usMin = min;
9 usMax = max;
10
11 return usMax - usMin; //return the range, in case the user wants to do a sanity check
12}
13
15{
16 pinMode(5, OUTPUT); // set pin as OUTPUT
17
18 cli();
19
20 // clear then set the OCR3A bits (pin 5)
21 TCCR3A = 0x82; //WGM
22 TCCR3B = 0x1A; //WGM + CS = 8
23 ICR3 = 39999; //20ms
24 OCR3A = 3000;
25
26 sei();
27
28 isAttached = true;
29}
30
32{
33 cli();
34
35 // clear the OCR3A bits
36 TCCR3A &= 0x7f; //cancel OCR3A
37 sei();
38
39 isAttached = false;
40}
41
42void Servo32U4Pin5::writeMicroseconds(uint16_t microseconds)
43{
44 if (!isAttached)
45 {
46 attach();
47 }
48
49 microseconds = constrain(microseconds, usMin, usMax);
50
51 //prescaler is 8, so 1 timer count = 0.5 us
52 OCR3A = (microseconds << 1) - 1; // multiplies by 2
53}
54
56{
57 pinMode(6, OUTPUT); // set pin as OUTPUT
58
59 cli();
60
61 // Be careful here, since Timer4 is used to manage speed controller. See Chassis::init()
62 TCCR4C |= 0x05;
63
64 sei();
65
66 isAttached = true;
67}
68
70{
71 cli();
72
73 // Be careful here, since Timer4 is used to manage speed controller. See Chassis::init()
74 TCCR4C = 0x00;
75
76 sei();
77
78 isAttached = false;
79}
80
81// Resolution is 64 us; not great, but shouldn't be too constraining
82void Servo32U4Pin6::writeMicroseconds(uint16_t microseconds)
83{
84 if (!isAttached)
85 {
86 attach();
87 }
88
89 microseconds = constrain(microseconds, usMin, usMax);
90
91 //prescaler is 8, so 1 timer count = 64 us
92 OCR4D = (microseconds >> 6) - 1; // divides by 64
93}
94
96{
97 pinMode(13, OUTPUT); // set pin as OUTPUT
98
99 cli();
100
101 // Be careful here, since Timer4 is used to manage speed controller. See Chassis::init()
102 TCCR4A = 0x82;
103
104 sei();
105
106 isAttached = true;
107}
108
110{
111 cli();
112
113 // Be careful here, since Timer4 is used to manage speed controller. See Chassis::init()
114 TCCR4A = 0x00;
115
116 sei();
117
118 isAttached = false;
119}
120
121// Resolution is 64 us; not great, but shouldn't be too constraining
122void Servo32U4Pin13::writeMicroseconds(uint16_t microseconds)
123{
124 if (!isAttached)
125 {
126 attach();
127 }
128
129 microseconds = constrain(microseconds, usMin, usMax);
130
131 //prescaler is 8, so 1 timer count = 64 us
132 OCR4A = (microseconds >> 6) - 1; // divides by 64
133}
134
136{
137 pinMode(12, OUTPUT); // set pin as OUTPUT
138
139 cli();
140
141 // Be careful here, since Timer4 is used to manage speed controller. See Chassis::init()
142 TCCR4C |= 0x05;
143
144 sei();
145
146 isAttached = true;
147}
148
150{
151 cli();
152
153 // Be careful here, since Timer4 is used to manage speed controller. See Chassis::init()
154 TCCR4C = 0x00;
155
156 sei();
157
158 isAttached = false;
159}
160
161// Resolution is 64 us; not great, but shouldn't be too constraining
162void Servo32U4Pin12::writeMicroseconds(uint16_t microseconds)
163{
164 if (!isAttached)
165 {
166 attach();
167 }
168
169 microseconds = constrain(microseconds, usMin, usMax);
170
171 //prescaler is 8, so 1 timer count = 64 us
172 OCR4D = 250 - (microseconds >> 6) - 1; // divides by 64
173}
uint16_t usMin
Definition: servo32u4.h:42
uint16_t setMinMaxMicroseconds(uint16_t min, uint16_t max)
Definition: servo32u4.cpp:3
uint16_t usMax
Definition: servo32u4.h:43
bool isAttached
Definition: servo32u4.h:46
void attach(void)
Definition: servo32u4.cpp:135
void writeMicroseconds(uint16_t microseconds)
Definition: servo32u4.cpp:162
void detach(void)
Definition: servo32u4.cpp:149
void writeMicroseconds(uint16_t microseconds)
Definition: servo32u4.cpp:122
void detach(void)
Definition: servo32u4.cpp:109
void attach(void)
Definition: servo32u4.cpp:95
void attach(void)
Definition: servo32u4.cpp:14
void writeMicroseconds(uint16_t microseconds)
Definition: servo32u4.cpp:42
void detach(void)
Definition: servo32u4.cpp:31
void detach(void)
Definition: servo32u4.cpp:69
void attach(void)
Definition: servo32u4.cpp:55
void writeMicroseconds(uint16_t microseconds)
Definition: servo32u4.cpp:82