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

Last change on this file since 186 was 186, checked in by Sanahuja Guillaume, 5 years ago

maj imu

File size: 3.6 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
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>
23#include <cvmatrix.h>
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
33namespace flair {
34namespace sensor {
35
36SimuImu::SimuImu(string name, uint32_t modelId,uint32_t deviceId,
37 uint8_t priority)
38 : Imu(name), Thread(getFrameworkManager(), name, priority) {
39 dataRate =
40 new SpinBox(GetGroupBox()->NewRow(), "data rate", " Hz", 1, 500, 1, 200);
41 ahrsData = new AhrsData((Imu *)this);
42
43 shmem = new SharedMem((Thread *)this, ShMemName(modelId, deviceId),
44 sizeof(imu_states_t));
45 SetIsReady(true);
46}
47
48SimuImu::SimuImu(const IODevice *parent, string name, uint32_t modelId,uint32_t deviceId)
49 : Imu(parent,name), Thread(parent, name, 0) {
50 dataRate = NULL;
51
52 shmem = new SharedMem((Thread *)this, ShMemName(modelId, deviceId),
53 sizeof(imu_states_t));
54 SetIsReady(true);
55}
56
57SimuImu::~SimuImu() {
58 SafeStop();
59 Join();
60}
61
62string SimuImu::ShMemName(uint32_t modelId,uint32_t deviceId) {
63 ostringstream dev_name;
64 dev_name << "simu" << modelId << "_imu_" << deviceId;
65 return dev_name.str().c_str();
66}
67
68void SimuImu::UpdateFrom(const io_data *data) {
69 if (data != NULL) {
70 cvmatrix *input = (cvmatrix *)data;
71 imu_states_t state;
72
73 input->GetMutex();
74 state.q0 = input->ValueNoMutex(0, 0);
75 state.q1 = input->ValueNoMutex(1, 0);
76 state.q2 = input->ValueNoMutex(2, 0);
77 state.q3 = input->ValueNoMutex(3, 0);
78 state.wx = input->ValueNoMutex(7, 0);
79 state.wy = input->ValueNoMutex(8, 0);
80 state.wz = input->ValueNoMutex(9, 0);
81 state.ax = input->ValueNoMutex(13, 0);
82 state.ay = input->ValueNoMutex(14, 0);
83 state.az = input->ValueNoMutex(15, 0);
84 state.mx = input->ValueNoMutex(16, 0);
85 state.my = input->ValueNoMutex(17, 0);
86 state.mz = input->ValueNoMutex(18, 0);
87 input->ReleaseMutex();
88
89 shmem->Write((char *)&state, sizeof(imu_states_t));
90 }
91}
92
93void SimuImu::Run(void) {
94 imu_states_t state;
95 ImuData *imuData;
96 GetDatas(&imuData);
97
98 if (dataRate == NULL) {
99 Thread::Err("not applicable for simulation part.\n");
100 return;
101 }
102
103 SetPeriodUS((uint32_t)(1000000. / dataRate->Value()));
104
105 while (!ToBeStopped()) {
106 WaitPeriod();
107
108 if (dataRate->ValueChanged() == true) {
109 SetPeriodUS((uint32_t)(1000000. / dataRate->Value()));
110 }
111
112 shmem->Read((char *)&state, sizeof(imu_states_t));
113
114 Quaternion quaternion(state.q0, state.q1, state.q2, state.q3);
115 Vector3Df angRate(state.wx, state.wy, state.wz);
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);
119 ApplyRotation(angRate);
120 ApplyRotation(quaternion);
121 ApplyRotation(rawAcc);
122 ApplyRotation(rawMag);
123 ApplyRotation(rawGyr);
124 ahrsData->SetQuaternionAndAngularRates(quaternion,angRate);
125 imuData->SetRawAccMagAndGyr(rawAcc,rawMag,rawGyr);
126 imuData->SetDataTime(GetTime());
127 ahrsData->SetDataTime(GetTime());
128
129 ProcessUpdate(ahrsData);
130 }
131}
132
133} // end namespace sensor
134} // end namespace flair
Note: See TracBrowser for help on using the repository browser.