// %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 { //Construct a SimuImu. Control part. SimuImu::SimuImu(string name, uint32_t modelId,uint32_t deviceId, uint8_t priority) : Imu(name,false), 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); } //Construct a SimuImu. Simulation part. 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) { Matrix *input = (Matrix *)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); state.ax = input->ValueNoMutex(13, 0); state.ay = input->ValueNoMutex(14, 0); state.az = input->ValueNoMutex(15, 0); state.mx = input->ValueNoMutex(16, 0); state.my = input->ValueNoMutex(17, 0); state.mz = input->ValueNoMutex(18, 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)); Quaternion quaternion(state.q0, state.q1, state.q2, state.q3); Vector3Df angRate(state.wx, state.wy, state.wz); Vector3Df rawAcc(state.ax, state.ay, state.az); Vector3Df rawMag(state.mx, state.my, state.mz); Vector3Df rawGyr(state.wx, state.wy, state.wz); //we do not need rotation in simulation /* ApplyRotation(angRate); ApplyRotation(quaternion); ApplyRotation(rawAcc); ApplyRotation(rawMag); ApplyRotation(rawGyr);*/ ahrsData->SetQuaternionAndAngularRates(quaternion,angRate); imuData->SetRawAccMagAndGyr(rawAcc,rawMag,rawGyr); imuData->SetDataTime(GetTime()); ahrsData->SetDataTime(GetTime()); ProcessUpdate(ahrsData); } } } // end namespace sensor } // end namespace flair