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

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

corrected simu/device id for sensors

File size: 3.0 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>
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
[15]33namespace flair {
34namespace sensor {
[3]35
[158]36SimuImu::SimuImu(string name, uint32_t modelId,uint32_t deviceId,
[15]37 uint8_t priority)
[137]38 : Imu(name), Thread(getFrameworkManager(), name, priority) {
[55]39 dataRate =
[15]40 new SpinBox(GetGroupBox()->NewRow(), "data rate", " Hz", 1, 500, 1, 200);
41 ahrsData = new AhrsData((Imu *)this);
[158]42
43 shmem = new SharedMem((Thread *)this, ShMemName(modelId, deviceId),
[15]44 sizeof(imu_states_t));
[157]45 SetIsReady(true);
[3]46}
47
[158]48SimuImu::SimuImu(const IODevice *parent, string name, uint32_t modelId,uint32_t deviceId)
[137]49 : Imu(parent,name), Thread(parent, name, 0) {
[55]50 dataRate = NULL;
[3]51
[158]52 shmem = new SharedMem((Thread *)this, ShMemName(modelId, deviceId),
[15]53 sizeof(imu_states_t));
[157]54 SetIsReady(true);
[3]55}
56
57SimuImu::~SimuImu() {
[15]58 SafeStop();
59 Join();
[3]60}
61
[158]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
[3]68void SimuImu::UpdateFrom(const io_data *data) {
[15]69 if (data != NULL) {
70 cvmatrix *input = (cvmatrix *)data;
71 imu_states_t state;
[3]72
[15]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();
[3]82
[15]83 shmem->Write((char *)&state, sizeof(imu_states_t));
84 }
[3]85}
86
87void SimuImu::Run(void) {
[15]88 imu_states_t state;
89 ImuData *imuData;
90 GetDatas(&imuData);
[3]91
[55]92 if (dataRate == NULL) {
[15]93 Thread::Err("not applicable for simulation part.\n");
94 return;
95 }
[3]96
[55]97 SetPeriodUS((uint32_t)(1000000. / dataRate->Value()));
[3]98
[15]99 while (!ToBeStopped()) {
100 WaitPeriod();
[3]101
[55]102 if (dataRate->ValueChanged() == true) {
103 SetPeriodUS((uint32_t)(1000000. / dataRate->Value()));
[15]104 }
[3]105
[15]106 shmem->Read((char *)&state, sizeof(imu_states_t));
107 ahrsData->SetQuaternionAndAngularRates(
108 Quaternion(state.q0, state.q1, state.q2, state.q3),
109 Vector3D(state.wx, state.wy, state.wz));
[3]110
[15]111 imuData->SetDataTime(GetTime());
112 ahrsData->SetDataTime(GetTime());
[3]113
[15]114 UpdateImu();
115 ProcessUpdate(ahrsData);
116 }
[3]117}
118
119} // end namespace sensor
120} // end namespace flair
Note: See TracBrowser for help on using the repository browser.