Mekatronics Team G
Dependencies: BNO055_fusion PowerControl mbed BMP280
Fork of DEMO3 by
LOCOMOTION.cpp
- Committer:
- 12104404
- Date:
- 2016-04-05
- Revision:
- 25:f3bf8351bbd4
- Parent:
- 24:fb1f083ebd62
- Child:
- 26:0ea6a550a99d
File content as of revision 25:f3bf8351bbd4:
#include "LOCOMOTION.h" LOCOMOTION::LOCOMOTION (PinName en, PinName motor1F, PinName motor1B, PinName motor2F, PinName motor2B, PinName forward1, PinName forward2, DigitalOut& led1, DigitalOut& led2, DigitalOut& led3, DigitalOut& led4): _en(en), _m1f(motor1F), _m1b(motor1B), _m2f(motor2F), _m2b(motor2B), _m1dir(forward1), _m2dir(forward2), _led1(led1), _led2(led2), _led3(led3), _led4(led4) { s=0; setMotors(0); _m1dir=0; _m2dir=0; _en=1; _led1=1; wait(0.01); _led1=0; } inline void LOCOMOTION::setMotors(float x) { _m1f=x; _m1b=x; _m2f=x; _m2b=x; } inline int LOCOMOTION::wrap(int num) { return num%360; } void LOCOMOTION::eStop(void) { //Stop Motors setMotors(0); //Disable Motor Drivers _en=0; } bool LOCOMOTION::setXPos(int target, int current, int error, int angle) { if(abs(current-target)<=error) s=SPEED_X_MIN; else s=((SPEED_X_MAX-SPEED_X_MIN)*abs(current-target)/FRAME_W)+SPEED_X_MIN; if(current>target+error) { if(angle==0) { _m1dir=1; _m2dir=1; } else { _m1dir=0; _m2dir=0; } setMotors(s); } else if(current<target-error) { if(angle==0) { _m1dir=0; _m2dir=0; } else { _m1dir=1; _m2dir=1; } setMotors(s); } else { setMotors(0); return true; } return false; } bool LOCOMOTION::setYBias(int target, int current, int error, int angle) { if(abs(current-target)<=error) s=Y_BIAS_MIN; else s=((Y_BIAS_MAX-Y_BIAS_MIN)*abs(current-target)/FRAME_H)+Y_BIAS_MIN; if(current>target+error) { //_m1dir=1; //_m2dir=1; if(angle==0) { _m1f=_m1f*(1+s); _m1b=_m1b*(1+s); } else { _m2f=_m2f*(1+s); _m2b=_m2b*(1+s); } } else if(current<target-error) { //_m1dir=0; //_m2dir=0; if(angle==0) { _m2f=_m2f*(1+s); _m2b=_m2b*(1+s); } else { _m1f=_m1f*(1+s); _m1b=_m1b*(1+s); } } else { s=0; return true; } return false; } bool LOCOMOTION::setAngle(int target, int current, int error, int mode) { s = 0; int diff = 0; diff = 180-wrap(target); if(abs(wrap(current+diff)-180)<=error) s=SPEED_TURN_MIN; else s=((SPEED_TURN_MAX-SPEED_TURN_MIN)*abs(wrap(current+diff)-180)/180)+SPEED_TURN_MIN; switch(mode) { default: case ANGLE_TURN: if(wrap(current+diff)>180+error) { _m1dir=1; _m2dir=0; setMotors(s); } else if(wrap(current+diff)<180-error) { _m1dir=0; _m2dir=1; setMotors(s); } else { setMotors(0); return true; } break; } return false; } void LOCOMOTION::setXstate(int *xCurrState, int *xTarget,bool xGood,bool angleGood,int *angleGoal) { if (*xCurrState==X_INCREASE|| *xCurrState==X_DECREASE) { if(angleGood && xGood) { *xCurrState=X_BACKWARDS; } } if(*xCurrState==X_BACKWARDS) { if(xGood && angleGood) { if(*angleGoal==0) { *xCurrState=X_INCREASE; } else { *xCurrState=X_DECREASE; } } } switch(*xCurrState) { case X_INCREASE: *angleGoal=180; *xTarget=X_FAR_GOAL; _m1dir=1; _m2dir=1; break; case X_DECREASE: *angleGoal=0; *xTarget=X_NEAR_GOAL; _m1dir=1; _m2dir=1; break; case X_BACKWARDS: if (*angleGoal==0) { *xTarget=X_NEAR_GOAL+BACKOFF; } else { *xTarget=X_FAR_GOAL-BACKOFF; } _m1dir=0; _m2dir=0; break; } } void LOCOMOTION::setAngleTol(int *angleTol,bool yGood, bool xGood) { if (xGood||yGood) { *angleTol=2; } else { *angleTol=10; } } void LOCOMOTION::setYgoal(bool xGood,bool angleGood,bool yGood,int *yTarget) { if (xGood && angleGood && yGood) { *yTarget+=Y_INCREMENT; } } void LOCOMOTION::check_xya(bool *xGood,bool *yGood,bool *angleGood, int xGoal,int angleGoal,int yGoal,LOCALIZE_xya xya,int xErr, int yErr,int angleErr) { *xGood=abs(xya.x-xGoal)<xErr; *yGood=abs(xya.y-yGoal)<yErr; *angleGood=setAngle(angleGoal,xya.a,angleErr,ANGLE_TURN); }