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

Last change on this file since 202 was 202, checked in by Sanahuja Guillaume, 4 years ago

modifs usleep

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