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

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

bug #36 solved

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 <Matrix.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
115  Message *message;
116
117  //if (getFrameworkManager()->ErrorOccured() || !ControllerInitialization()) {
118  if (!ControllerInitialization()) {
119//    SafeStop();
120    Thread::Err("An error occured, we don't proceed with the loop.\n");
121  } else {
122    axis = new Matrix((IODevice *)this, axisNumber, 1, floatType);
123    button =
124        new Matrix((IODevice *)this, buttonNumber, 1, SignedIntegerType(8));
125
126    SetPeriodMS(20); //50Hz
127    while (!ToBeStopped()) {
128      // Thread::Info("Debug: entering acquisition loop\n");
129      if (IsDataFrameReady()) {
130        // Thread::Info("Debug: data frame is ready\n");
131
132        AcquireAxisData(*axis);
133        AcquireButtonData(*button);
134
135        // send the data
136        axis->SetDataTime(GetTime());
137        ProcessUpdate(axis);
138
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          }
146        }
147      }
148      WaitPeriod();
149    }
150  }
151}
152
153Tab *TargetController::GetTab() const { return setup_tab; }
154
155} // end namespace sensor
156} // end namespace flair
Note: See TracBrowser for help on using the repository browser.