-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathremote_control_car.hpp
63 lines (56 loc) · 1.64 KB
/
remote_control_car.hpp
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
#pragma once
#include "car/picar.hpp"
#include "common/log.hpp"
#include "joystick/joystick_pi.hpp"
constexpr int16_t kAxisMax = 32767;
constexpr int16_t kAxisMin = -32767;
class RemoteControlCar : public JoyStickPi {
public:
RemoteControlCar() = default;
int Init() {
CHECK(JoyStickPi::Init() == 0) << "joystick init err";
CHECK(picar_.Init() == 0) << "picar init err";
return 0;
}
virtual void HandleButton(ButtonName btn, int16_t val, uint32_t t) {
JoyStickPi::HandleButton(btn, val, t);
switch (btn) {
case ButtonB:
picar_.Reset();
}
}
virtual void HandleAxis(AxisName axis, int16_t val, uint32_t t) {
JoyStickPi::HandleAxis(axis, val, t);
switch (axis) {
case AxisLeftHandleHorizontal:
picar_.SetDirection(ratio(val) * FLAGS_dir_max_angle);
break;
case AxisRightHandleVertical:
picar_.Forward(-ratio(val) * 100.0);
break;
case AxisDirLeftRight:
if (val == kAxisMax) {
picar_.SetCamYaw(picar_.GetCamYaw() + 5.0);
} else if (val == kAxisMin) {
picar_.SetCamYaw(picar_.GetCamYaw() - 5.0);
}
break;
case AxisDirUpDown:
if (val == kAxisMin) {
picar_.SetCamPitch(picar_.GetCamPitch() + 5.0);
} else if (val == kAxisMax) {
picar_.SetCamPitch(picar_.GetCamPitch() - 5.0);
}
break;
case AxisLeftHandleVertical:
picar_.SetCamPitch(-ratio(val) * FLAGS_cam_pitch_max_angle);
break;
case AxisRightHandleHorizontal:
picar_.SetCamYaw(ratio(val) * FLAGS_cam_yaw_max_angle);
break;
}
}
static double ratio(double val) { return val / kAxisMax; }
private:
Picar picar_;
};