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

Last change on this file since 199 was 186, checked in by Sanahuja Guillaume, 7 years ago

maj imu

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 {
34namespace sensor {
35
36TargetController::TargetController(string name,uint8_t priority) :
37 Thread(getFrameworkManager(),name,priority),IODevice(getFrameworkManager(),name) {
38 main_tab=new Tab(getFrameworkManager()->GetTabWidget(),name);
39 TabWidget* tab=new TabWidget(main_tab->NewRow(),name);
40 setup_tab=new Tab(tab,"Reglages");
41}
42
43TargetController::~TargetController() {
44 SafeStop();
45 Join();
46}
47
48std::string TargetController::GetAxisName(unsigned int axisId) const {
49 return string("axis") + std::to_string(axisId);
50}
51
52std::string TargetController::GetButtonName(unsigned int buttonId) const {
53 return string("button") + std::to_string(buttonId);
54}
55
56bool TargetController::SetLedOn(unsigned int ledId) {
57 if (!IsControllerActionSupported(ControllerAction::SetLedOn))
58 return false;
59 SwitchLedMessage *msgLed = new SwitchLedMessage(true, ledId);
60 changeStateQueue.push(msgLed);
61 return true;
62}
63
64bool TargetController::SetLedOff(unsigned int ledId) {
65 if (!IsControllerActionSupported(ControllerAction::SetLedOn))
66 return false;
67 SwitchLedMessage *msgLed = new SwitchLedMessage(false, ledId);
68 changeStateQueue.push(msgLed);
69 return true;
70}
71
72bool TargetController::Rumble(unsigned int leftForce, unsigned int leftTimeout,
73 unsigned int rightForce,
74 unsigned int rightTimeout) {
75 if (!IsControllerActionSupported(ControllerAction::Rumble))
76 return false;
77 RumbleMessage *msgRumble =
78 new RumbleMessage(leftForce, leftTimeout, rightForce, rightTimeout);
79 changeStateQueue.push(msgRumble);
80 return true;
81}
82
83bool TargetController::FlashLed(unsigned int ledId, unsigned int onTimeout,
84 unsigned int offTimeout) {
85 if (!IsControllerActionSupported(ControllerAction::FlashLed))
86 return false;
87 FlashLedMessage *msgFlashLed =
88 new FlashLedMessage(ledId, onTimeout, offTimeout);
89 changeStateQueue.push(msgFlashLed);
90 return true;
91}
92
93float TargetController::GetAxisValue(unsigned int axisId) const {
94 // TODO we'd better throw an exception here
95 if (axis == NULL)
96 return 0;
97
98 axis->GetMutex();
99 float axisValue = axis->Value(axisId, 0);
100 axis->ReleaseMutex();
101 return axisValue;
102}
103
104bool TargetController::IsButtonPressed(unsigned int buttonId) const {
105 // TODO we'd better throw an exception here
106 if (button == NULL)
107 return false;
108
109 button->GetMutex();
110 bool buttonValue = button->Value(buttonId, 0);
111 button->ReleaseMutex();
112 return buttonValue;
113}
114
115void TargetController::Run() {
116 SetPeriodMS(20);; //50Hz
117
118 Message *message;
119
120 //if (getFrameworkManager()->ErrorOccured() || !ControllerInitialization()) {
121 if (!ControllerInitialization()) {
122 SafeStop();
123 Thread::Err("An error occured, we don't proceed with the loop.\n");
124 } else {
125
126 axis = new cvmatrix((IODevice *)this, axisNumber, 1, floatType);
127 button =
128 new cvmatrix((IODevice *)this, buttonNumber, 1, SignedIntegerType(8));
129
130 while (!ToBeStopped()) {
131 // Thread::Info("Debug: entering acquisition loop\n");
132 if (getFrameworkManager()->ConnectionLost() == true)
133 SafeStop();
134
135 if (IsDataFrameReady()) {
136 // Thread::Info("Debug: data frame is ready\n");
137
138 AcquireAxisData(*axis);
139 AcquireButtonData(*button);
140
141 // send the data
142 axis->SetDataTime(GetTime());
143 ProcessUpdate(axis);
144
145 // send pending controller state change request(s)
146
147 while (changeStateQueue.size() != 0) {
148 message = changeStateQueue.front();
149 if (ProcessMessage(message)) {
150 changeStateQueue.pop();
151 delete message;
152 }
153 }
154 }
155 WaitPeriod();
156 }
157 }
158}
159
160Tab *TargetController::GetTab() const { return setup_tab; }
161
162} // end namespace sensor
163} // end namespace flair
Note: See TracBrowser for help on using the repository browser.