amiro-os / devices / DiWheelDrive / userthread.cpp @ 8dced1c9
History | View | Annotate | Download (27.622 KB)
| 1 | 10bf9cc0 | galberding | // #include "userthread.hpp"
|
|---|---|---|---|
| 2 | 58fe0e0b | Thomas Schöpping | #include "global.hpp" |
| 3 | 10bf9cc0 | galberding | #include <cmath> |
| 4 | 8dced1c9 | galberding | #include "linefollow.hpp" |
| 5 | d4c6efa9 | galberding | #include "userthread.hpp" |
| 6 | 10bf9cc0 | galberding | // #include <cmath>
|
| 7 | // #include "global.hpp"
|
||
| 8 | 58fe0e0b | Thomas Schöpping | using namespace amiro; |
| 9 | |||
| 10 | extern Global global;
|
||
| 11 | |||
| 12 | // a buffer for the z-value of the accelerometer
|
||
| 13 | int16_t accel_z; |
||
| 14 | 5d138bca | galberding | bool running = false; |
| 15 | 58fe0e0b | Thomas Schöpping | |
| 16 | |||
| 17 | 181f2892 | galberding | /**
|
| 18 | * Set speed.
|
||
| 19 | 8dced1c9 | galberding | *
|
| 20 | 181f2892 | galberding | * @param rpmSpeed speed for left and right wheel in rounds/min
|
| 21 | */
|
||
| 22 | c9fa414d | galberding | void UserThread::setRpmSpeedFuzzy(const int (&rpmSpeed)[2]) { |
| 23 | 5d138bca | galberding | global.motorcontrol.setTargetRPM(rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL] * 1000000, rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL] * 1000000); |
| 24 | 58fe0e0b | Thomas Schöpping | } |
| 25 | |||
| 26 | c9fa414d | galberding | void UserThread::setRpmSpeed(const int (&rpmSpeed)[2]) { |
| 27 | global.motorcontrol.setTargetRPM(rpmSpeed[constants::DiWheelDrive::LEFT_WHEEL], rpmSpeed[constants::DiWheelDrive::RIGHT_WHEEL]); |
||
| 28 | } |
||
| 29 | |||
| 30 | 181f2892 | galberding | void UserThread::lightOneLed(Color color, int idx){ |
| 31 | 5d138bca | galberding | global.robot.setLightColor(idx, Color(color)); |
| 32 | } |
||
| 33 | 58fe0e0b | Thomas Schöpping | |
| 34 | 181f2892 | galberding | void UserThread::lightAllLeds(Color color){
|
| 35 | 5d138bca | galberding | int led = 0; |
| 36 | for(led=0; led<8; led++){ |
||
| 37 | lightOneLed(color, led); |
||
| 38 | } |
||
| 39 | 58fe0e0b | Thomas Schöpping | } |
| 40 | |||
| 41 | 181f2892 | galberding | void UserThread::showChargingState(){
|
| 42 | uint8_t numLeds = global.robot.getPowerStatus().state_of_charge / 12;
|
||
| 43 | Color color = Color::GREEN; |
||
| 44 | if (numLeds <= 2){ |
||
| 45 | color = Color::RED; |
||
| 46 | }else if(numLeds <= 6){ |
||
| 47 | color = Color::YELLOW; |
||
| 48 | } |
||
| 49 | for (int i=0; i<numLeds; i++){ |
||
| 50 | lightOneLed(color, i); |
||
| 51 | this->sleep(300); |
||
| 52 | 9c46b728 | galberding | } |
| 53 | 181f2892 | galberding | this->sleep(1000); |
| 54 | lightAllLeds(Color::BLACK); |
||
| 55 | 9c46b728 | galberding | } |
| 56 | |||
| 57 | 10bf9cc0 | galberding | void UserThread::chargeAsLED(){
|
| 58 | uint8_t numLeds = global.robot.getPowerStatus().state_of_charge / 12;
|
||
| 59 | Color color = Color::GREEN; |
||
| 60 | if (numLeds <= 2){ |
||
| 61 | color = Color::RED; |
||
| 62 | }else if(numLeds <= 6){ |
||
| 63 | color = Color::YELLOW; |
||
| 64 | } |
||
| 65 | for (int i=0; i<numLeds; i++){ |
||
| 66 | lightOneLed(color, i); |
||
| 67 | // this->sleep(300);
|
||
| 68 | } |
||
| 69 | // this->sleep(1000);
|
||
| 70 | // lightAllLeds(Color::BLACK);
|
||
| 71 | } |
||
| 72 | |||
| 73 | // ----------------------------------------------------------------
|
||
| 74 | |||
| 75 | void UserThread::getProxySectorVals(uint16_t (&proxVals)[8], uint16_t (&sProx)[8]){ |
||
| 76 | for (int i=0; i<8; i++){ |
||
| 77 | sProx[i] = (proxVals[i] < proxVals[(i+1) % 8]) ? proxVals[i] : proxVals[(i+1) % 8]; |
||
| 78 | // chprintf((BaseSequentialStream*)&global.sercanmux1, "%d: %d, ", i, sProx[i]);
|
||
| 79 | 8dced1c9 | galberding | |
| 80 | 10bf9cc0 | galberding | } |
| 81 | // chprintf((BaseSequentialStream*)&global.sercanmux1, "\n");
|
||
| 82 | |||
| 83 | } |
||
| 84 | |||
| 85 | |||
| 86 | void UserThread::getMaxFrontSectorVal(uint16_t (&sProx)[8], int32_t &sPMax){ |
||
| 87 | for (int i=2; i<5; i++){ |
||
| 88 | sPMax = (sPMax < sProx[i]) ? sProx[i] : sPMax; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | void UserThread::proxSectorSpeedCorrection(int (&rpmSpeed)[2], uint16_t (&proxVals)[8]){ |
||
| 93 | int i;
|
||
| 94 | uint16_t sProx[8];
|
||
| 95 | int32_t sPMax = 0;
|
||
| 96 | getProxySectorVals(proxVals, sProx); |
||
| 97 | getMaxFrontSectorVal(sProx, sPMax); |
||
| 98 | 8dced1c9 | galberding | |
| 99 | 10bf9cc0 | galberding | int32_t speedL = rpmSpeed[0] - (sPMax * pCtrl.pFactor);
|
| 100 | int32_t speedR = rpmSpeed[1] - (sPMax * pCtrl.pFactor);
|
||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | if(sPMax > pCtrl.threshMid){
|
||
| 105 | rpmSpeed[0] = 0; |
||
| 106 | rpmSpeed[1] = 0; |
||
| 107 | pCtrl.staticCont++; |
||
| 108 | }else if((speedL > 0) || (speedR > 0)){ |
||
| 109 | pCtrl.staticCont = 0;
|
||
| 110 | rpmSpeed[0] = speedL;
|
||
| 111 | rpmSpeed[1] = speedR;
|
||
| 112 | }else{
|
||
| 113 | rpmSpeed[0] = 4000000 + (rpmSpeed[0] - global.rpmForward[0] * 1000000); |
||
| 114 | rpmSpeed[1] = 4000000 + (rpmSpeed[1] - global.rpmForward[0] * 1000000); |
||
| 115 | } |
||
| 116 | |||
| 117 | for(i=4; i<5; i++){ |
||
| 118 | if ((proxVals[i] > pCtrl.threshMid) && (proxVals[i+1] > pCtrl.threshLow)){ |
||
| 119 | rpmSpeed[0] = -5000000 ; |
||
| 120 | rpmSpeed[1] = -5000000 ; |
||
| 121 | // pCtrl.staticCont++;
|
||
| 122 | break;
|
||
| 123 | } |
||
| 124 | } |
||
| 125 | chargeAsLED(); |
||
| 126 | 8dced1c9 | galberding | |
| 127 | 10bf9cc0 | galberding | // chprintf((BaseSequentialStream*)&global.sercanmux1, "Max: %d factor: %d, Panel: %d SpeedL: %d SpeedR: %d ActualL: %d ActualR: %d\n",sPMax, pCtrl.pFactor, sPMax * pCtrl.pFactor, speedL, speedR, rpmSpeed[0], rpmSpeed[1]);
|
| 128 | |||
| 129 | |||
| 130 | } |
||
| 131 | // -------------------------------------------------------------------
|
||
| 132 | |||
| 133 | |||
| 134 | void UserThread::preventCollision( int (&rpmSpeed)[2], uint16_t (&proxVals)[8]) { |
||
| 135 | |||
| 136 | if((proxVals[3] > pCtrl.threshLow) || (proxVals[4] > pCtrl.threshLow)){ |
||
| 137 | rpmSpeed[0] = rpmSpeed[0] / 2; |
||
| 138 | rpmSpeed[1] = rpmSpeed[1] / 2; |
||
| 139 | } |
||
| 140 | |||
| 141 | if((proxVals[3] > pCtrl.threshMid) || (proxVals[4] > pCtrl.threshMid)){ |
||
| 142 | d4c6efa9 | galberding | rpmSpeed[0] = rpmSpeed[0] / 3; |
| 143 | rpmSpeed[1] = rpmSpeed[1] / 3; |
||
| 144 | 10bf9cc0 | galberding | } |
| 145 | |||
| 146 | if((proxVals[3] > pCtrl.threshHigh) || (proxVals[4] > pCtrl.threshHigh)){ |
||
| 147 | rpmSpeed[0] = 0; |
||
| 148 | rpmSpeed[1] = 0; |
||
| 149 | utCount.ringProxCount++; |
||
| 150 | }else{
|
||
| 151 | utCount.ringProxCount = 0;
|
||
| 152 | } |
||
| 153 | 8dced1c9 | galberding | |
| 154 | 10bf9cc0 | galberding | } |
| 155 | |||
| 156 | |||
| 157 | 9c46b728 | galberding | /**
|
| 158 | * Blocks as long as the position changes.
|
||
| 159 | */
|
||
| 160 | 181f2892 | galberding | void UserThread::checkForMotion(){
|
| 161 | 8dced1c9 | galberding | bool motion = true; |
| 162 | 9c46b728 | galberding | int led = 0; |
| 163 | types::position oldPos = global.odometry.getPosition(); |
||
| 164 | while(motion){
|
||
| 165 | 8dced1c9 | galberding | this->sleep(200); |
| 166 | 9c46b728 | galberding | types::position tmp = global.odometry.getPosition(); |
| 167 | 8dced1c9 | galberding | motion = oldPos.x != tmp.x; //abs(oldPos.x - tmp.x)+ abs(oldPos.y - tmp.y)+abs(oldPos.z - tmp.z);
|
| 168 | 9c46b728 | galberding | oldPos = tmp; |
| 169 | 8dced1c9 | galberding | global.robot.setLightColor((led + 1) % 8, Color(Color::YELLOW)); |
| 170 | global.robot.setLightColor(led % 8, Color(Color::BLACK));
|
||
| 171 | led++; |
||
| 172 | 9c46b728 | galberding | } |
| 173 | 10bf9cc0 | galberding | lightAllLeds(Color::BLACK); |
| 174 | 9c46b728 | galberding | } |
| 175 | |||
| 176 | fbcb25cc | galberding | bool UserThread::checkFrontalObject(){
|
| 177 | 10bf9cc0 | galberding | uint32_t thresh = pCtrl.threshMid; |
| 178 | fbcb25cc | galberding | uint32_t prox; |
| 179 | for(int i=0; i<8; i++){ |
||
| 180 | prox = global.robot.getProximityRingValue(i); |
||
| 181 | if((i == 3) || (i == 4)){ |
||
| 182 | if(prox < thresh){
|
||
| 183 | return false; |
||
| 184 | } |
||
| 185 | }else{
|
||
| 186 | if(prox > thresh){
|
||
| 187 | return false; |
||
| 188 | } |
||
| 189 | } |
||
| 190 | } |
||
| 191 | return true; |
||
| 192 | } |
||
| 193 | |||
| 194 | 181f2892 | galberding | bool UserThread::checkPinVoltage(){
|
| 195 | 8dced1c9 | galberding | return global.ltc4412.isPluggedIn();
|
| 196 | 181f2892 | galberding | } |
| 197 | 9c46b728 | galberding | |
| 198 | 181f2892 | galberding | bool UserThread::checkPinEnabled(){
|
| 199 | return global.ltc4412.isEnabled();
|
||
| 200 | } |
||
| 201 | |||
| 202 | int UserThread::checkDockingSuccess(){
|
||
| 203 | // setRpmSpeed(stop);
|
||
| 204 | checkForMotion(); |
||
| 205 | 9c46b728 | galberding | int success = 0; |
| 206 | 10bf9cc0 | galberding | // global.odometry.resetPosition();
|
| 207 | 9c46b728 | galberding | types::position start = global.startPos = global.odometry.getPosition(); |
| 208 | 27d4e1fa | galberding | global.motorcontrol.setMotorEnable(false);
|
| 209 | this->sleep(1000); |
||
| 210 | 181f2892 | galberding | types::position stop_ = global.endPos = global.odometry.getPosition(); |
| 211 | 8dced1c9 | galberding | |
| 212 | 9c46b728 | galberding | // Amiro moved, docking was not successful
|
| 213 | 10bf9cc0 | galberding | // if ((start.x + stop_.x) || (start.y + stop_.y)){
|
| 214 | 84b4c632 | galberding | if (abs(start.x - stop_.x) > 200 /* || (start.y + stop_.y) */){ |
| 215 | 181f2892 | galberding | lightAllLeds(Color::RED); |