@@ -140,27 +140,42 @@ bool SwerveDrive::SetIdleMode(rev::CANSparkMax::IdleMode mode) {
140140}
141141
142142bool SwerveDrive::VectorTeleOpDrive (const double &drive, const double &strafe, const double &turn) {
143- double final_drive = drive * control_decay + last_drive * ( 1 - control_decay) ;
144- double final_strafe = strafe * control_decay + last_strafe * ( 1 - control_decay) ;
145- double final_turn = turn * control_decay + last_turn * ( 1 - control_decay) ;
143+ double drive_ = drive;
144+ double strafe_ = strafe;
145+ double turn_ = turn;
146146
147147 // if turn is very small
148- if (abs (final_turn ) < 0.3 ) {
149- final_turn = 0.0 ; // then zero it
148+ if (abs (turn ) < 0.3 ) {
149+ turn_ = 0.0 ; // then zero it
150150 // } else if (final_turn)
151151 }
152152
153153 // if strafe is very small
154- if (abs (final_strafe ) < 0.15 ) {
155- final_strafe = 0.0 ; // then zero it
154+ if (abs (strafe ) < 0.15 ) {
155+ strafe_ = 0.0 ; // then zero it
156156 }
157157
158+ if (abs (drive) < 0.1 ) {
159+ drive_ = 0.0 ;
160+ }
161+
162+ double heading = 0.0 ;
163+ OKC_CALL (this ->GetHeading (&heading));
164+ heading *= -1 ;
165+
166+ double temp_drive = drive_;
167+ double temp_strafe = strafe_;
168+
169+ // field oriented drive (page 10 of https://www.first1684.com/uploads/2/0/1/6/20161347/chimiswerve_whitepaper__2_.pdf)
170+ drive_ = temp_drive * cos (TeamOKC::Radians (heading)) + temp_strafe * sin (TeamOKC::Radians (heading));
171+ strafe_ = temp_strafe * cos (TeamOKC::Radians (heading)) - temp_drive * sin (TeamOKC::Radians (heading));
172+
158173 // copied from ChiefDelphi thread
159174 // TODO post link here
160- double A = final_strafe - final_turn * tracklength_/2 ;
161- double B = final_strafe + final_turn * tracklength_/2 ;
162- double C = final_drive - final_turn * trackwidth_/2 ;
163- double D = final_drive + final_turn * trackwidth_/2 ;
175+ double A = strafe_ - turn_ * tracklength_/2 ;
176+ double B = strafe_ + turn_ * tracklength_/2 ;
177+ double C = drive_ - turn_ * trackwidth_/2 ;
178+ double D = drive_ + turn_ * trackwidth_/2 ;
164179
165180 // speed
166181 double left_front_speed = sqrt (pow (B, 2 ) + pow (D, 2 ));
@@ -236,14 +251,14 @@ bool SwerveDrive::VectorTeleOpDrive(const double &drive, const double &strafe, c
236251 // really nice convoluted deadband
237252 // this is to stop the swerve modules from immediately trying to center themselves instead of
238253 // coasting until receiving another instruction so we don't tip
239- if (abs (final_drive ) > 0.05 || abs (final_strafe ) > 0.05 || abs (final_turn ) > 0.3 ) {
254+ if (abs (drive_ ) > 0.05 || abs (strafe_ ) > 0.05 || abs (turn_ ) > 0.3 ) {
240255 OKC_CALL (this ->left_front_module_ ->SetAngle (left_front_turn));
241256 OKC_CALL (this ->left_back_module_ ->SetAngle (left_back_turn));
242257 OKC_CALL (this ->right_front_module_ ->SetAngle (right_front_turn));
243258 OKC_CALL (this ->right_back_module_ ->SetAngle (right_back_turn));
244259 }
245260
246- if (abs (final_drive ) < 0.05 && abs (final_strafe ) < 0.05 && abs (final_turn ) < 0.3 ) {
261+ if (abs (drive_ ) < 0.05 && abs (strafe_ ) < 0.05 && abs (turn_ ) < 0.3 ) {
247262 this ->interface_ ->left_front_drive_motor_output = 0.0 ;
248263 this ->interface_ ->left_back_drive_motor_output = 0.0 ;
249264 this ->interface_ ->right_front_drive_motor_output = 0.0 ;
@@ -254,10 +269,10 @@ bool SwerveDrive::VectorTeleOpDrive(const double &drive, const double &strafe, c
254269 double right_front_steer_error = 0.0 ;
255270 double right_back_steer_error = 0.0 ;
256271
257- this ->interface_ ->left_front_drive_motor_output = cos (left_front_module_->GetSteerError (&left_front_steer_error)) * left_front_speed;
258- this ->interface_ ->left_back_drive_motor_output = cos (left_front_module_ ->GetSteerError (&left_back_steer_error)) * left_back_speed;
259- this ->interface_ ->right_front_drive_motor_output = cos (left_front_module_ ->GetSteerError (&right_front_steer_error)) * right_front_speed;
260- this ->interface_ ->right_back_drive_motor_output = cos (left_front_module_ ->GetSteerError (&right_back_steer_error)) * right_back_speed;
272+ this ->interface_ ->left_front_drive_motor_output = cos (TeamOKC::Radians ( left_front_module_->GetSteerError (&left_front_steer_error) )) * left_front_speed;
273+ this ->interface_ ->left_back_drive_motor_output = cos (TeamOKC::Radians (left_back_module_ ->GetSteerError (&left_back_steer_error) )) * left_back_speed;
274+ this ->interface_ ->right_front_drive_motor_output = cos (TeamOKC::Radians (right_front_module_ ->GetSteerError (&right_front_steer_error) )) * right_front_speed;
275+ this ->interface_ ->right_back_drive_motor_output = cos (TeamOKC::Radians (right_back_module_ ->GetSteerError (&right_back_steer_error) )) * right_back_speed;
261276 }
262277
263278 OKC_CALL (this ->left_front_module_ ->GetSteerOutput (&this ->interface_ ->left_front_steer_motor_output ));
0 commit comments