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

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

modif imu

File size: 3.1 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 input->ReleaseMutex();
82
83 shmem->Write((char *)&state, sizeof(imu_states_t));
84 }
85}
86
87void SimuImu::Run(void) {
88 imu_states_t state;
89 ImuData *imuData;
90 GetDatas(&imuData);
91
92 if (dataRate == NULL) {
93 Thread::Err("not applicable for simulation part.\n");
94 return;
95 }
96
97 SetPeriodUS((uint32_t)(1000000. / dataRate->Value()));
98
99 while (!ToBeStopped()) {
100 WaitPeriod();
101
102 if (dataRate->ValueChanged() == true) {
103 SetPeriodUS((uint32_t)(1000000. / dataRate->Value()));
104 }
105
106 shmem->Read((char *)&state, sizeof(imu_states_t));
107
108 Quaternion quaternion(state.q0, state.q1, state.q2, state.q3);
109 Vector3Df angRate(state.wx, state.wy, state.wz);
110 ApplyRotation(angRate);
111 ApplyRotation(quaternion);
112 ahrsData->SetQuaternionAndAngularRates(quaternion,angRate);
113
114 imuData->SetDataTime(GetTime());
115 ahrsData->SetDataTime(GetTime());
116
117 ProcessUpdate(ahrsData);
118 }
119}
120
121} // end namespace sensor
122} // end namespace flair
Note: See TracBrowser for help on using the repository browser.