-
Notifications
You must be signed in to change notification settings - Fork 0
/
car.cpp
243 lines (188 loc) · 5.1 KB
/
car.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
/*
* Copyright (C) 2016 Tolga Ceylan
*
* CopyPolicy: Released under the terms of the GNU GPL v3.0.
*/
#include "car.h"
#include "logger.h"
#include "timer.h"
#include "sensor_state.h"
#include "utils.h"
#include <errno.h>
#include <math.h>
#include <assert.h>
// diameter of the rotary encoder shaft is 2.0 cm.
// with 1024 ticks for each rotation
#define WHEEL_CIRC (2.0 * (M_PIl))
#define ENCODER_SCALER (1024.0l / (WHEEL_CIRC) * 2.0l)
#define PID_KP (0.45f)
#define PID_KI (0.0f)
#define PID_KD (9.0f)
#define SPEED_STEP (25)
namespace robo
{
int Car::convert_to_speed(const Pid &pid)
{
const int speed = linear_map(
static_cast<long double>(pid.output),
-1024.0l, 1024.0l,
-255.0l, 255.0l
);
return speed;
}
Car::Car(MotorDriver *driver, SensorState *sensors)
:
m_driver(driver),
m_sensors(sensors),
m_left(driver),
m_right(driver),
m_leftSpeed(0),
m_rightSpeed(0),
m_previous(0),
m_period(1)
{
assert(driver);
assert(sensors);
reset_state();
}
Car::~Car()
{
shutdown();
}
void Car::shutdown()
{
logger(LOG_INFO, "car shutting down");
m_right.shutdown();
m_left.shutdown();
m_driver->execute();
reset_state();
}
void Car::reset_state()
{
m_leftSpeed = 0;
m_rightSpeed = 0;
pid_init(m_leftPid, 0, PID_KP, PID_KI, PID_KD);
pid_init(m_rightPid, 1, PID_KP, PID_KI, PID_KD);
}
int Car::initialize(int left_id, int right_id, uint64_t period)
{
logger(LOG_INFO, "car initializing with period=%llu", period);
reset_state();
int res = 0;
m_period = period;
m_previous = 0;
res = m_left.initialize(left_id);
if (res)
goto fail;
res = m_right.initialize(right_id);
if (res)
goto fail;
return 0;
fail:
if (res)
logger(LOG_ERROR, "cannot initialize car err=%d", res);
return res;
}
int Car::run(int distance)
{
const int ticks = distance * ENCODER_SCALER;
logger(LOG_INFO, "car run distance=%d ticks=%d", distance, ticks);
pid_init(m_leftPid, 0, PID_KP, PID_KI, PID_KD);
pid_init(m_rightPid, 1, PID_KP, PID_KI, PID_KD);
m_leftPid.target = ticks;
m_rightPid.target = ticks;
m_previous = 0;
return 0;
}
void Car::stop()
{
logger(LOG_INFO, "car stopping");
m_left.set_speed(0);
m_right.set_speed(0);
m_driver->execute();
reset_state();
}
int Car::turn(int angle)
{
// this is BS, but let's start with something.
const int ticks = angle * ENCODER_SCALER / 5.0f;
logger(LOG_INFO, "car turn angle=%d ticks=%d", angle, ticks);
pid_init(m_leftPid, 0, PID_KP, PID_KI, PID_KD);
pid_init(m_rightPid, 1, PID_KP, PID_KI, PID_KD);
m_leftPid.target = ticks > 0 ? ticks : -ticks;
m_rightPid.target = ticks > 0 ? -ticks : ticks;
m_previous = 0;
return 0;
}
int Car::update_pid(uint64_t now, Pid &pid_left, Pid &pid_right)
{
int res = 0;
int res2 = 0;
res = m_sensors->m_encoder.error;
if (res)
goto out;
pid_left.actual = -m_sensors->m_encoder.left;
pid_right.actual = m_sensors->m_encoder.right;
if (pid_left.target != 0)
logger(LOG_INFO, "Car id=%d left=%u", pid_left.id, pid_left.actual);
if (pid_right.target != 0)
logger(LOG_INFO, "Car id=%d right=%u", pid_right.id, pid_right.actual);
pid_update(pid_left, now);
pid_update(pid_right, now);
if (pid_left.target != 0)
pid_logger(pid_left);
if (pid_right.target != 0)
pid_logger(pid_right);
out:
return res;
}
int Car::update_wheel(Wheel &wheel, int &speed, Pid &pid, bool &isExecute)
{
int new_speed = pid.target ? convert_to_speed(pid) : 0;
if (speed == new_speed)
return 0;
// ramp up slowly
const int delta = new_speed - speed;
if (delta > 0 && delta > SPEED_STEP)
new_speed = speed + SPEED_STEP;
logger(LOG_INFO, "Car pid=%d old_speed=%d new_speed=%d delta=%d", pid.id, speed, new_speed, delta);
isExecute = true;
speed = new_speed;
int res = wheel.set_speed(speed);
if (res)
logger(LOG_ERROR, "Error in wheel set/release err=%d", res);
return res;
}
int Car::update(uint64_t now)
{
if (Timer::get_elapsed_usec(m_previous, now) <= m_period)
return 0;
m_previous = now;
int res = 0;
bool isExecute = false;
res = update_pid(now, m_leftPid, m_rightPid);
if (res) {
logger(LOG_ERROR, "Error Car::update update_pid err=%d", res);
goto fail;
}
res = update_wheel(m_left, m_leftSpeed, m_leftPid, isExecute);
if (res) {
logger(LOG_ERROR, "Error in Car::update update_wheel(left) err=%d", res);
goto fail;
}
res = update_wheel(m_right, m_rightSpeed, m_rightPid, isExecute);
if (res) {
logger(LOG_ERROR, "Error in Car::update update_wheel(right) err=%d", res);
goto fail;
}
if (isExecute) {
res = m_driver->execute();
if (res) {
logger(LOG_ERROR, "Error in Car::update driver execute err=%d", res);
goto fail;
}
}
fail:
return res;
}
} // namespace robo