source: flair-src/trunk/lib/FlairSensorActuator/src/SimulatedBldc.cpp @ 330

Last change on this file since 330 was 286, checked in by Sanahuja Guillaume, 3 years ago

draw vrpn axis in simulator

File size: 2.2 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:   SimulatedBldc.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#ifdef CORE2_64
18
19#include "SimulatedBldc.h"
20#include <FrameworkManager.h>
21#include <GridLayout.h>
22#include <DoubleSpinBox.h>
23#include <GroupBox.h>
24#include <SharedMem.h>
25#include <Matrix.h>
26#include <sstream>
27#include <string.h>
28
29using std::string;
30using std::ostringstream;
31using namespace flair::core;
32using namespace flair::gui;
33
34namespace flair {
35namespace actuator {
36
37SimulatedBldc::SimulatedBldc(const IODevice *parent, Layout *layout, string name,
38                   uint8_t motors_count, uint32_t modelId,uint32_t deviceId)
39    : Bldc(parent, layout, name, motors_count) {
40  shmem =
41      new SharedMem(this, ShMemName(modelId, deviceId), motors_count * sizeof(float)+sizeof(Time));
42
43  GroupBox *groupbox = new GroupBox(layout->NewRow(), "simubldc");
44  k = new DoubleSpinBox(groupbox->NewRow(), "k driver:", 0, 10000, 1);
45 
46  buf=(char*)malloc(motors_count * sizeof(float)+sizeof(Time));
47  if(buf==NULL) {
48    Err("error creating buffer\n");
49    return;
50  }
51  SetIsReady(true);
52}
53
54SimulatedBldc::~SimulatedBldc() {
55  if(buf!=NULL) free(buf);
56}
57
58string SimulatedBldc::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
64void SimulatedBldc::SetMotors(float *value) {
65  float *values=(float*)buf;
66  for (int i = 0; i < MotorsCount(); i++) values[i] = k->Value() * value[i];
67  Time time=GetTime();
68  memcpy(buf+MotorsCount() * sizeof(float),&time,sizeof(Time));
69
70  shmem->Write(buf, MotorsCount() * sizeof(float)+sizeof(Time));
71
72  // on prend une fois pour toute le mutex et on fait des accès directs
73  output->GetMutex();
74  for (int i = 0; i < MotorsCount(); i++) {
75    output->SetValueNoMutex(i, 0, values[i]);
76  }
77  output->ReleaseMutex();
78}
79
80} // end namespace sensor
81} // end namespace flair
82
83#endif
Note: See TracBrowser for help on using the repository browser.