[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: SimuBldc.cpp
|
---|
| 7 | //
|
---|
| 8 | // author: Guillaume Sanahuja
|
---|
| 9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 10 | //
|
---|
| 11 | // version: $Id: $
|
---|
| 12 | //
|
---|
| 13 | // purpose: Class for a simulation bldc
|
---|
| 14 | //
|
---|
| 15 | //
|
---|
| 16 | /*********************************************************************/
|
---|
| 17 | #include "SimuBldc.h"
|
---|
| 18 | #include <FrameworkManager.h>
|
---|
| 19 | #include <GridLayout.h>
|
---|
| 20 | #include <DoubleSpinBox.h>
|
---|
| 21 | #include <GroupBox.h>
|
---|
| 22 | #include <SharedMem.h>
|
---|
[214] | 23 | #include <Matrix.h>
|
---|
[3] | 24 | #include <sstream>
|
---|
[214] | 25 | #include <string.h>
|
---|
[3] | 26 |
|
---|
| 27 | using std::string;
|
---|
| 28 | using std::ostringstream;
|
---|
| 29 | using namespace flair::core;
|
---|
| 30 | using namespace flair::gui;
|
---|
| 31 |
|
---|
[15] | 32 | namespace flair {
|
---|
| 33 | namespace actuator {
|
---|
[3] | 34 |
|
---|
[15] | 35 | SimuBldc::SimuBldc(const IODevice *parent, Layout *layout, string name,
|
---|
[158] | 36 | uint8_t motors_count, uint32_t modelId,uint32_t deviceId)
|
---|
[15] | 37 | : Bldc(parent, layout, name, motors_count) {
|
---|
| 38 | shmem =
|
---|
[214] | 39 | new SharedMem(this, ShMemName(modelId, deviceId), motors_count * sizeof(float)+sizeof(Time));
|
---|
[3] | 40 |
|
---|
[15] | 41 | GroupBox *groupbox = new GroupBox(layout->NewRow(), "simubldc");
|
---|
| 42 | k = new DoubleSpinBox(groupbox->NewRow(), "k driver:", 0, 10000, 1);
|
---|
[157] | 43 |
|
---|
[214] | 44 | buf=(char*)malloc(motors_count * sizeof(float)+sizeof(Time));
|
---|
| 45 | if(buf==NULL) {
|
---|
| 46 | Err("error creating buffer\n");
|
---|
| 47 | return;
|
---|
| 48 | }
|
---|
[157] | 49 | SetIsReady(true);
|
---|
[3] | 50 | }
|
---|
| 51 |
|
---|
[15] | 52 | SimuBldc::SimuBldc(const Object *parent, string name, uint8_t motors_count,
|
---|
[158] | 53 | uint32_t modelId,uint32_t deviceId)
|
---|
[15] | 54 | : Bldc(parent, name, motors_count) {
|
---|
| 55 | shmem =
|
---|
[214] | 56 | new SharedMem(this, ShMemName(modelId, deviceId), motors_count * sizeof(float)+sizeof(Time));
|
---|
| 57 |
|
---|
| 58 | buf=(char*)malloc(motors_count * sizeof(float)+sizeof(Time));
|
---|
| 59 | if(buf==NULL) {
|
---|
| 60 | Err("error creating buffer\n");
|
---|
| 61 | return;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[15] | 64 | // reset values
|
---|
[214] | 65 | float *values=(float*)buf;
|
---|
| 66 | for (int i = 0; i < motors_count; i++) values[i] = 0;
|
---|
| 67 | Time time=GetTime();
|
---|
| 68 | memcpy(buf+motors_count * sizeof(float),&time,sizeof(Time));
|
---|
[3] | 69 |
|
---|
[214] | 70 | shmem->Write(buf, motors_count * sizeof(float)+sizeof(Time));
|
---|
[157] | 71 |
|
---|
| 72 | SetIsReady(true);
|
---|
[3] | 73 | }
|
---|
| 74 |
|
---|
[214] | 75 | SimuBldc::~SimuBldc() {
|
---|
| 76 | if(buf!=NULL) free(buf);
|
---|
| 77 | }
|
---|
[3] | 78 |
|
---|
[158] | 79 | string SimuBldc::ShMemName(uint32_t modelId,uint32_t deviceId) {
|
---|
| 80 | ostringstream dev_name;
|
---|
| 81 | dev_name << "simu" << modelId << "_bldc_" << deviceId;
|
---|
| 82 | return dev_name.str().c_str();
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[15] | 85 | void SimuBldc::SetMotors(float *value) {
|
---|
[214] | 86 | float *values=(float*)buf;
|
---|
| 87 | for (int i = 0; i < MotorsCount(); i++) values[i] = k->Value() * value[i];
|
---|
| 88 | Time time=GetTime();
|
---|
| 89 | memcpy(buf+MotorsCount() * sizeof(float),&time,sizeof(Time));
|
---|
[3] | 90 |
|
---|
[214] | 91 | shmem->Write(buf, MotorsCount() * sizeof(float)+sizeof(Time));
|
---|
[3] | 92 |
|
---|
[15] | 93 | // on prend une fois pour toute le mutex et on fait des accès directs
|
---|
| 94 | output->GetMutex();
|
---|
[214] | 95 | for (int i = 0; i < MotorsCount(); i++) {
|
---|
[15] | 96 | output->SetValueNoMutex(i, 0, values[i]);
|
---|
[214] | 97 | }
|
---|
[15] | 98 | output->ReleaseMutex();
|
---|
[3] | 99 | }
|
---|
| 100 |
|
---|
[214] | 101 | void SimuBldc::GetSpeeds(float *value,Time* time) const {
|
---|
| 102 | float *values=(float*)buf;
|
---|
| 103 | shmem->Read(buf, MotorsCount() * sizeof(float)+sizeof(Time));
|
---|
| 104 | memcpy(time,buf+MotorsCount() * sizeof(float),sizeof(Time));
|
---|
[3] | 105 |
|
---|
[214] | 106 | for (int i = 0; i < MotorsCount(); i++) {
|
---|
[15] | 107 | value[i] = values[i];
|
---|
[214] | 108 | }
|
---|
[3] | 109 | }
|
---|
| 110 |
|
---|
| 111 | } // end namespace sensor
|
---|
| 112 | } // end namespace flair
|
---|