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

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

simu gps

File size: 2.9 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(const FrameworkManager *parent, string name, uint32_t dev_id,
37 uint8_t priority)
38 : Imu(parent, name), Thread(parent, name, priority) {
39 dataRate =
40 new SpinBox(GetGroupBox()->NewRow(), "data rate", " Hz", 1, 500, 1, 200);
41 ahrsData = new AhrsData((Imu *)this);
42
43 ostringstream dev_name;
44 dev_name << "simu_imu_" << dev_id;
45 shmem = new SharedMem((Thread *)this, dev_name.str().c_str(),
46 sizeof(imu_states_t));
47}
48
49SimuImu::SimuImu(const IODevice *parent, string name, uint32_t dev_id)
50 : Imu(parent, name), Thread(parent, name, 0) {
51 dataRate = NULL;
52
53 ostringstream dev_name;
54 dev_name << "simu_imu_" << dev_id;
55 shmem = new SharedMem((Thread *)this, dev_name.str().c_str(),
56 sizeof(imu_states_t));
57}
58
59SimuImu::~SimuImu() {
60 SafeStop();
61 Join();
62}
63
64void SimuImu::UpdateFrom(const io_data *data) {
65 if (data != NULL) {
66 cvmatrix *input = (cvmatrix *)data;
67 imu_states_t state;
68
69 input->GetMutex();
70 state.q0 = input->ValueNoMutex(0, 0);
71 state.q1 = input->ValueNoMutex(1, 0);
72 state.q2 = input->ValueNoMutex(2, 0);
73 state.q3 = input->ValueNoMutex(3, 0);
74 state.wx = input->ValueNoMutex(7, 0);
75 state.wy = input->ValueNoMutex(8, 0);
76 state.wz = input->ValueNoMutex(9, 0);
77 input->ReleaseMutex();
78
79 shmem->Write((char *)&state, sizeof(imu_states_t));
80 }
81}
82
83void SimuImu::Run(void) {
84 imu_states_t state;
85 ImuData *imuData;
86 GetDatas(&imuData);
87
88 if (dataRate == NULL) {
89 Thread::Err("not applicable for simulation part.\n");
90 return;
91 }
92
93 SetPeriodUS((uint32_t)(1000000. / dataRate->Value()));
94
95 while (!ToBeStopped()) {
96 WaitPeriod();
97
98 if (dataRate->ValueChanged() == true) {
99 SetPeriodUS((uint32_t)(1000000. / dataRate->Value()));
100 }
101
102 shmem->Read((char *)&state, sizeof(imu_states_t));
103 ahrsData->SetQuaternionAndAngularRates(
104 Quaternion(state.q0, state.q1, state.q2, state.q3),
105 Vector3D(state.wx, state.wy, state.wz));
106
107 imuData->SetDataTime(GetTime());
108 ahrsData->SetDataTime(GetTime());
109
110 UpdateImu();
111 ProcessUpdate(ahrsData);
112 }
113}
114
115} // end namespace sensor
116} // end namespace flair
Note: See TracBrowser for help on using the repository browser.