source: flair-src/trunk/lib/FlairSensorActuator/src/SimuImu.cpp@ 223

Last change on this file since 223 was 214, checked in by Sanahuja Guillaume, 6 years ago

matrix

File size: 3.8 KB
RevLine 
[3]1// %flair:license{
[15]2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
[3]4// %flair:license}
5// created: 2014/02/07
6// filename: SimuImu.cpp
7//
8// author: Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: Class for a simulation imu
14//
15//
16/*********************************************************************/
17
18#include "SimuImu.h"
19#include <FrameworkManager.h>
20#include <ImuData.h>
21#include <SpinBox.h>
22#include <GroupBox.h>
[214]23#include <Matrix.h>
[3]24#include <SharedMem.h>
25#include <AhrsData.h>
26#include <sstream>
27
28using std::string;
29using std::ostringstream;
30using namespace flair::core;
31using namespace flair::gui;
32
[15]33namespace flair {
34namespace sensor {
[3]35
[202]36//Construct a SimuImu. Control part.
[158]37SimuImu::SimuImu(string name, uint32_t modelId,uint32_t deviceId,
[15]38 uint8_t priority)
[198]39 : Imu(name,false), Thread(getFrameworkManager(), name, priority) {
[55]40 dataRate =
[15]41 new SpinBox(GetGroupBox()->NewRow(), "data rate", " Hz", 1, 500, 1, 200);
42 ahrsData = new AhrsData((Imu *)this);
[158]43
44 shmem = new SharedMem((Thread *)this, ShMemName(modelId, deviceId),
[15]45 sizeof(imu_states_t));
[157]46 SetIsReady(true);
[3]47}
48
[202]49//Construct a SimuImu. Simulation part.
[158]50SimuImu::SimuImu(const IODevice *parent, string name, uint32_t modelId,uint32_t deviceId)
[137]51 : Imu(parent,name), Thread(parent, name, 0) {
[55]52 dataRate = NULL;
[3]53
[158]54 shmem = new SharedMem((Thread *)this, ShMemName(modelId, deviceId),
[15]55 sizeof(imu_states_t));
[157]56 SetIsReady(true);
[3]57}
58
59SimuImu::~SimuImu() {
[15]60 SafeStop();
61 Join();
[3]62}
63
[158]64string SimuImu::ShMemName(uint32_t modelId,uint32_t deviceId) {
65 ostringstream dev_name;
66 dev_name << "simu" << modelId << "_imu_" << deviceId;
67 return dev_name.str().c_str();
68}
69
[3]70void SimuImu::UpdateFrom(const io_data *data) {
[15]71 if (data != NULL) {
[214]72 Matrix *input = (Matrix *)data;
[15]73 imu_states_t state;
[3]74
[15]75 input->GetMutex();
76 state.q0 = input->ValueNoMutex(0, 0);
77 state.q1 = input->ValueNoMutex(1, 0);
78 state.q2 = input->ValueNoMutex(2, 0);
79 state.q3 = input->ValueNoMutex(3, 0);
80 state.wx = input->ValueNoMutex(7, 0);
81 state.wy = input->ValueNoMutex(8, 0);
82 state.wz = input->ValueNoMutex(9, 0);
[186]83 state.ax = input->ValueNoMutex(13, 0);
84 state.ay = input->ValueNoMutex(14, 0);
85 state.az = input->ValueNoMutex(15, 0);
86 state.mx = input->ValueNoMutex(16, 0);
87 state.my = input->ValueNoMutex(17, 0);
88 state.mz = input->ValueNoMutex(18, 0);
[15]89 input->ReleaseMutex();
[3]90
[15]91 shmem->Write((char *)&state, sizeof(imu_states_t));
92 }
[3]93}
94
95void SimuImu::Run(void) {
[15]96 imu_states_t state;
97 ImuData *imuData;
98 GetDatas(&imuData);
[3]99
[55]100 if (dataRate == NULL) {
[15]101 Thread::Err("not applicable for simulation part.\n");
102 return;
103 }
[3]104
[55]105 SetPeriodUS((uint32_t)(1000000. / dataRate->Value()));
[3]106
[15]107 while (!ToBeStopped()) {
108 WaitPeriod();
[3]109
[55]110 if (dataRate->ValueChanged() == true) {
111 SetPeriodUS((uint32_t)(1000000. / dataRate->Value()));
[15]112 }
113 shmem->Read((char *)&state, sizeof(imu_states_t));
[202]114 Quaternion quaternion(state.q0, state.q1, state.q2, state.q3);
[173]115 Vector3Df angRate(state.wx, state.wy, state.wz);
[186]116 Vector3Df rawAcc(state.ax, state.ay, state.az);
117 Vector3Df rawMag(state.mx, state.my, state.mz);
118 Vector3Df rawGyr(state.wx, state.wy, state.wz);
[198]119 //we do not need rotation in simulation
120 /*
[173]121 ApplyRotation(angRate);
122 ApplyRotation(quaternion);
[186]123 ApplyRotation(rawAcc);
124 ApplyRotation(rawMag);
[198]125 ApplyRotation(rawGyr);*/
[173]126 ahrsData->SetQuaternionAndAngularRates(quaternion,angRate);
[186]127 imuData->SetRawAccMagAndGyr(rawAcc,rawMag,rawGyr);
[15]128 imuData->SetDataTime(GetTime());
129 ahrsData->SetDataTime(GetTime());
130 ProcessUpdate(ahrsData);
131 }
[3]132}
133
134} // end namespace sensor
135} // end namespace flair
Note: See TracBrowser for help on using the repository browser.