2020-02-29 22:10:53 +01:00
# include "motors_movement/drivecontroller.h"
# include "sensors/sensors.h"
# include "behaviour_control/status_vector.h"
2020-03-12 12:09:13 +01:00
# include "vars.h"
2019-11-11 22:26:34 +01:00
DriveController : : DriveController ( Motor * m1_ , Motor * m2_ , Motor * m3_ , Motor * m4_ ) {
2019-11-13 16:26:03 +01:00
m1 = m1_ ;
m2 = m2_ ;
m3 = m3_ ;
m4 = m4_ ;
2019-12-05 11:53:01 +01:00
pDir = 0 ;
pSpeed = 0 ;
pTilt = 0 ;
vx = 0 ;
vy = 0 ;
speed1 = 0 ;
speed2 = 0 ;
speed3 = 0 ;
speed4 = 0 ;
delta = 0 ;
2020-02-10 19:48:31 +01:00
input = 0 ;
output = 0 ;
setpoint = 0 ;
2020-02-19 17:44:31 +01:00
pid = new PID ( & input , & output , & setpoint , KP , KI , KD , 1 , DIRECT ) ;
2020-02-17 19:14:48 +01:00
2021-02-22 18:43:43 +01:00
pid - > SetSampleTime ( 2.5 ) ;
2020-02-19 17:44:31 +01:00
pid - > setAngleWrap ( true ) ;
pid - > SetDerivativeLag ( 2 ) ;
2020-02-17 19:14:48 +01:00
pid - > SetOutputLimits ( - 255 , 255 ) ;
2020-02-10 19:48:31 +01:00
pid - > SetMode ( AUTOMATIC ) ;
2020-02-26 18:56:39 +01:00
// Complementary filter for speed
speedFilter = new ComplementaryFilter ( 0.3 ) ;
2020-01-29 18:56:49 +01:00
canUnlock = true ;
unlockTime = 0 ;
vxp = 0 ;
vxn = 0 ;
vyp = 0 ;
vyn = 0 ;
2019-11-11 22:26:34 +01:00
}
2020-02-26 18:51:46 +01:00
void DriveController : : prepareDrive ( int dir , int speed , int tilt ) {
2019-11-11 22:26:34 +01:00
pDir = dir ;
pSpeed = speed ;
pTilt = tilt ;
}
void DriveController : : drivePrepared ( ) {
drive ( pDir , pSpeed , pTilt ) ;
}
2019-11-18 14:37:55 +01:00
float DriveController : : torad ( float f ) {
return ( f * PI / 180.0 ) ;
}
2019-12-11 16:29:18 +01:00
void DriveController : : drive ( int dir , int speed , int tilt ) {
2020-02-18 09:37:36 +01:00
2020-02-26 18:56:39 +01:00
speed = speedFilter - > calculate ( speed ) * GLOBAL_SPD_MULT ;
2021-04-19 19:14:51 +02:00
//tilt = tilt > 180 ? tilt - 360 : tilt;
2020-02-21 13:37:32 +01:00
2020-03-12 12:09:13 +01:00
//TODO: Changing CURRENT_DATA_READ to CURRENT_DATA_WRITE?
2020-10-31 15:37:05 +01:00
// Disable vector sum because calculations are a bitty crappy imho. Will have to test if it's what makes the robot act strange with lines
// Re enabling the below lines requires to comment out drive->prepareDrive and uncommenting the lines relative to vector sum inside positionsys_camera and comment out the other lines here
2021-04-14 15:00:43 +02:00
vx = ( ( speed * cosins [ dir ] ) ) + CURRENT_DATA_READ . addvx ;
vy = ( ( - speed * sins [ dir ] ) ) + CURRENT_DATA_READ . addvy ;
2021-03-22 14:03:53 +01:00
2021-04-14 15:00:43 +02:00
// vx = ((speed * cosins[dir]));
// vy = ((-speed * sins[dir]));
2019-11-11 22:26:34 +01:00
2021-03-22 14:03:53 +01:00
// if((((vy < 0 && vxn == 1) || (vy > 0 && vxp == 1) || (vx < 0 && vyp == 1) || (vx > 0 && vyn == 1)) && canUnlock) || (millis() > this->unlockTime+UNLOCK_THRESH)) {
// vxn = 0;
// vxp = 0;
// vyp = 0;
// vyn = 0;
// }
2020-01-29 18:56:49 +01:00
2021-03-22 14:03:53 +01:00
// if((vy > 0 && vxn == 1) || (vy < 0 && vxp == 1)) vy = 0;
// if((vx > 0 && vyp == 1) || (vx < 0 && vyn == 1)) vx = 0;
2020-01-29 18:56:49 +01:00
2019-11-11 22:26:34 +01:00
speed1 = ( ( vx * sins [ m1 - > angle ] ) + ( vy * cosins [ m1 - > angle ] ) ) ;
speed2 = ( ( vx * sins [ m2 - > angle ] ) + ( vy * cosins [ m2 - > angle ] ) ) ;
2021-01-11 15:47:18 +01:00
speed3 = ( ( vx * sins [ m3 - > angle ] ) + ( vy * cosins [ m3 - > angle ] ) ) ;
speed4 = ( ( vx * sins [ m4 - > angle ] ) + ( vy * cosins [ m4 - > angle ] ) ) ;
2019-11-11 22:26:34 +01:00
2020-10-31 15:37:05 +01:00
// Calculate position error relative to the 0
2020-02-21 13:37:32 +01:00
delta = CURRENT_DATA_READ . IMUAngle ;
2020-02-17 19:14:48 +01:00
if ( delta > 180 ) delta = delta - 360 ;
2020-02-10 19:48:31 +01:00
input = delta ;
2020-02-21 13:37:32 +01:00
setpoint = tilt ;
2020-02-17 19:14:48 +01:00
pid - > Compute ( ) ;
2019-11-11 22:26:34 +01:00
2020-02-19 17:44:31 +01:00
pidfactor = - output ;
2019-11-11 22:26:34 +01:00
speed1 + = pidfactor ;
speed2 + = pidfactor ;
speed3 + = pidfactor ;
speed4 + = pidfactor ;
2021-03-22 14:03:53 +01:00
// Find the maximum speed and scale all of them for the maximum to be 255
float maxVel = 0 ;
maxVel = max ( abs ( speed1 ) , maxVel ) ;
maxVel = max ( abs ( speed2 ) , maxVel ) ;
maxVel = max ( abs ( speed3 ) , maxVel ) ;
maxVel = max ( abs ( speed4 ) , maxVel ) ;
if ( maxVel > 255 ) {
// Ratio to 255
float ratio = maxVel / 255 ;
// //Scale all the velocities
speed1 / = ratio ;
speed2 / = ratio ;
speed3 / = ratio ;
speed4 / = ratio ;
DEBUG . print ( speed1 ) ;
DEBUG . print ( " | " ) ;
DEBUG . print ( speed2 ) ;
DEBUG . print ( " | " ) ;
DEBUG . print ( speed3 ) ;
DEBUG . print ( " | " ) ;
DEBUG . print ( speed4 ) ;
DEBUG . print ( " | " ) ;
DEBUG . println ( maxVel ) ;
}
2019-11-11 22:26:34 +01:00
speed1 = constrain ( speed1 , - 255 , 255 ) ;
speed2 = constrain ( speed2 , - 255 , 255 ) ;
speed3 = constrain ( speed3 , - 255 , 255 ) ;
2019-12-05 11:53:01 +01:00
speed4 = constrain ( speed4 , - 255 , 255 ) ;
2019-11-11 22:26:34 +01:00
m1 - > drive ( ( int ) speed1 ) ;
m2 - > drive ( ( int ) speed2 ) ;
m3 - > drive ( ( int ) speed3 ) ;
m4 - > drive ( ( int ) speed4 ) ;
2020-02-18 09:37:36 +01:00
2020-02-21 13:37:32 +01:00
oldSpeed = speed ;
2020-02-18 09:37:36 +01:00
CURRENT_DATA_WRITE . dir = dir ;
CURRENT_DATA_WRITE . speed = speed ;
CURRENT_DATA_WRITE . tilt = tilt ;
CURRENT_DATA_WRITE . axisBlock [ 0 ] = vxp ;
CURRENT_DATA_WRITE . axisBlock [ 1 ] = vxn ;
CURRENT_DATA_WRITE . axisBlock [ 2 ] = vyp ;
CURRENT_DATA_WRITE . axisBlock [ 3 ] = vyn ;
2021-04-14 15:00:43 +02:00
}
void DriveController : : resetDrive ( ) {
CURRENT_DATA_WRITE . addvx = 0 ;
CURRENT_DATA_WRITE . addvy = 0 ;
prepareDrive ( 0 , 0 , 0 ) ;
2021-05-07 21:39:03 +02:00
}
void DriveController : : stopAll ( ) {
m1 - > stop ( ) ;
m2 - > stop ( ) ;
m3 - > stop ( ) ;
m4 - > stop ( ) ;
2021-04-14 15:00:43 +02:00
}