-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwmServo.cpp
291 lines (233 loc) · 6.75 KB
/
pwmServo.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
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
/*
* Copyright (C) 2016 Tolga Ceylan
*
* CopyPolicy: Released under the terms of the GNU GPL v3.0.
* This file incorporates work covered by the following copyright and
* permission notice:
*/
/***************************************************
This is a library for our Adafruit 16-channel PWM & Servo driver
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/products/815
These displays use I2C to communicate, 2 pins are required to
interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include "pwmServo.h"
#include "logger.h"
#include "i2c.h"
#include "thread.h"
#include "utils.h"
#include <errno.h>
#include <math.h>
#include <assert.h>
namespace robo
{
#define PCA9685_SUBADR1 0x2
#define PCA9685_SUBADR2 0x3
#define PCA9685_SUBADR3 0x4
#define PCA9685_MODE1 0x0
#define PCA9685_PRESCALE 0xFE
#define LED0_ON_L 0x6
#define LED0_ON_H 0x7
#define LED0_OFF_L 0x8
#define LED0_OFF_H 0x9
#define ALLLED_ON_L 0xFA
#define ALLLED_ON_H 0xFB
#define ALLLED_OFF_L 0xFC
#define ALLLED_OFF_H 0xFD
// Static forwarders
void PWMServo::s_reset(void *arg, uint8_t *arg_blob)
{
PWMServo *me = static_cast<PWMServo *>(arg);
me->thr_reset();
}
void PWMServo::s_set_pwm_freq1(void *arg, uint8_t *arg_blob)
{
PWMServo *me = static_cast<PWMServo *>(arg);
me->thr_set_pwm_freq1();
}
void PWMServo::s_set_pwm_freq2(void *arg, uint8_t *arg_blob)
{
PWMServo *me = static_cast<PWMServo *>(arg);
me->thr_set_pwm_freq2();
}
void PWMServo::s_set_pwm(void *arg, uint8_t *arg_blob)
{
PWMServo *me = static_cast<PWMServo *>(arg);
me->thr_set_pwm(arg_blob);
}
// end of static forwarders
PWMServo::PWMServo(I2C *driver, Thread *thr)
:
m_address(0),
m_freq(0.0f),
m_driver(driver),
m_thread(thr),
m_initialized(false),
m_io_ready(false),
m_oldmode(0)
{
assert(driver);
assert(thr);
}
PWMServo::~PWMServo()
{
shutdown();
}
void PWMServo::shutdown()
{
logger(LOG_INFO, "PWMServo shutting down");
if (!m_thread || !m_thread->is_running() || !m_initialized)
return;
Thread::Cmd cmd;
cmd.fun = &PWMServo::s_reset;
cmd.arg = this;
int res = m_thread->queue_cmd(cmd);
if (!res)
res = m_thread->signal_io();
if (res)
logger(LOG_ERROR, "PWMServo shutdown queue_cmd error err=%d", res);
m_initialized = false;
}
int PWMServo::initialize(int address, float freq)
{
logger(LOG_INFO, "PWMServo initializing address=0x%x", address);
if (!m_thread->is_running() || m_initialized)
return EINVAL;
m_address = address;
m_freq = freq;
m_initialized = true;
Thread::Cmd cmd;
cmd.fun = &PWMServo::s_set_pwm_freq1;
cmd.arg = this;
int res = 0;
res = m_thread->queue_cmd(cmd);
if (!res)
res = m_thread->signal_io();
if (res)
logger(LOG_ERROR, "PWMServo initialize error address=0x%x freq=%f err=%d",
address, freq, res);
return res;
}
uint8_t PWMServo::thr_get_prescale() const
{
float prescale_val = 25000000;
prescale_val /= 4096;
prescale_val /= m_freq;
prescale_val -= 1;
const uint8_t prescale = floor(prescale_val + 0.5);
return prescale;
}
int PWMServo::execute()
{
if (!m_thread || !m_thread->is_running())
return EINVAL;
return m_thread->signal_io();
}
int PWMServo::update(uint64_t now)
{
if (m_start_pwm_freq2 && !m_io_ready)
{
static uint64_t prev = 0;
if (prev == 0)
prev = now;
if (Timer::get_elapsed_usec(prev, now) > 5000)
{
Thread::Cmd cmd;
cmd.fun = &PWMServo::s_set_pwm_freq2;
cmd.arg = this;
int res = m_thread->queue_cmd(cmd);
if (!res)
res = m_thread->signal_io();
prev = now; // this will schedule this again, after 5ms *if* thread fails.
return res;
}
}
return 0;
}
void PWMServo::thr_reset()
{
int res = 0;
const uint8_t data[2] = { PCA9685_MODE1, 0x0 };
m_io_ready = false;
res = m_driver->i2c_set_slave_address(m_address);
res = m_driver->i2c_write(res, data, 2);
if (res)
logger(LOG_ERROR, "PWMServo thr_reset error err=%d", res);
}
void PWMServo::thr_set_pwm_freq1()
{
uint8_t data[2];
int len = 0;
uint8_t newmode = 0;
int res = 0;
m_io_ready = false;
data[0] = PCA9685_MODE1;
data[1] = 0x0;
res = m_driver->i2c_set_slave_address(m_address);
res = m_driver->i2c_write(res, data, 2);
res = m_driver->i2c_read(res, data, 1, len);
if (!res && len != 1)
res = EINVAL;
m_oldmode = data[0];
newmode = (m_oldmode & 0x7F) | 0x10; // sleep
data[0] = PCA9685_MODE1;
data[1] = newmode;
res = m_driver->i2c_write(res, data, 2); // goto sleep
data[0] = PCA9685_PRESCALE;
data[1] = thr_get_prescale();
res = m_driver->i2c_write(res, data, 2); // set the prescaler
data[0] = PCA9685_MODE1;
data[1] = m_oldmode;
res = m_driver->i2c_write(res, data, 2); // goto sleep
if (!res)
m_start_pwm_freq2 = true;
else
logger(LOG_ERROR, "PWMServo thr_set_pwm_freq1 error err=%d", res);
}
void PWMServo::thr_set_pwm_freq2()
{
uint8_t data[2];
int res = 0;
m_start_pwm_freq2 = false;
// This sets the MODE1 register to turn on auto increment
data[0] = PCA9685_MODE1;
data[1] = m_oldmode | 0xa1;
res = m_driver->i2c_set_slave_address(m_address);
res = m_driver->i2c_write(res, data, 2);
if (!res)
m_io_ready = true;
else
logger(LOG_ERROR, "PWMServo thr_set_pwm_freq2 error err=%d", res);
}
int PWMServo::set_pwm(uint8_t num, uint16_t on, uint16_t off)
{
if (!m_thread || !m_thread->is_running() || !m_initialized || !m_io_ready)
return EINVAL;
Thread::Cmd cmd;
cmd.fun = &PWMServo::s_set_pwm;
cmd.arg = this;
cmd.arg_blob[0] = LED0_ON_L + 4 * num;
cmd.arg_blob[1] = on;
cmd.arg_blob[2] = on >> 8;
cmd.arg_blob[3] = off;
cmd.arg_blob[4] = off >> 8;
const int res = m_thread->queue_cmd(cmd);
if (res)
logger(LOG_ERROR, "PWMServo set_pwm error err=%d", res);
return res;
}
void PWMServo::thr_set_pwm(uint8_t *data)
{
int res = 0;
res = m_driver->i2c_set_slave_address(m_address);
res = m_driver->i2c_write(res, data, 5);
if (res)
logger(LOG_ERROR, "PWMServo thr_set_pwm error err=%d", res);
}
} // namespace robo