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

Last change on this file since 330 was 249, checked in by Bayard Gildas, 4 years ago

bug #36 solved

File size: 4.4 KB
RevLine 
[3]1// %flair:license{
[15]2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
[3]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>
[214]22#include <Matrix.h>
[3]23
24#include <cstring>
25#include <string>
26
27using namespace flair::core;
28using namespace flair::gui;
29using std::string;
30
[15]31namespace flair {
32namespace sensor {
[3]33
[137]34TargetController::TargetController(string name,uint8_t priority) :
35 Thread(getFrameworkManager(),name,priority),IODevice(getFrameworkManager(),name) {
[38]36 main_tab=new Tab(getFrameworkManager()->GetTabWidget(),name);
37 TabWidget* tab=new TabWidget(main_tab->NewRow(),name);
38 setup_tab=new Tab(tab,"Reglages");
[3]39}
40
41TargetController::~TargetController() {
[15]42 SafeStop();
43 Join();
[3]44}
45
46std::string TargetController::GetAxisName(unsigned int axisId) const {
[15]47 return string("axis") + std::to_string(axisId);
[3]48}
49
50std::string TargetController::GetButtonName(unsigned int buttonId) const {
[15]51 return string("button") + std::to_string(buttonId);
[3]52}
53
54bool TargetController::SetLedOn(unsigned int ledId) {
[15]55 if (!IsControllerActionSupported(ControllerAction::SetLedOn))
56 return false;
57 SwitchLedMessage *msgLed = new SwitchLedMessage(true, ledId);
58 changeStateQueue.push(msgLed);
59 return true;
[3]60}
61
62bool TargetController::SetLedOff(unsigned int ledId) {
[15]63 if (!IsControllerActionSupported(ControllerAction::SetLedOn))
64 return false;
65 SwitchLedMessage *msgLed = new SwitchLedMessage(false, ledId);
66 changeStateQueue.push(msgLed);
67 return true;
[3]68}
69
[15]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;
[3]79}
80
[15]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;
[3]89}
90
[15]91float TargetController::GetAxisValue(unsigned int axisId) const {
92 // TODO we'd better throw an exception here
93 if (axis == NULL)
94 return 0;
[3]95
[15]96 axis->GetMutex();
97 float axisValue = axis->Value(axisId, 0);
98 axis->ReleaseMutex();
99 return axisValue;
[3]100}
101
[15]102bool TargetController::IsButtonPressed(unsigned int buttonId) const {
103 // TODO we'd better throw an exception here
104 if (button == NULL)
105 return false;
[3]106
[15]107 button->GetMutex();
108 bool buttonValue = button->Value(buttonId, 0);
109 button->ReleaseMutex();
110 return buttonValue;
[3]111}
112
113void TargetController::Run() {
[42]114
[15]115 Message *message;
[3]116
[186]117 //if (getFrameworkManager()->ErrorOccured() || !ControllerInitialization()) {
118 if (!ControllerInitialization()) {
[249]119// SafeStop();
[15]120 Thread::Err("An error occured, we don't proceed with the loop.\n");
121 } else {
[214]122 axis = new Matrix((IODevice *)this, axisNumber, 1, floatType);
[15]123 button =
[214]124 new Matrix((IODevice *)this, buttonNumber, 1, SignedIntegerType(8));
[3]125
[249]126 SetPeriodMS(20); //50Hz
[15]127 while (!ToBeStopped()) {
128 // Thread::Info("Debug: entering acquisition loop\n");
129 if (IsDataFrameReady()) {
130 // Thread::Info("Debug: data frame is ready\n");
[3]131
[15]132 AcquireAxisData(*axis);
133 AcquireButtonData(*button);
[3]134
[15]135 // send the data
136 axis->SetDataTime(GetTime());
137 ProcessUpdate(axis);
[3]138
[15]139 // send pending controller state change request(s)
140 while (changeStateQueue.size() != 0) {
141 message = changeStateQueue.front();
142 if (ProcessMessage(message)) {
143 changeStateQueue.pop();
144 delete message;
145 }
[3]146 }
[15]147 }
[38]148 WaitPeriod();
[3]149 }
[15]150 }
[3]151}
152
[15]153Tab *TargetController::GetTab() const { return setup_tab; }
[3]154
155} // end namespace sensor
156} // end namespace flair
Note: See TracBrowser for help on using the repository browser.