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

Last change on this file since 157 was 157, checked in by Sanahuja Guillaume, 4 years ago

iadded isready to iodevice:
avoid problem of imu not ready in ardrone2

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