[376] | 1 | // created: 2020/12/09
|
---|
| 2 | // filename: CircleFollower.cpp
|
---|
| 3 | //
|
---|
| 4 | // author: Guillaume Sanahuja
|
---|
| 5 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
| 6 | //
|
---|
| 7 | // version: $Id: $
|
---|
| 8 | //
|
---|
| 9 | // purpose: demo cercle avec optitrack
|
---|
| 10 | //
|
---|
| 11 | //
|
---|
| 12 | /*********************************************************************/
|
---|
| 13 |
|
---|
| 14 | #include "CircleFollower.h"
|
---|
| 15 | #include <TargetController.h>
|
---|
| 16 | #include <GridLayout.h>
|
---|
| 17 | #include <PushButton.h>
|
---|
| 18 | #include <DataPlot1D.h>
|
---|
| 19 | #include <DataPlot2D.h>
|
---|
| 20 | #include <FrameworkManager.h>
|
---|
| 21 | #include <VrpnClient.h>
|
---|
| 22 | #include <MetaVrpnObject.h>
|
---|
| 23 | #include <TrajectoryGenerator2DCircle.h>
|
---|
| 24 | #include <Matrix.h>
|
---|
| 25 | #include <Tab.h>
|
---|
[379] | 26 | #include <TabWidget.h>
|
---|
| 27 | #include <DoubleSpinBox.h>
|
---|
[376] | 28 | #include <Pid.h>
|
---|
[379] | 29 | #include <Quaternion.h>
|
---|
| 30 | #include <Euler.h>
|
---|
[377] | 31 | #include <Ugv.h>
|
---|
| 32 | #include <UgvControls.h>
|
---|
[379] | 33 | #include <math.h>
|
---|
[376] | 34 |
|
---|
| 35 | using namespace std;
|
---|
| 36 | using namespace flair::core;
|
---|
| 37 | using namespace flair::gui;
|
---|
| 38 | using namespace flair::sensor;
|
---|
| 39 | using namespace flair::filter;
|
---|
| 40 | using namespace flair::meta;
|
---|
[377] | 41 | using namespace flair::actuator;
|
---|
[376] | 42 |
|
---|
[379] | 43 | CircleFollower::CircleFollower(string name,TargetController *controller): Thread(getFrameworkManager(),"CircleFollower",50), behaviourMode(BehaviourMode_t::Manual), vrpnLost(false) {
|
---|
| 44 | this->controller=controller;
|
---|
[376] | 45 | controller->Start();
|
---|
| 46 |
|
---|
[377] | 47 | Ugv* ugv=GetUgv();
|
---|
| 48 | ugv->UseDefaultPlot();
|
---|
| 49 |
|
---|
| 50 | VrpnClient* vrpnclient=new VrpnClient("vrpn", ugv->GetDefaultVrpnAddress(),80);
|
---|
[389] | 51 | ugvVrpn = new MetaVrpnObject(name);
|
---|
| 52 | targetVrpn=new MetaVrpnObject("target");
|
---|
[377] | 53 |
|
---|
| 54 | getFrameworkManager()->AddDeviceToLog(ugvVrpn);
|
---|
[376] | 55 | getFrameworkManager()->AddDeviceToLog(targetVrpn);
|
---|
| 56 | vrpnclient->Start();
|
---|
| 57 |
|
---|
[379] | 58 | Tab *ugvTab = new Tab(getFrameworkManager()->GetTabWidget(), "ugv", 0);
|
---|
[376] | 59 | GridLayout* buttonslayout = new GridLayout(ugvTab->NewRow(), "buttons");
|
---|
[379] | 60 | button_kill = new PushButton(buttonslayout->NewRow(), "kill");
|
---|
[376] | 61 | startCircle=new PushButton(buttonslayout->NewRow(),"start_circle");
|
---|
| 62 | stopCircle=new PushButton(buttonslayout->LastRowLastCol(),"stop_circle");
|
---|
| 63 |
|
---|
| 64 | circle=new TrajectoryGenerator2DCircle(vrpnclient->GetLayout()->NewRow(),"circle");
|
---|
[377] | 65 | ugvVrpn->xPlot()->AddCurve(circle->GetMatrix()->Element(0,0),DataPlot::Blue);
|
---|
| 66 | ugvVrpn->yPlot()->AddCurve(circle->GetMatrix()->Element(0,1),DataPlot::Blue);
|
---|
| 67 | ugvVrpn->VxPlot()->AddCurve(circle->GetMatrix()->Element(1,0),DataPlot::Blue);
|
---|
| 68 | ugvVrpn->VyPlot()->AddCurve(circle->GetMatrix()->Element(1,1),DataPlot::Blue);
|
---|
| 69 | ugvVrpn->XyPlot()->AddCurve(circle->GetMatrix()->Element(0,1),circle->GetMatrix()->Element(0,0),DataPlot::Blue,"circle");
|
---|
[379] | 70 |
|
---|
| 71 | Tab *lawTab = new Tab(getFrameworkManager()->GetTabWidget(), "control laws");
|
---|
| 72 | TabWidget *tabWidget = new TabWidget(lawTab->NewRow(), "laws");
|
---|
| 73 | Tab *setupLawTab = new Tab(tabWidget, "Setup");
|
---|
| 74 | Tab *graphLawTab = new Tab(tabWidget, "Graphes");
|
---|
[376] | 75 | uX=new Pid(setupLawTab->At(1,0),"u_x");
|
---|
| 76 | uX->UseDefaultPlot(graphLawTab->NewRow());
|
---|
| 77 | uY=new Pid(setupLawTab->At(1,1),"u_y");
|
---|
[379] | 78 | uY->UseDefaultPlot(graphLawTab->LastRowLastCol());
|
---|
| 79 |
|
---|
| 80 | l=new DoubleSpinBox(setupLawTab->NewRow(),"L", " m", 0, 10, 0.1, 1,1);
|
---|
[376] | 81 | }
|
---|
| 82 |
|
---|
| 83 | CircleFollower::~CircleFollower() {
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | void CircleFollower::Run(void) {
|
---|
| 87 | WarnUponSwitches(true);
|
---|
[389] | 88 | SetPeriodMS(20);
|
---|
[376] | 89 |
|
---|
| 90 | if (getFrameworkManager()->ErrorOccured() == true) {
|
---|
| 91 | SafeStop();
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[377] | 94 | while (!ToBeStopped()) {
|
---|
[389] | 95 | SecurityCheck();
|
---|
| 96 | CheckJoystick();
|
---|
| 97 | CheckPushButton();
|
---|
| 98 |
|
---|
[379] | 99 | if(behaviourMode==BehaviourMode_t::Manual) ComputeManualControls();
|
---|
| 100 | if(behaviourMode==BehaviourMode_t::Circle) ComputeCircleControls();
|
---|
[389] | 101 | WaitPeriod();
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | void CircleFollower::CheckPushButton(void) {
|
---|
| 106 | if (startCircle->Clicked() == true)
|
---|
| 107 | StartCircle();
|
---|
[379] | 108 |
|
---|
[389] | 109 | if (stopCircle->Clicked() == true)
|
---|
| 110 | StopCircle();
|
---|
| 111 |
|
---|
| 112 | if (button_kill->Clicked() == true)
|
---|
| 113 | SafeStop();
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | void CircleFollower::CheckJoystick(void) {
|
---|
| 117 | //R1 and Circle
|
---|
| 118 | if(controller->IsButtonPressed(9) && controller->IsButtonPressed(4)) {
|
---|
| 119 | StartCircle();
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | //R1 and Cross
|
---|
| 123 | if(controller->IsButtonPressed(9) && controller->IsButtonPressed(5)) {
|
---|
| 124 | StopCircle();
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | void CircleFollower::SecurityCheck(void) {
|
---|
| 129 | if ((!vrpnLost) && (behaviourMode==BehaviourMode_t::Circle)) {
|
---|
| 130 | if (!targetVrpn->IsTracked(500)) {
|
---|
| 131 | Thread::Err("VRPN, target lost\n");
|
---|
| 132 | vrpnLost=true;
|
---|
[379] | 133 | StopCircle();
|
---|
| 134 | }
|
---|
[389] | 135 | if (!ugvVrpn->IsTracked(500)) {
|
---|
| 136 | Thread::Err("VRPN, ugv lost\n");
|
---|
| 137 | vrpnLost=true;
|
---|
[379] | 138 | StopCircle();
|
---|
| 139 | }
|
---|
[376] | 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[379] | 143 | void CircleFollower::ComputeManualControls(void) {
|
---|
| 144 | float speed=-controller->GetAxisValue(3);
|
---|
| 145 | float turn=controller->GetAxisValue(0);
|
---|
| 146 | GetUgv()->GetUgvControls()->SetControls(speed,turn);
|
---|
| 147 | }
|
---|
[376] | 148 |
|
---|
[379] | 149 | void CircleFollower::ComputeCircleControls(void) {
|
---|
| 150 |
|
---|
| 151 | Vector3Df ugv_pos,ugv_vel,target_pos; // in VRPN coordinate system
|
---|
| 152 | Vector2Df ugv_2Dpos,ugv_2Dvel,target_2Dpos; // in VRPN coordinate system
|
---|
| 153 | Vector2Df pos_error,vel_error;
|
---|
| 154 | Vector2Df circle_pos,circle_vel;
|
---|
| 155 |
|
---|
| 156 | ugvVrpn->GetPosition(ugv_pos);
|
---|
| 157 | ugvVrpn->GetSpeed(ugv_vel);
|
---|
| 158 |
|
---|
| 159 | ugv_pos.To2Dxy(ugv_2Dpos);
|
---|
| 160 | ugv_vel.To2Dxy(ugv_2Dvel);
|
---|
| 161 |
|
---|
| 162 | targetVrpn->GetPosition(target_pos);
|
---|
| 163 | target_pos.To2Dxy(target_2Dpos);
|
---|
| 164 | circle->SetCenter(target_2Dpos);
|
---|
| 165 |
|
---|
| 166 | //circle reference
|
---|
| 167 | circle->Update(GetTime());
|
---|
| 168 | circle->GetPosition(circle_pos);
|
---|
| 169 | circle->GetSpeed(circle_vel);
|
---|
| 170 |
|
---|
| 171 | //error in optitrack frame
|
---|
| 172 | pos_error=ugv_2Dpos-circle_pos;
|
---|
| 173 | vel_error=ugv_2Dvel-circle_vel;
|
---|
| 174 |
|
---|
| 175 | uX->SetValues(pos_error.x, vel_error.x);
|
---|
| 176 | uX->Update(GetTime());
|
---|
| 177 | uY->SetValues(pos_error.y, vel_error.y);
|
---|
| 178 | uY->Update(GetTime());
|
---|
| 179 |
|
---|
| 180 | //get yaw from vrpn
|
---|
| 181 | Quaternion vrpnQuaternion;
|
---|
| 182 | ugvVrpn->GetQuaternion(vrpnQuaternion);
|
---|
| 183 | float yaw=vrpnQuaternion.ToEuler().yaw;
|
---|
| 184 | float v= cosf(yaw)*uX->Output() + sinf(yaw)*uY->Output();
|
---|
| 185 | float w = -sinf(yaw)/l->Value()*uX->Output() + cosf(yaw)/l->Value()*uY->Output();
|
---|
| 186 | GetUgv()->GetUgvControls()->SetControls(-v,-w);
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 |
|
---|
| 190 | void CircleFollower::StartCircle(void) {
|
---|
[389] | 191 | if(behaviourMode!=BehaviourMode_t::Circle) {
|
---|
[379] | 192 | Vector3Df ugv_pos,target_pos;
|
---|
| 193 | Vector2Df ugv_2Dpos,target_2Dpos;
|
---|
| 194 |
|
---|
[376] | 195 | targetVrpn->GetPosition(target_pos);
|
---|
| 196 | target_pos.To2Dxy(target_2Dpos);
|
---|
| 197 | circle->SetCenter(target_2Dpos);
|
---|
| 198 |
|
---|
[379] | 199 | ugvVrpn->GetPosition(ugv_pos);
|
---|
| 200 | ugv_pos.To2Dxy(ugv_2Dpos);
|
---|
| 201 | circle->StartTraj(ugv_2Dpos);
|
---|
[376] | 202 |
|
---|
| 203 | uX->Reset();
|
---|
| 204 | uY->Reset();
|
---|
[379] | 205 | behaviourMode=BehaviourMode_t::Circle;
|
---|
| 206 | Thread::Info("CircleFollower: start circle\n");
|
---|
[389] | 207 | }
|
---|
[376] | 208 | }
|
---|
| 209 |
|
---|
| 210 | void CircleFollower::StopCircle(void) {
|
---|
[389] | 211 | if(behaviourMode==BehaviourMode_t::Circle) {
|
---|
[376] | 212 | circle->FinishTraj();
|
---|
| 213 | //GetJoystick()->Rumble(0x70);
|
---|
[379] | 214 | behaviourMode=BehaviourMode_t::Manual;
|
---|
| 215 | Thread::Info("CircleFollower: finishing circle\n");
|
---|
[389] | 216 | }
|
---|
[376] | 217 | }
|
---|
| 218 |
|
---|