// %flair:license{ // This file is part of the Flair framework distributed under the // CECILL-C License, Version 1.0. // %flair:license} // created: 2014/02/07 // filename: SimuImu.cpp // // author: Guillaume Sanahuja // Copyright Heudiasyc UMR UTC/CNRS 7253 // // version: $Id: $ // // purpose: Class for a simulation imu // // /*********************************************************************/ #include "SimuImu.h" #include #include #include #include #include #include #include #include using std::string; using std::ostringstream; using namespace flair::core; using namespace flair::gui; namespace flair { namespace sensor { SimuImu::SimuImu(string name, uint32_t modelId,uint32_t deviceId, uint8_t priority) : Imu(name), Thread(getFrameworkManager(), name, priority) { dataRate = new SpinBox(GetGroupBox()->NewRow(), "data rate", " Hz", 1, 500, 1, 200); ahrsData = new AhrsData((Imu *)this); shmem = new SharedMem((Thread *)this, ShMemName(modelId, deviceId), sizeof(imu_states_t)); SetIsReady(true); } SimuImu::SimuImu(const IODevice *parent, string name, uint32_t modelId,uint32_t deviceId) : Imu(parent,name), Thread(parent, name, 0) { dataRate = NULL; shmem = new SharedMem((Thread *)this, ShMemName(modelId, deviceId), sizeof(imu_states_t)); SetIsReady(true); } SimuImu::~SimuImu() { SafeStop(); Join(); } string SimuImu::ShMemName(uint32_t modelId,uint32_t deviceId) { ostringstream dev_name; dev_name << "simu" << modelId << "_imu_" << deviceId; return dev_name.str().c_str(); } void SimuImu::UpdateFrom(const io_data *data) { if (data != NULL) { cvmatrix *input = (cvmatrix *)data; imu_states_t state; input->GetMutex(); state.q0 = input->ValueNoMutex(0, 0); state.q1 = input->ValueNoMutex(1, 0); state.q2 = input->ValueNoMutex(2, 0); state.q3 = input->ValueNoMutex(3, 0); state.wx = input->ValueNoMutex(7, 0); state.wy = input->ValueNoMutex(8, 0); state.wz = input->ValueNoMutex(9, 0); input->ReleaseMutex(); shmem->Write((char *)&state, sizeof(imu_states_t)); } } void SimuImu::Run(void) { imu_states_t state; ImuData *imuData; GetDatas(&imuData); if (dataRate == NULL) { Thread::Err("not applicable for simulation part.\n"); return; } SetPeriodUS((uint32_t)(1000000. / dataRate->Value())); while (!ToBeStopped()) { WaitPeriod(); if (dataRate->ValueChanged() == true) { SetPeriodUS((uint32_t)(1000000. / dataRate->Value())); } shmem->Read((char *)&state, sizeof(imu_states_t)); ahrsData->SetQuaternionAndAngularRates( Quaternion(state.q0, state.q1, state.q2, state.q3), Vector3Df(state.wx, state.wy, state.wz)); imuData->SetDataTime(GetTime()); ahrsData->SetDataTime(GetTime()); UpdateImu(); ProcessUpdate(ahrsData); } } } // end namespace sensor } // end namespace flair