amiro-os / components / ControllerAreaNetworkRx.cpp @ 1b3adcdd
History | View | Annotate | Download (6.458 KB)
1 |
#include <ch.hpp> |
---|---|
2 |
#include <hal.h> |
3 |
#include <string.h> // memcpy |
4 |
|
5 |
#include <amiro/Constants.h> |
6 |
#include <amiro/ControllerAreaNetworkRx.h> |
7 |
|
8 |
#include <global.hpp> |
9 |
|
10 |
using namespace chibios_rt; |
11 |
using namespace amiro; |
12 |
|
13 |
extern Global global;
|
14 |
|
15 |
using namespace types; |
16 |
using namespace amiro::constants; |
17 |
|
18 |
ControllerAreaNetworkRx::ControllerAreaNetworkRx(CANDriver *can, const uint8_t boardId)
|
19 |
: BaseStaticThread<128>(),
|
20 |
boardId(boardId), |
21 |
canDriver(can) { |
22 |
#ifdef STM32F4XX
|
23 |
this->canConfig.mcr = CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP;
|
24 |
this->canConfig.btr = CAN_BTR_SJW(1) | CAN_BTR_TS2(3) | CAN_BTR_TS1(15) |
25 |
| CAN_BTR_BRP(1);
|
26 |
#else
|
27 |
this->canConfig.mcr = CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP;
|
28 |
this->canConfig.btr = CAN_BTR_SJW(1) | CAN_BTR_TS2(2) | CAN_BTR_TS1(13) |
29 |
| CAN_BTR_BRP(1);
|
30 |
#endif
|
31 |
} |
32 |
|
33 |
ControllerAreaNetworkRx:: |
34 |
~ControllerAreaNetworkRx() { |
35 |
|
36 |
} |
37 |
|
38 |
//----------------------------------------------------------------
|
39 |
|
40 |
msg_t ControllerAreaNetworkRx::receiveSensorVal(CANRxFrame *frame) { |
41 |
int deviceId = this->decodeDeviceId(frame); |
42 |
|
43 |
// chprintf((BaseSequentialStream*) &global.sercanmux1, "DeviceId: %d\r\n",deviceId);
|
44 |
|
45 |
switch (deviceId) {
|
46 |
case CAN::PROXIMITY_RING_ID(0): |
47 |
case CAN::PROXIMITY_RING_ID(1): |
48 |
case CAN::PROXIMITY_RING_ID(2): |
49 |
case CAN::PROXIMITY_RING_ID(3): |
50 |
case CAN::PROXIMITY_RING_ID(4): |
51 |
case CAN::PROXIMITY_RING_ID(5): |
52 |
case CAN::PROXIMITY_RING_ID(6): |
53 |
case CAN::PROXIMITY_RING_ID(7): |
54 |
if (frame->DLC == 2) { |
55 |
int index = deviceId & 0x7; |
56 |
proximityRingValue[index] = frame->data16[0];
|
57 |
return RDY_OK;
|
58 |
} |
59 |
break;
|
60 |
|
61 |
case CAN::ACTUAL_SPEED_ID:
|
62 |
if (frame->DLC == 8) { |
63 |
actualSpeed[0] = frame->data32[0]; |
64 |
actualSpeed[1] = frame->data32[1]; |
65 |
return RDY_OK;
|
66 |
} |
67 |
break;
|
68 |
|
69 |
case CAN::ODOMETRY_ID:
|
70 |
if (frame->DLC == 8) { |
71 |
this->robotPosition.x = (frame->data8[0] << 8 | frame->data8[1] << 16 | frame->data8[2] << 24); |
72 |
this->robotPosition.y = (frame->data8[3] << 8 | frame->data8[4] << 16 | frame->data8[5] << 24); |
73 |
this->robotPosition.f_z = (frame->data8[6] << 8 | frame->data8[7] << 16); |
74 |
return RDY_OK;
|
75 |
} |
76 |
break;
|
77 |
|
78 |
case CAN::PROXIMITY_FLOOR_ID:
|
79 |
// chprintf((BaseSequentialStream*) &global.sercanmux1, "CAN::PROXIMITY_FLOOR_ID");
|
80 |
if (frame->DLC == 8) { |
81 |
proximityFloorValue[0] = frame->data16[0]; |
82 |
proximityFloorValue[1] = frame->data16[1]; |
83 |
proximityFloorValue[2] = frame->data16[2]; |
84 |
proximityFloorValue[3] = frame->data16[3]; |
85 |
return RDY_OK;
|
86 |
} |
87 |
break;
|
88 |
|
89 |
case CAN::POWER_STATUS_ID:
|
90 |
if (frame->DLC == 6) { |
91 |
powerStatus.charging_flags.value = frame->data8[0];
|
92 |
powerStatus.state_of_charge = frame->data8[1];
|
93 |
powerStatus.minutes_remaining = frame->data16[1];
|
94 |
powerStatus.power_consumption = frame->data16[2];
|
95 |
return RDY_OK;
|
96 |
} |
97 |
break;
|
98 |
|
99 |
case CAN::ROBOT_ID:
|
100 |
if (frame->DLC == 1) { |
101 |
robotId = frame->data8[0];
|
102 |
return RDY_OK;
|
103 |
} |
104 |
break;
|
105 |
|
106 |
case CAN::MAGNETOMETER_X_ID:
|
107 |
if (frame->DLC == 4) { |
108 |
magnetometerValue[0] = frame->data32[0]; |
109 |
return RDY_OK;
|
110 |
} |
111 |
break;
|
112 |
|
113 |
case CAN::MAGNETOMETER_Y_ID:
|
114 |
if (frame->DLC == 4) { |
115 |
magnetometerValue[1] = frame->data32[0]; |
116 |
return RDY_OK;
|
117 |
} |
118 |
break;
|
119 |
|
120 |
case CAN::MAGNETOMETER_Z_ID:
|
121 |
if (frame->DLC == 4) { |
122 |
magnetometerValue[2] = frame->data32[0]; |
123 |
return RDY_OK;
|
124 |
} |
125 |
break;
|
126 |
|
127 |
case CAN::GYROSCOPE_ID:
|
128 |
if (frame->DLC == 6) { |
129 |
gyroscopeValue[0] = frame->data16[0]; |
130 |
gyroscopeValue[1] = frame->data16[1]; |
131 |
gyroscopeValue[2] = frame->data16[2]; |
132 |
return RDY_OK;
|
133 |
} |
134 |
break;
|
135 |
|
136 |
default:
|
137 |
break;
|
138 |
} |
139 |
|
140 |
return RDY_RESET;
|
141 |
} |
142 |
|
143 |
uint16_t ControllerAreaNetworkRx::getProximityRingValue(int index) {
|
144 |
return this->proximityRingValue[index]; |
145 |
} |
146 |
|
147 |
void ControllerAreaNetworkRx::getActualSpeed(types::kinematic &targetSpeed) {
|
148 |
targetSpeed.x = this->actualSpeed[0]; |
149 |
targetSpeed.w_z = this->actualSpeed[1]; |
150 |
} |
151 |
|
152 |
types::position ControllerAreaNetworkRx::getOdometry() { |
153 |
return this->robotPosition; |
154 |
} |
155 |
|
156 |
power_status &ControllerAreaNetworkRx::getPowerStatus() { |
157 |
return this->powerStatus; |
158 |
} |
159 |
|
160 |
uint8_t ControllerAreaNetworkRx::getRobotID() { |
161 |
return this->robotId; |
162 |
} |
163 |
|
164 |
int32_t ControllerAreaNetworkRx::getMagnetometerValue(int axis)
|
165 |
{ |
166 |
return this->magnetometerValue[axis]; |
167 |
} |
168 |
|
169 |
int16_t ControllerAreaNetworkRx::getGyroscopeValue(int axis)
|
170 |
{ |
171 |
return this->gyroscopeValue[axis]; |
172 |
} |
173 |
|
174 |
|
175 |
uint16_t ControllerAreaNetworkRx::getProximityFloorValue(int index) {
|
176 |
return this->proximityFloorValue[index]; |
177 |
} |
178 |
|
179 |
//----------------------------------------------------------------
|
180 |
|
181 |
msg_t ControllerAreaNetworkRx::main(void) {
|
182 |
|
183 |
this->rxFullCanEvtSource = reinterpret_cast<EvtSource *>(&this->canDriver->rxfull_event); |
184 |
|
185 |
this->rxFullCanEvtSource->registerOne(&this->rxFullCanEvtListener, CAN::RECEIVED_ID); |
186 |
|
187 |
canStart(this->canDriver, &this->canConfig); |
188 |
|
189 |
this->setName("ControllerAreaNetworkRx"); |
190 |
|
191 |
while (!this->shouldTerminate()) { |
192 |
eventmask_t eventMask = this->waitOneEventTimeout(ALL_EVENTS, MS2ST(10000)); |
193 |
switch (eventMask) {
|
194 |
|
195 |
case EVENT_MASK(CAN::RECEIVED_ID):
|
196 |
CANRxFrame rxframe; |
197 |
msg_t message = canReceive(this->canDriver, CAN_ANY_MAILBOX, &rxframe, TIME_IMMEDIATE);
|
198 |
if (message == RDY_OK) {
|
199 |
// chprintf((BaseSequentialStream*) &global.sercanmux1, "Rx Message");
|
200 |
message = this->receiveMessage(&rxframe);
|
201 |
if (message != RDY_OK)
|
202 |
this->receiveSensorVal(&rxframe);
|
203 |
} |
204 |
break;
|
205 |
} |
206 |
} |
207 |
|
208 |
canStop(this->canDriver);
|
209 |
this->rxFullCanEvtSource->unregister(&this->rxFullCanEvtListener); |
210 |
|
211 |
return RDY_OK;
|
212 |
} |
213 |
|
214 |
msg_t ControllerAreaNetworkRx::receiveMessage(CANRxFrame *frame) { |
215 |
(void)frame;
|
216 |
return RDY_OK;
|
217 |
} |
218 |
|
219 |
//----------------------------------------------------------------
|
220 |
|
221 |
int ControllerAreaNetworkRx::decodeBoardId(CANRxFrame *frame) {
|
222 |
return (frame->SID >> CAN::BOARD_ID_SHIFT) & CAN::BOARD_ID_MASK;
|
223 |
} |
224 |
|
225 |
int ControllerAreaNetworkRx::decodeDeviceId(CANRxFrame *frame) {
|
226 |
return (frame->SID >> CAN::DEVICE_ID_SHIFT) & CAN::DEVICE_ID_MASK;
|
227 |
} |
228 |
|
229 |
int ControllerAreaNetworkRx::decodeIndexId(CANRxFrame *frame) {
|
230 |
return (frame->SID >> CAN::INDEX_ID_SHIFT) & CAN::INDEX_ID_MASK;
|
231 |
} |
232 |
|
233 |
//----------------------------------------------------------------
|
234 |
|
235 |
int ControllerAreaNetworkRx::rxCmdShell(CANRxFrame *frame) {
|
236 |
return (frame->SID >> CAN::BOARD_ID_SHIFT) & CAN::BOARD_ID_MASK;
|
237 |
} |