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

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

matrix

File size: 3.1 KB
Line 
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: 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>
23#include <Matrix.h>
24#include <sstream>
25#include <string.h>
26
27using std::string;
28using std::ostringstream;
29using namespace flair::core;
30using namespace flair::gui;
31
32namespace flair {
33namespace actuator {
34
35SimuBldc::SimuBldc(const IODevice *parent, Layout *layout, string name,
36 uint8_t motors_count, uint32_t modelId,uint32_t deviceId)
37 : Bldc(parent, layout, name, motors_count) {
38 shmem =
39 new SharedMem(this, ShMemName(modelId, deviceId), motors_count * sizeof(float)+sizeof(Time));
40
41 GroupBox *groupbox = new GroupBox(layout->NewRow(), "simubldc");
42 k = new DoubleSpinBox(groupbox->NewRow(), "k driver:", 0, 10000, 1);
43
44 buf=(char*)malloc(motors_count * sizeof(float)+sizeof(Time));
45 if(buf==NULL) {
46 Err("error creating buffer\n");
47 return;
48 }
49 SetIsReady(true);
50}
51
52SimuBldc::SimuBldc(const Object *parent, string name, uint8_t motors_count,
53 uint32_t modelId,uint32_t deviceId)
54 : Bldc(parent, name, motors_count) {
55 shmem =
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
64 // reset values
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));
69
70 shmem->Write(buf, motors_count * sizeof(float)+sizeof(Time));
71
72 SetIsReady(true);
73}
74
75SimuBldc::~SimuBldc() {
76 if(buf!=NULL) free(buf);
77}
78
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
85void SimuBldc::SetMotors(float *value) {
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));
90
91 shmem->Write(buf, MotorsCount() * sizeof(float)+sizeof(Time));
92
93 // on prend une fois pour toute le mutex et on fait des accès directs
94 output->GetMutex();
95 for (int i = 0; i < MotorsCount(); i++) {
96 output->SetValueNoMutex(i, 0, values[i]);
97 }
98 output->ReleaseMutex();
99}
100
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));
105
106 for (int i = 0; i < MotorsCount(); i++) {
107 value[i] = values[i];
108 }
109}
110
111} // end namespace sensor
112} // end namespace flair
Note: See TracBrowser for help on using the repository browser.