source: flair-src/trunk/lib/FlairSensorActuator/src/TargetController.cpp @ 10

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

sensoractuator

File size: 4.5 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:    2015/03/30
6//  filename:   TargetController.cpp
7//
8//  author:     Gildas Bayard
9//              Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11//  version:    $Id: $
12//
13//  purpose:    Base class for target side remote controls
14//
15//
16/*********************************************************************/
17#include "TargetController.h"
18#include "Controller.h"
19#include <TabWidget.h>
20#include <Tab.h>
21#include <FrameworkManager.h>
22#include <cvmatrix.h>
23
24#include <cstring>
25#include <string>
26
27#include <unistd.h> //for usleep
28
29using namespace flair::core;
30using namespace flair::gui;
31using std::string;
32
33namespace flair
34{
35namespace sensor
36{
37
38TargetController::TargetController(const FrameworkManager* parent,string name,uint8_t priority) :
39    Thread(parent,name,priority),IODevice(parent,name) {
40    main_tab=new Tab(getFrameworkManager()->GetTabWidget(),name);
41    TabWidget* tab=new TabWidget(main_tab->NewRow(),name);
42    setup_tab=new Tab(tab,"Reglages");
43
44}
45
46TargetController::~TargetController() {
47    SafeStop();
48    Join();
49}
50
51std::string TargetController::GetAxisName(unsigned int axisId) const {
52    return string("axis")+std::to_string(axisId);
53}
54
55std::string TargetController::GetButtonName(unsigned int buttonId) const {
56    return string("button")+std::to_string(buttonId);
57}
58
59bool TargetController::SetLedOn(unsigned int ledId) {
60    if (!IsControllerActionSupported(ControllerAction::SetLedOn)) return false;
61    SwitchLedMessage *msgLed=new SwitchLedMessage(true,ledId);
62    changeStateQueue.push(msgLed);
63    return true;
64}
65
66bool TargetController::SetLedOff(unsigned int ledId) {
67    if (!IsControllerActionSupported(ControllerAction::SetLedOn)) return false;
68    SwitchLedMessage *msgLed=new SwitchLedMessage(false,ledId);
69    changeStateQueue.push(msgLed);
70    return true;
71}
72
73bool TargetController::Rumble(unsigned int leftForce,unsigned int leftTimeout,unsigned int rightForce,unsigned int rightTimeout) {
74    if (!IsControllerActionSupported(ControllerAction::Rumble)) return false;
75    RumbleMessage *msgRumble=new RumbleMessage(leftForce,leftTimeout,rightForce,rightTimeout);
76    changeStateQueue.push(msgRumble);
77    return true;
78}
79
80bool TargetController::FlashLed(unsigned int ledId,unsigned int onTimeout,unsigned int offTimeout){
81    if (!IsControllerActionSupported(ControllerAction::FlashLed)) return false;
82    FlashLedMessage *msgFlashLed=new FlashLedMessage(ledId,onTimeout,offTimeout);
83    changeStateQueue.push(msgFlashLed);
84    return true;
85}
86
87float TargetController::GetAxisValue(unsigned int axisId) const{
88    //TODO we'd better throw an exception here
89    if (axis==NULL) return 0;
90
91    axis->GetMutex();
92    float axisValue=axis->Value(axisId, 0);
93    axis->ReleaseMutex();
94    return axisValue;
95}
96
97bool TargetController::IsButtonPressed(unsigned int buttonId) const{
98    //TODO we'd better throw an exception here
99    if (button==NULL) return false;
100
101    button->GetMutex();
102    bool buttonValue=button->Value(buttonId, 0);
103    button->ReleaseMutex();
104    return buttonValue;
105}
106
107void TargetController::Run() {
108    Message *message;
109
110    if(getFrameworkManager()->ErrorOccured()||!ControllerInitialization()) {
111        SafeStop();
112        Thread::Err("An error occured, we don't proceed with the loop.\n");
113    } else {
114
115        axis=new cvmatrix((IODevice *)this,axisNumber,1,floatType);
116        button=new cvmatrix((IODevice *)this,buttonNumber,1,SignedIntegerType(8));
117
118        while(!ToBeStopped()) {
119            //Thread::Info("Debug: entering acquisition loop\n");
120            if(getFrameworkManager()->ConnectionLost()==true) SafeStop();
121
122            if (IsDataFrameReady()) {
123                //Thread::Info("Debug: data frame is ready\n");
124
125                AcquireAxisData(*axis);
126                AcquireButtonData(*button);
127
128                //send the data
129                axis->SetDataTime(GetTime());
130                ProcessUpdate(axis);
131
132                //send pending controller state change request(s)
133
134                while(changeStateQueue.size()!=0) {
135                    message=changeStateQueue.front();
136                    if (ProcessMessage(message)) {
137                        changeStateQueue.pop();
138                        delete message;
139                    }
140                }
141            } else {
142                //Thread::Info("Debug: relax...\n");
143                usleep(20000); //20ms
144            }
145        }
146    }
147}
148
149Tab* TargetController::GetTab() const {
150    return setup_tab;
151}
152
153} // end namespace sensor
154} // end namespace flair
Note: See TracBrowser for help on using the repository browser.