source: flair-src/trunk/lib/FlairSensorActuator/src/SimuBldc.cpp@ 219

Last change on this file since 219 was 214, checked in by Sanahuja Guillaume, 6 years ago

matrix

File size: 3.1 KB
RevLine 
[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
27using std::string;
28using std::ostringstream;
29using namespace flair::core;
30using namespace flair::gui;
31
[15]32namespace flair {
33namespace actuator {
[3]34
[15]35SimuBldc::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]52SimuBldc::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]75SimuBldc::~SimuBldc() {
76 if(buf!=NULL) free(buf);
77}
[3]78
[158]79string 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]85void 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]101void 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
Note: See TracBrowser for help on using the repository browser.