| 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: SimuUs.cpp
|
---|
| 7 | //
|
---|
| 8 | // author: Guillaume Sanahuja
|
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 10 | //
|
---|
| 11 | // version: $Id: $
|
---|
| 12 | //
|
---|
| 13 | // purpose: Class for a simulation us
|
---|
| 14 | //
|
---|
| 15 | //
|
---|
| 16 | /*********************************************************************/
|
---|
| 17 | #include "SimuUs.h"
|
---|
| 18 | #include <FrameworkManager.h>
|
---|
| 19 | #include <ImuData.h>
|
---|
| 20 | #include <SpinBox.h>
|
---|
| 21 | #include <GroupBox.h>
|
---|
| 22 | #include <Matrix.h>
|
---|
| 23 | #include <SharedMem.h>
|
---|
| 24 | #include <sstream>
|
---|
| 25 |
|
---|
| 26 | using std::string;
|
---|
| 27 | using std::ostringstream;
|
---|
| 28 | using namespace flair::core;
|
---|
| 29 | using namespace flair::gui;
|
---|
| 30 |
|
---|
| 31 | namespace flair {
|
---|
| 32 | namespace sensor {
|
---|
| 33 |
|
---|
| 34 | //control part
|
---|
| 35 | SimuUs::SimuUs(string name, uint32_t modelId,uint32_t deviceId,
|
---|
| 36 | uint8_t priority)
|
---|
| 37 | : Thread(getFrameworkManager(), name, priority), UsRangeFinder( name) {
|
---|
| 38 | data_rate =
|
---|
| 39 | new SpinBox(GetGroupBox()->NewRow(), "data rate", " Hz", 1, 500, 1, 50);
|
---|
| 40 |
|
---|
| 41 | shmem = new SharedMem((Thread *)this, ShMemName(modelId, deviceId), sizeof(float));
|
---|
| 42 |
|
---|
| 43 | SetIsReady(true);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | //simulation part
|
---|
| 47 | SimuUs::SimuUs(const IODevice *parent, string name,uint32_t modelId,uint32_t deviceId)
|
---|
| 48 | : Thread(parent, name, 0), UsRangeFinder(parent,name) {
|
---|
| 49 | data_rate = NULL;
|
---|
| 50 |
|
---|
| 51 | shmem = new SharedMem((Thread *)this, ShMemName(modelId, deviceId), sizeof(float));
|
---|
| 52 |
|
---|
| 53 | SetIsReady(true);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | SimuUs::~SimuUs() {
|
---|
| 57 | SafeStop();
|
---|
| 58 | Join();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | string SimuUs::ShMemName(uint32_t modelId,uint32_t deviceId) {
|
---|
| 62 | ostringstream dev_name;
|
---|
| 63 | dev_name << "simu" << modelId << "_us_" << deviceId;
|
---|
| 64 | return dev_name.str().c_str();
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | void SimuUs::Run(void) {
|
---|
| 68 | float z;
|
---|
| 69 |
|
---|
| 70 | if (data_rate == NULL) {
|
---|
| 71 | Thread::Err("not applicable for simulation part.\n");
|
---|
| 72 | return;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | SetPeriodUS((uint32_t)(1000000. / data_rate->Value()));
|
---|
| 76 |
|
---|
| 77 | while (!ToBeStopped()) {
|
---|
| 78 | WaitPeriod();
|
---|
| 79 |
|
---|
| 80 | shmem->Read((char *)&z, sizeof(float));
|
---|
| 81 |
|
---|
| 82 | if (data_rate->ValueChanged() == true) {
|
---|
| 83 | SetPeriodUS((uint32_t)(1000000. / data_rate->Value()));
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | output->SetValue(0, 0, z);
|
---|
| 87 | output->SetDataTime(GetTime());
|
---|
| 88 | ProcessUpdate(output);
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | } // end namespace sensor
|
---|
| 93 | } // end namespace flair
|
---|