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

Last change on this file since 204 was 202, checked in by Sanahuja Guillaume, 7 years ago

modifs usleep

File size: 3.8 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
36//Construct a SimuImu. Control part.
37SimuImu::SimuImu(string name, uint32_t modelId,uint32_t deviceId,
38 uint8_t priority)
39 : Imu(name,false), Thread(getFrameworkManager(), name, priority) {
40 dataRate =
41 new SpinBox(GetGroupBox()->NewRow(), "data rate", " Hz", 1, 500, 1, 200);
42 ahrsData = new AhrsData((Imu *)this);
43
44 shmem = new SharedMem((Thread *)this, ShMemName(modelId, deviceId),
45 sizeof(imu_states_t));
46 SetIsReady(true);
47}
48
49//Construct a SimuImu. Simulation part.
50SimuImu::SimuImu(const IODevice *parent, string name, uint32_t modelId,uint32_t deviceId)
51 : Imu(parent,name), Thread(parent, name, 0) {
52 dataRate = NULL;
53
54 shmem = new SharedMem((Thread *)this, ShMemName(modelId, deviceId),
55 sizeof(imu_states_t));
56 SetIsReady(true);
57}
58
59SimuImu::~SimuImu() {
60 SafeStop();
61 Join();
62}
63
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
70void SimuImu::UpdateFrom(const io_data *data) {
71 if (data != NULL) {
72 cvmatrix *input = (cvmatrix *)data;
73 imu_states_t state;
74
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);
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);
89 input->ReleaseMutex();
90
91 shmem->Write((char *)&state, sizeof(imu_states_t));
92 }
93}
94
95void SimuImu::Run(void) {
96 imu_states_t state;
97 ImuData *imuData;
98 GetDatas(&imuData);
99
100 if (dataRate == NULL) {
101 Thread::Err("not applicable for simulation part.\n");
102 return;
103 }
104
105 SetPeriodUS((uint32_t)(1000000. / dataRate->Value()));
106
107 while (!ToBeStopped()) {
108 WaitPeriod();
109
110 if (dataRate->ValueChanged() == true) {
111 SetPeriodUS((uint32_t)(1000000. / dataRate->Value()));
112 }
113 shmem->Read((char *)&state, sizeof(imu_states_t));
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 //we do not need rotation in simulation
120 /*
121 ApplyRotation(angRate);
122 ApplyRotation(quaternion);
123 ApplyRotation(rawAcc);
124 ApplyRotation(rawMag);
125 ApplyRotation(rawGyr);*/
126 ahrsData->SetQuaternionAndAngularRates(quaternion,angRate);
127 imuData->SetRawAccMagAndGyr(rawAcc,rawMag,rawGyr);
128 imuData->SetDataTime(GetTime());
129 ahrsData->SetDataTime(GetTime());
130 ProcessUpdate(ahrsData);
131 }
132}
133
134} // end namespace sensor
135} // end namespace flair
Note: See TracBrowser for help on using the repository browser.