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