-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrf_controller.cpp
More file actions
176 lines (147 loc) · 4.86 KB
/
rf_controller.cpp
File metadata and controls
176 lines (147 loc) · 4.86 KB
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
#include "rf_controller.h"
RFController::RFController(HardwareSerial& serial) : serialPort(serial) {}
void RFController::setChannel(uint8_t estopChannel, uint8_t disconnectChannel, uint8_t driveModeChannel) {
CH_ESTOP = estopChannel;
CH_DISCONNECT = disconnectChannel;
CH_DRIVE_MODE = driveModeChannel;
}
bool RFController::checkChannel() {
if (CH_ESTOP != CH_NULL && CH_DISCONNECT != CH_NULL) {
return true; // 값들이 유효하면 true 반환
}
return false;
}
bool RFController::begin() {
if (!checkChannel()) {
return false;
}
ibus.begin(serialPort);
while (cnt_rec == 0) { // 첫 번째 iBus 메시지를 받을 때까지 대기
ibus.loop();
cnt_rec = ibus.cnt_rec;
delay(50);
}
return true;
}
int RFController::readChannel(uint8_t channel, int minLimit, int maxLimit, int defaultValue) {
uint16_t ch = ibus.readChannel(channel);
if (ch >= 100) {
return map(ch, 1000, 2000, minLimit, maxLimit);
}
return defaultValue;
}
int RFController::readThreeStageSwitch(uint8_t channel, int defaultValue) {
uint16_t ch = ibus.readChannel(channel);
if (ch >= 100) {
if (ch > 1500) return -1; // 후진
else if (ch < 1500) return 1; // 전진
else return 0; // 중립
}
return defaultValue;
}
void RFController::checkConnection() {
bool is_receiver_on = (cnt_rec != ibus.cnt_rec);
if (is_receiver_on) {
cnt_rec = ibus.cnt_rec;
}
bool is_transmitter_on = !readSwitch(CH_DISCONNECT, false);
is_connected = is_receiver_on && is_transmitter_on;
}
bool RFController::readSwitch(uint8_t channel, bool defaultValue) {
int intDefaultValue = (defaultValue) ? 100 : 0;
int ch = readChannel(channel, 0, 100, intDefaultValue);
return (ch > 50);
}
// =========================================================================
RFControllerDDRobot::RFControllerDDRobot(HardwareSerial& serial) : RFController(serial) {}
void RFControllerDDRobot::setChannel(uint8_t estopChannel, uint8_t disconnectChannel, uint8_t driveModeChannel, uint8_t velBrkChannel, uint8_t omegaChannel) {
RFController::setChannel(estopChannel, disconnectChannel, driveModeChannel);
CH_VEL_BRK = velBrkChannel;
CH_OMEGA = omegaChannel;
}
void RFControllerDDRobot::setMaxOmega(float max_omega_) {
max_omega = max_omega_ * 100;
}
void RFControllerDDRobot::setMaxVelocity(float max_vel_) {
max_vel = max_vel_ * 100;
}
void RFControllerDDRobot::getCommand(CommandDDRobot &cmd) {
// velocity
cmd.velocity = readChannel(CH_VEL_BRK, -max_vel, max_vel, -5000) - 1;
if (cmd.velocity <= 3 && cmd.velocity >= -3)
{
cmd.velocity = 0;
}
// omega
cmd.omega = readChannel(CH_OMEGA, -max_omega, max_omega, -5000);
if (cmd.omega <= 20 && cmd.omega >= -20)
{
cmd.omega = 0;
}
// estop
cmd.estop = readSwitch(CH_ESTOP, false);
// drive_mode
bool isAuto = readSwitch(CH_DRIVE_MODE, false);
cmd.drive_mode = isAuto ? MODE_AUTO : MODE_MANUAL;
}
// =========================================================================
RFControllerSteeringRobot::RFControllerSteeringRobot(HardwareSerial& serial) : RFController(serial) {}
void RFControllerSteeringRobot::setChannel(uint8_t estopChannel, uint8_t disconnectChannel, uint8_t gearChannel, uint8_t driveModeChannel, uint8_t velBrkChannel, uint8_t steerChannel) {
RFController::setChannel(estopChannel, disconnectChannel, driveModeChannel);
CH_VEL_BRK = velBrkChannel;
CH_STEER = steerChannel;
CH_GEAR = gearChannel;
}
void RFControllerSteeringRobot::setMaxSteer(float max_steer_) {
max_steer = max_steer_ * 100;
}
void RFControllerSteeringRobot::setMaxVelocity(float max_vel_) {
max_vel = max_vel_ * 100;
}
void RFControllerSteeringRobot::getCommand(CommandSteeringRobot &cmd) {
// velocity
int vel = readChannel(CH_VEL_BRK, -max_vel, max_vel, -5000) - 1;
if (vel <= 3 && vel >= -3) // near zero
{
cmd.velocity = 0;
cmd.brake = 0;
}
else if (vel < 0)
{
cmd.brake = -vel;
cmd.velocity = 0;
}
else
{
cmd.velocity = vel;
cmd.brake = 0;
}
// omega
cmd.steer = readChannel(CH_STEER, -max_steer, max_steer, -5000);
if (cmd.steer <= 20 && cmd.steer >= -20)
{
cmd.steer = 0;
}
// gear
int gear_ = readThreeStageSwitch(CH_GEAR, -100);
switch (gear_) {
case 1:
cmd.gear = GEAR_FORWARD;
break;
case 0:
cmd.gear = GEAR_NEUTRAL;
break;
case -1:
cmd.gear = GEAR_REVERSE;
break;
case -100:
default:
cmd.gear = GEAR_NONE;
break;
}
// estop
cmd.estop = readSwitch(CH_ESTOP, false);
// drive_mode
bool isAuto = readSwitch(CH_DRIVE_MODE, false);
cmd.drive_mode = isAuto ? MODE_AUTO : MODE_MANUAL;
}