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 <SharedMem.h>
|
---|
19 | #include <sstream>
|
---|
20 | #include <string.h>
|
---|
21 |
|
---|
22 | using std::string;
|
---|
23 | using std::ostringstream;
|
---|
24 | using namespace flair::core;
|
---|
25 |
|
---|
26 | namespace flair {
|
---|
27 | namespace actuator {
|
---|
28 |
|
---|
29 |
|
---|
30 | SimuBldc::SimuBldc(const Object *parent, string name, uint8_t motors_count,
|
---|
31 | uint32_t modelId,uint32_t deviceId)
|
---|
32 | : IODevice(parent, name) {
|
---|
33 | shmem =
|
---|
34 | new SharedMem(this, ShMemName(modelId, deviceId), motors_count * sizeof(float)+sizeof(Time));
|
---|
35 |
|
---|
36 | buf=(char*)malloc(motors_count * sizeof(float)+sizeof(Time));
|
---|
37 | if(buf==NULL) {
|
---|
38 | Err("error creating buffer\n");
|
---|
39 | return;
|
---|
40 | }
|
---|
41 |
|
---|
42 | // reset values
|
---|
43 | float *values=(float*)buf;
|
---|
44 | for (int i = 0; i < motors_count; i++) values[i] = 0;
|
---|
45 | Time time=GetTime();
|
---|
46 | memcpy(buf+motors_count * sizeof(float),&time,sizeof(Time));
|
---|
47 |
|
---|
48 | shmem->Write(buf, motors_count * sizeof(float)+sizeof(Time));
|
---|
49 |
|
---|
50 | SetIsReady(true);
|
---|
51 | this->motors_count=motors_count;
|
---|
52 | }
|
---|
53 |
|
---|
54 | SimuBldc::~SimuBldc() {
|
---|
55 | if(buf!=NULL) free(buf);
|
---|
56 | }
|
---|
57 |
|
---|
58 | string SimuBldc::ShMemName(uint32_t modelId,uint32_t deviceId) {
|
---|
59 | ostringstream dev_name;
|
---|
60 | dev_name << "simu" << modelId << "_bldc_" << deviceId;
|
---|
61 | return dev_name.str().c_str();
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | void SimuBldc::GetSpeeds(float *value,Time* time) const {
|
---|
66 | float *values=(float*)buf;
|
---|
67 | shmem->Read(buf, motors_count * sizeof(float)+sizeof(Time));
|
---|
68 | memcpy(time,buf+motors_count * sizeof(float),sizeof(Time));
|
---|
69 |
|
---|
70 | for (int i = 0; i < motors_count; i++) {
|
---|
71 | value[i] = values[i];
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | } // end namespace sensor
|
---|
76 | } // end namespace flair
|
---|