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