source: flair-src/trunk/lib/FlairSensorActuator/src/EmulatedController.cpp @ 140

Last change on this file since 140 was 137, checked in by Sanahuja Guillaume, 5 years ago

singleton manager

File size: 5.9 KB
Line 
1//  created:    2015/03/30
2//  filename:   EmulatedController.cpp
3//
4//  author:     Gildas Bayard
5//              Copyright Heudiasyc UMR UTC/CNRS 7253
6//
7//  version:    $Id: $
8//
9//  purpose:    class that emulate a remote control.
10//              Typical use case: run a demo without any reliable communication channel
11//
12/*********************************************************************/
13#include "EmulatedController.h"
14#include <Controller.h>
15#include <TcpSocket.h>
16#include <Socket.h>
17#include <cstring>
18#include <string>
19#include <cvmatrix.h>
20#include <stdexcept>
21
22#include <unistd.h> // for sleep (debug)
23
24using namespace flair::core;
25using namespace flair::gui;
26using std::string;
27
28namespace flair { namespace sensor {
29
30EmulatedController::EmulatedController(string name,uint8_t priority) :
31    TargetController(name,priority) {
32}
33
34EmulatedController::~EmulatedController() {
35    //TargetController calls TargetEthController methods in its run
36    //we must stop the thread now
37    SafeStop();
38    Join();
39}
40
41bool EmulatedController::IsConnected() const {
42    return true;
43}
44
45bool EmulatedController::IsDataFrameReady(){
46    return true;
47}
48
49template<typename T> void EmulatedController::fillVectorNoMutex(cvmatrix &vector,T data[],unsigned int size) {
50    for (unsigned int i=0; i<size; i++) {
51        vector.SetValueNoMutex(i,0,data[i]);
52    }
53}
54
55void EmulatedController::fillVectorNoMutex(cvmatrix &destination,cvmatrix &source,unsigned int size) {
56    for (unsigned int i=0; i<size; i++) {
57        destination.SetValueNoMutex(i,0,source.Value(i,0));
58    }
59}
60
61void EmulatedController::StepData::Print() {
62    Printf("<StepData %s, duration=%d, axisData={%f,%f,%f,%f}, buttonData={%c,%c,%c,%c,%c,%c,%c,%c,%c,%c,%c,%c,%c,%c,%c,%c} />\n",description.c_str(),durationMs,
63      axisData->Value(0,0),axisData->Value(1,0),axisData->Value(2,0),axisData->Value(3,0),
64      buttonData->Value(0,0)?'X':'-',buttonData->Value(1,0)?'X':'-',buttonData->Value(2,0)?'X':'-',buttonData->Value(3,0)?'X':'-',
65      buttonData->Value(4,0)?'X':'-',buttonData->Value(5,0)?'X':'-',buttonData->Value(6,0)?'X':'-',buttonData->Value(7,0)?'X':'-',
66      buttonData->Value(8,0)?'X':'-',buttonData->Value(9,0)?'X':'-',buttonData->Value(10,0)?'X':'-',buttonData->Value(11,0)?'X':'-',
67      buttonData->Value(12,0)?'X':'-',buttonData->Value(13,0)?'X':'-',buttonData->Value(14,0)?'X':'-',buttonData->Value(15,0)?'X':'-'
68    );
69}
70
71void EmulatedController::AddStep(unsigned int durationMs,string description,uint16_t buttonPressed, float leftAxisX, float leftAxisY, float rightAxisX, float rightAxisY) {
72    cvmatrix *axisMatrix=new cvmatrix((IODevice *)this,4,1,floatType);
73    axisMatrix->SetValueNoMutex(0,0,leftAxisX);
74    axisMatrix->SetValueNoMutex(1,0,leftAxisY);
75    axisMatrix->SetValueNoMutex(2,0,rightAxisX);
76    axisMatrix->SetValueNoMutex(3,0,rightAxisY);
77
78    cvmatrix *buttonMatrix=new cvmatrix((IODevice *)this,16,1,SignedIntegerType(8));
79    if (buttonPressed&(uint16_t)ButtonType::start) buttonMatrix->SetValueNoMutex(0,0,1);
80    if (buttonPressed&(uint16_t)ButtonType::select) buttonMatrix->SetValueNoMutex(1,0,1);
81    if (buttonPressed&(uint16_t)ButtonType::square) buttonMatrix->SetValueNoMutex(2,0,1);
82    if (buttonPressed&(uint16_t)ButtonType::triangle) buttonMatrix->SetValueNoMutex(3,0,1);
83    if (buttonPressed&(uint16_t)ButtonType::circle) buttonMatrix->SetValueNoMutex(4,0,1);
84    if (buttonPressed&(uint16_t)ButtonType::cross) buttonMatrix->SetValueNoMutex(5,0,1);
85    if (buttonPressed&(uint16_t)ButtonType::left1) buttonMatrix->SetValueNoMutex(6,0,1);
86    if (buttonPressed&(uint16_t)ButtonType::left2) buttonMatrix->SetValueNoMutex(7,0,1);
87    if (buttonPressed&(uint16_t)ButtonType::left3) buttonMatrix->SetValueNoMutex(8,0,1);
88    if (buttonPressed&(uint16_t)ButtonType::right1) buttonMatrix->SetValueNoMutex(9,0,1);
89    if (buttonPressed&(uint16_t)ButtonType::right2) buttonMatrix->SetValueNoMutex(10,0,1);
90    if (buttonPressed&(uint16_t)ButtonType::right3) buttonMatrix->SetValueNoMutex(11,0,1);
91    if (buttonPressed&(uint16_t)ButtonType::up) buttonMatrix->SetValueNoMutex(12,0,1);
92    if (buttonPressed&(uint16_t)ButtonType::down) buttonMatrix->SetValueNoMutex(13,0,1);
93    if (buttonPressed&(uint16_t)ButtonType::left) buttonMatrix->SetValueNoMutex(14,0,1);
94    if (buttonPressed&(uint16_t)ButtonType::right) buttonMatrix->SetValueNoMutex(15,0,1);
95
96    StepData step={durationMs,axisMatrix,buttonMatrix,description};
97    //step.Print();
98    steps.push_back(step);
99}
100
101void EmulatedController::ComputeControllerData(DataType dataType, cvmatrix &data) {
102    static Time startStepTime=GetTime();
103
104    Time now=GetTime();
105
106    axis->GetMutex();
107    button->GetMutex();
108
109    if (steps.empty()) {
110        if (dataType==DataType::axis) {
111            float values[]={0.,0.,0.,0.};
112            fillVectorNoMutex(data,values,4);
113        }
114        if (dataType==DataType::button) {
115            int values[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
116            fillVectorNoMutex(data,values,16);
117        }
118    } else {
119        StepData currentStep=steps.front();
120        if (dataType==DataType::axis) fillVectorNoMutex(data,*(currentStep.axisData),4);
121        if (dataType==DataType::button) fillVectorNoMutex(data,*(currentStep.buttonData),16);
122        if (now-startStepTime>(Time)currentStep.durationMs*1000*1000) {
123            Thread::Info("Step '%s' done\n",currentStep.description.c_str());
124            startStepTime=now;
125            steps.pop_front();
126        }
127    }
128
129    button->ReleaseMutex();
130    axis->ReleaseMutex();
131}
132
133void EmulatedController::AcquireAxisData(core::cvmatrix &axis) {
134    ComputeControllerData(DataType::axis,axis);
135}
136
137void EmulatedController::AcquireButtonData(core::cvmatrix &button) {
138    ComputeControllerData(DataType::button,button);
139}
140
141bool EmulatedController::ProcessMessage(Message *msg) {
142    return false;
143}
144
145bool EmulatedController::ControllerInitialization() {
146    axisNumber=4;
147    buttonNumber=16;
148    return true;
149}
150
151} // end namespace sensor
152} // end namespace flair
Note: See TracBrowser for help on using the repository browser.