Statistics
| Branch: | Tag: | Revision:

amiro-os / devices / DiWheelDrive / DiWheelDrive.cpp @ d607fcef

History | View | Annotate | Download (10.743 KB)

1
#include "ch.hpp"
2
#include "hal.h"
3
#include "qei.h"
4
#include "DiWheelDrive.h"
5
#include <chprintf.h>
6

    
7

    
8
#include <global.hpp>
9

    
10
using namespace chibios_rt;
11
using namespace amiro;
12
using namespace types;
13

    
14
extern volatile uint32_t shutdown_now;
15
extern Global global;
16

    
17
DiWheelDrive::DiWheelDrive(CANDriver *can)
18
    : ControllerAreaNetworkTx(can, CAN::DI_WHEEL_DRIVE_ID),
19
      ControllerAreaNetworkRx(can, CAN::DI_WHEEL_DRIVE_ID),
20
      bcCounter(0)
21
{
22
}
23

    
24
msg_t DiWheelDrive::receiveMessage(CANRxFrame *frame) {
25
  int deviceId = this->decodeDeviceId(frame);
26

    
27
  switch (deviceId) {
28

    
29
    case CAN::SHELL_REPLY_ID(CAN::DI_WHEEL_DRIVE_ID):
30
      if (frame->DLC > 0) {
31
        sdWrite(&SD1, frame->data8, frame->DLC);
32
        return RDY_OK;
33
      }
34
      break;
35

    
36
    case CAN::SHELL_QUERY_ID(CAN::DI_WHEEL_DRIVE_ID):
37
      if (frame->DLC != 0) {
38
        global.sercanmux1.convCan2Serial(frame->data8, frame->DLC);
39
        return RDY_OK;
40
      } else {
41
        global.sercanmux1.rcvSwitchCmd(this->decodeBoardId(frame));
42
        return RDY_OK;
43
      }
44
      break;
45

    
46
    case CAN::TARGET_SPEED_ID:
47
      if (frame->DLC == 8) {
48
        global.distcontrol.deactivateController();
49
        kinematic targetVelocity;
50
        targetVelocity.x = frame->data32[0];
51
        targetVelocity.w_z = frame->data32[1];
52
        global.motorcontrol.setTargetSpeed(targetVelocity);
53
        return RDY_OK;
54
      }
55
      break;
56

    
57
    case CAN::TARGET_RPM_ID:
58
      if (frame->DLC == 8) {
59
        global.distcontrol.deactivateController();
60
        global.motorcontrol.setTargetRPM(frame->data32[0], frame->data32[1]);
61
        return RDY_OK;
62
      }
63
      break;
64

    
65
    case CAN::SET_ODOMETRY_ID:
66
      if (frame->DLC == 8) {
67
        int32_t robotPositionX = (frame->data8[0] << 8 | frame->data8[1] << 16 | frame->data8[2] << 24);
68
        int32_t robotPositionY = (frame->data8[3] << 8 | frame->data8[4] << 16 | frame->data8[5] << 24);
69
        int32_t robotPositionF_Z = (frame->data8[6] << 8 | frame->data8[7] << 16);
70
        global.odometry.setPosition(float(robotPositionX)*1e-6,float(robotPositionY)*1e-6,float(robotPositionF_Z)*1e-6);
71
        return RDY_OK;
72
      }
73
      break;
74

    
75
    case CAN::BROADCAST_SHUTDOWN:
76
      if (frame->DLC == 2 && frame->data16[0] == CAN::SHUTDOWN_MAGIC) {
77
        shutdown_now = 0x4;
78
        return RDY_OK;
79
      }
80
      break;
81

    
82
    case CAN::CALIBRATE_PROXIMITY_FLOOR:
83
      // Dont care about the payload but start the calibration
84
      // TODO Care about the payload. Differ between:
85
      // 1: Do fresh calibration (Save values to memory and to temporary values)
86
      // 2: Remove temporary Calibration and get uncalibrated values
87
      // 3: Load calibration from memory
88
      this->calibrate();
89
      break;
90

    
91
    case CAN::TARGET_POSITION_ID:
92
      if (frame->DLC == 8) {
93
        // Robot target position [x] = µm, [f_z] = µrad, [t] = ms
94
        int32_t robotPositionX = (frame->data8[0] << 8 | frame->data8[1] << 16 | frame->data8[2] << 24);
95
        int32_t robotPositionF_Z = (frame->data8[3] << 8 | frame->data8[4] << 16 | frame->data8[5] << 24);
96
        uint16_t targetTimeMilliSeconds = (frame->data8[6] | frame->data8[7] << 8);
97
        //chprintf((BaseSequentialStream*) &SD1, "\nx=%d\nf_z=%d\nt=%d", robotPositionX, robotPositionF_Z, targetTimeMilliSeconds);
98
        global.distcontrol.setTargetPosition(robotPositionX, robotPositionF_Z, targetTimeMilliSeconds);
99
        return RDY_OK;
100
      }
101
      break;
102
    case CAN::SET_LINE_FOLLOW_SPEED:
103
      if (frame->DLC == 8) {
104
        uint8_t speedForward    = frame->data8[0];
105
        uint8_t speedSoftLeft0  = frame->data8[1];
106
        uint8_t speedSoftLeft1  = frame->data8[2];
107
        uint8_t speedHardLeft0  = frame->data8[3];
108
        uint8_t speedHardLeft1  = frame->data8[4];
109
        global.rpmForward[0] = speedForward;
110
        global.rpmForward[1] = speedForward;
111
        global.rpmSoftLeft[0] = speedSoftLeft0;
112
        global.rpmSoftLeft[1] = speedSoftLeft1;
113
        global.rpmHardLeft[0] = speedHardLeft0;
114
        global.rpmHardLeft[1] = speedHardLeft1;
115
        global.rpmSoftRight[0] = global.rpmSoftLeft[1];
116
        global.rpmSoftRight[1] = global.rpmSoftLeft[0];
117
        global.rpmHardRight[0] = global.rpmHardLeft[1];
118
        global.rpmHardRight[1] = global.rpmHardLeft[0];
119
        return RDY_OK;
120
      }
121
      break;
122
    case CAN::SET_LINE_FOLLOW_MSG:
123
      chprintf((BaseSequentialStream*) &SD1, "Received Strategy!\n");
124
      if (frame->DLC == 1) {
125
        global.lfStrategy = frame->data8[0];
126
        global.msgReceived = true;
127
        return RDY_OK;
128
      }
129
      break;
130
    case CAN::SET_KINEMATIC_CONST_ID:
131
      if (frame->DLC == 8) {
132
/*        // Set (but do not store) Ed
133
        global.motorcontrol.setWheelDiameterCorrectionFactor(static_cast<float>(frame->data32[0]), false);
134
        // Set (but do not store) Eb
135
        global.motorcontrol.setActualWheelBaseDistance(static_cast<float>(frame->data32[1]), false);
136
        return RDY_OK;*/
137
        // Set (but do not store) Ed
138
        uint32_t ed_int = static_cast<uint32_t>(frame->data32[0]);
139
        float ed_float = static_cast<float>(ed_int)/1000000.0;
140
        global.motorcontrol.setWheelDiameterCorrectionFactor(ed_float, false);
141
        // Set (but do not store) Eb
142
        uint32_t eb_int = static_cast<uint32_t>(frame->data32[1]);
143
        float eb_float = static_cast<float>(eb_int)/1000000.0;
144
        global.motorcontrol.setActualWheelBaseDistance(eb_float, false);
145
        //chprintf((BaseSequentialStream*) &SD1, "Edi=%i, Edf=%f, Ebi=%i, Ebf=%f\n", ed_int, ed_float, eb_int, eb_float);
146
        return RDY_OK;
147
      }
148
      break;
149

    
150
    case CAN::POWER_STATUS_ID:
151
      if (frame->DLC == 6) {
152
        // The power status is evaluated by inherited ControllerAreaNetworkRx object, but depending on the flags the power path controller needs to enabled or disabled.
153
        types::power_status::ChargingState charging_flags;
154
        charging_flags.value = frame->data8[0];
155
        global.ltc4412.enable(charging_flags.content.diwheeldrive_enable_power_path);
156
        // Do not return with RDY_OK, or the inherited ControllerAreaNetworkRx object would not evaluate the rest of this message.
157
      }
158
    break;
159

    
160
    default:
161
      break;
162
  }
163
  return -1;
164
}
165

    
166
msg_t DiWheelDrive::updateSensorVal() {
167

    
168
  // Update robot velocity values
169
  kinematic currentVelocity = global.motorcontrol.getCurrentVelocity();
170
  this->actualSpeed[0] = currentVelocity.x;
171
  this->actualSpeed[1] = currentVelocity.w_z;
172

    
173
  // Update odometry values
174
  this->robotPosition = global.odometry.getPosition();
175

    
176
  // Update proximity values
177
  for (int idx = 0; idx < 4; ++idx)
178
    this->proximityFloorValue[idx] = global.vcnl4020[idx].getProximityScaledWoOffset();
179

    
180
  // Update magnetometer values
181
  for (uint8_t axis = 0; axis < 3; ++axis) {
182
    this->magnetometerValue[axis] = global.hmc5883l.getMagnetizationGauss(axis);
183
  }
184

    
185
  // Update gyroscope values
186
  for (uint8_t axis = 0; axis < 3; ++axis) {
187
    this->gyroscopeValue[axis] = global.l3g4200d.getAngularRate(axis);
188
  }
189

    
190
  return 0;
191
}
192

    
193
void DiWheelDrive::periodicBroadcast() {
194
  CANTxFrame frame;
195
  frame.SID = 0;
196

    
197
  // Send the velocites µm/s of the x axis and µrad/s around z axis: start
198
  this->encodeDeviceId(&frame, CAN::ACTUAL_SPEED_ID);
199
  frame.data32[0] = this->actualSpeed[0];
200
  frame.data32[1] = this->actualSpeed[1];
201
  frame.DLC = 8;
202
  this->transmitMessage(&frame);
203

    
204
  // Send the valocites µm/s of the x axis and µrad/s around z axis: end
205
  // Send the odometry: start
206
  BaseThread::sleep(US2ST(10)); // Use to sleep for 10 CAN cycle (@1Mbit), otherwise the cognition-board might not receive all messagee
207
  // Set the frame id
208
  frame.SID = 0;
209
  this->encodeDeviceId(&frame, CAN::ODOMETRY_ID);
210
  // Cut of the first byte, which precission is not needed
211
  int32_t x_mm = (this->robotPosition.x >> 8);
212
  int32_t y_mm = (this->robotPosition.y >> 8);
213
  int16_t f_z_mrad = int16_t(this->robotPosition.f_z >> 8 );
214
  // Copy the data structure
215
  memcpy((uint8_t *)&(frame.data8[0]), (uint8_t *)&x_mm, 3);
216
  memcpy((uint8_t *)&(frame.data8[3]), (uint8_t *)&y_mm, 3);
217
  memcpy((uint8_t *)&(frame.data8[6]), (uint8_t *)&f_z_mrad, 2);
218
  frame.DLC = 8;
219
  this->transmitMessage(&frame);
220

    
221
  // Send the odometry: end
222
  // Send the proximity values of the floor: start
223
  BaseThread::sleep(US2ST(10)); // Use to sleep for 10 CAN cycle (@1Mbit), otherwise the cognition-board might not receive all messagee
224
  // Set the frame id
225
  frame.SID = 0;
226
  this->encodeDeviceId(&frame, CAN::PROXIMITY_FLOOR_ID);
227
  frame.data16[0] = this->proximityFloorValue[0];
228
  frame.data16[1] = this->proximityFloorValue[1];
229
  frame.data16[2] = this->proximityFloorValue[2];
230
  frame.data16[3] = this->proximityFloorValue[3];
231
  frame.DLC = 8;
232
  this->transmitMessage(&frame);
233

    
234
  // Send the magnetometer data
235
  for (uint8_t axis = 0; axis < 3; ++axis) {
236
    frame.SID = 0;
237
    this->encodeDeviceId(&frame, CAN::MAGNETOMETER_X_ID + axis); // Y- and Z-axis have according IDs
238
    frame.data32[0] = this->magnetometerValue[axis];
239
    frame.DLC = 4;
240
    this->transmitMessage(&frame);
241
  }
242

    
243
  // Send gyroscope data
244
  frame.SID = 0;
245
  this->encodeDeviceId(&frame, CAN::GYROSCOPE_ID);
246
  frame.data16[0] = this->gyroscopeValue[0];
247
  frame.data16[1] = this->gyroscopeValue[1];
248
  frame.data16[2] = this->gyroscopeValue[2];
249
  frame.DLC = 6;
250
  this->transmitMessage(&frame);
251

    
252
  
253

    
254
  // Send the board ID (board ID of DiWheelDrive = Robot ID)
255
  if (this->bcCounter % 10 == 0) {
256
    frame.SID = 0;
257
    this->encodeDeviceId(&frame, CAN::ROBOT_ID);
258
    frame.data8[0] = this->robotId;
259
    frame.DLC = 1;
260
    this->transmitMessage(&frame);
261
  }
262

    
263
  ++this->bcCounter;
264
}
265

    
266
void DiWheelDrive::calibrate() {
267
  // Stop sending and receiving of values to indicate the calibration phase
268
//   eventTimerEvtSource->unregister(&this->eventTimerEvtListener);
269
//   rxFullCanEvtSource->unregister(&this->rxFullCanEvtListener);
270

    
271
  this->calibrateProximityFloorValues();
272

    
273
  // Start sending and receving of values
274
//   eventTimerEvtSource->registerOne(&this->eventTimerEvtListener, CAN::PERIODIC_TIMER_ID);
275
//   rxFullCanEvtSource->registerOne(&this->rxFullCanEvtListener, CAN::RECEIVED_ID);
276

    
277
}
278

    
279
void DiWheelDrive::calibrateProximityFloorValues() {
280

    
281
  uint16_t buffer;
282
  for (uint8_t idx = 0; idx < 4; ++idx) {
283
    global.vcnl4020[idx].calibrate();
284
    buffer = global.vcnl4020[idx].getProximityOffset();
285
    global.memory.setVcnl4020Offset(buffer,idx);
286
  }
287

    
288
}
289

    
290
ThreadReference DiWheelDrive::start(tprio_t PRIO) {
291
  // set the robot ID as the board ID, which is read from the memory
292
  if (global.memory.getBoardId(&this->robotId) != fileSystemIo::FileSystemIoBase::OK) {
293
    this->robotId = 0;
294
  }
295

    
296
  this->ControllerAreaNetworkRx::start(PRIO + 1);
297
  this->ControllerAreaNetworkTx::start(PRIO);
298
  return NULL;
299
}
300

    
301
msg_t
302
DiWheelDrive::terminate(void) {
303
  msg_t ret = RDY_OK;
304

    
305
  this->ControllerAreaNetworkTx::requestTerminate();
306
  ret |= this->ControllerAreaNetworkTx::wait();
307
  this->ControllerAreaNetworkRx::requestTerminate();
308
  ret |= this->ControllerAreaNetworkRx::wait();
309

    
310
  return ret;
311
}