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