Skip to content

Commit bcfca5c

Browse files
Feat/field oriented drive (#57)
* add field oriented drive * fix the limiting of drive speed to steer error * fix the gyro readings (read the navx docs, had to to the Omnimount thing) * actual field oriented drive * fix field-oriented drive, and thus far it works pretty well (not entirely well, but that's more a matter for feat/swerve-drive-tuning or something) * put the inversion logic for teleop back in * second driver camera * make default speed and acceleration faster * add drive deadband, remove duplicated code
1 parent 2c2b05c commit bcfca5c

File tree

7 files changed

+101
-20
lines changed

7 files changed

+101
-20
lines changed

shuffleboard.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,62 @@
497497
}
498498
}
499499
}
500+
},
501+
{
502+
"title": "camera",
503+
"autoPopulate": false,
504+
"autoPopulatePrefix": "",
505+
"widgetPane": {
506+
"gridSize": 128.0,
507+
"showGrid": true,
508+
"hgap": 16.0,
509+
"vgap": 16.0,
510+
"titleType": 0,
511+
"tiles": {
512+
"0,0": {
513+
"size": [
514+
6,
515+
5
516+
],
517+
"content": {
518+
"_type": "Camera Stream",
519+
"_source0": "camera_server://HD_USB_Camera-output",
520+
"_title": "HD_USB_Camera-output",
521+
"_glyph": 148,
522+
"_showGlyph": false,
523+
"Crosshair/Show crosshair": false,
524+
"Crosshair/Crosshair color": "#FFFFFFFF",
525+
"Controls/Show controls": true,
526+
"Controls/Rotation": "NONE",
527+
"compression": -1.0,
528+
"fps": -1,
529+
"imageWidth": 240,
530+
"imageHeight": 120
531+
}
532+
},
533+
"6,0": {
534+
"size": [
535+
7,
536+
5
537+
],
538+
"content": {
539+
"_type": "Camera Stream",
540+
"_source0": "camera_server://USB Camera 0",
541+
"_title": "USB Camera 0",
542+
"_glyph": 148,
543+
"_showGlyph": false,
544+
"Crosshair/Show crosshair": false,
545+
"Crosshair/Crosshair color": "#FFFFFFFF",
546+
"Controls/Show controls": true,
547+
"Controls/Rotation": "NONE",
548+
"compression": -1.0,
549+
"fps": -1,
550+
"imageWidth": 240,
551+
"imageHeight": 120
552+
}
553+
}
554+
}
555+
}
500556
}
501557
],
502558
"windowGeometry": {

src/main/cpp/RobotContainer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ bool RobotContainer::InitCommands() {
310310
score_preload_balance_auto_ = std::make_shared<ScorePreloadedBalanceAuto>(swerve_drive_, arm_, intake_);
311311

312312
// swerve commands
313-
swerve_teleop_command_ = std::make_shared<TeleOpSwerveCommand>(swerve_drive_, gamepad1_, 0.9, 0.6, false); // speed mod, open loop
313+
swerve_teleop_command_ = std::make_shared<TeleOpSwerveCommand>(swerve_drive_, gamepad1_, 1, 0.4, false); // speed mod, open loop
314314
slow_swerve_teleop_command_ = std::make_shared<TeleOpSwerveCommand>(swerve_drive_, gamepad1_, 0.5, 1, true); // brake mode
315315
fast_swerve_teleop_command_ = std::make_shared<TeleOpSwerveCommand>(swerve_drive_, gamepad1_, 1.5, 0.1, false); // BOOOOOOOOOST
316316
OKC_CHECK(swerve_teleop_command_ != nullptr);

src/main/cpp/Utils.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Utils.h"
22

3+
34
namespace TeamOKC {
45
// TODO: make this a templated function.
56
bool Clamp(const double &lower, const double &upper, double *value) {
@@ -27,5 +28,9 @@ namespace TeamOKC {
2728
}
2829
return true;
2930
}
31+
32+
double Radians(double degrees) {
33+
return degrees * M_PI / 180.0;
34+
}
3035
} // namespace TeamOKC
3136

src/main/cpp/io/SwerveDriveIO.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ bool SwerveDriveIO::ProcessInputs() {
154154
// navX IMU:
155155
OKC_CHECK(hw_interface_->ahrs != nullptr);
156156
#ifdef __FRC_ROBORIO__
157-
sw_interface_->imu_yaw = hw_interface_->ahrs->GetAngle();
157+
sw_interface_->imu_yaw = hw_interface_->ahrs->GetYaw();
158158
sw_interface_->imu_pitch = hw_interface_->ahrs->GetPitch();
159159
#endif
160160
#ifndef __FRC_ROBORIO__

src/main/cpp/subsystems/SwerveDrive.cpp

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,27 +140,42 @@ bool SwerveDrive::SetIdleMode(rev::CANSparkMax::IdleMode mode) {
140140
}
141141

142142
bool 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));

src/main/include/Utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#pragma once
33

44
#include <iostream>
5+
#define _USE_MATH_DEFINES
6+
#include <math.h>
7+
58

69
#define OKC_CALL(res) \
710
if (!(res)) { \
@@ -58,9 +61,11 @@ namespace TeamOKC {
5861
} Pose;
5962

6063
bool WrapAngle(double *angle);
64+
double Radians(double degrees);
6165

6266
typedef struct arm_state_t {
6367
double extension;
6468
double rotation;
69+
6570
} ArmState;
6671
}

src/main/include/io/SwerveDriveIO.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ typedef struct swerve_drive_hardware_interface_t {
5959
typedef struct swerve_drive_software_interface_t {
6060
// SW INPUTS
6161
// IMU yaw angle
62-
double imu_yaw;
62+
float imu_yaw;
6363
float imu_pitch;
6464

6565
// Encoders

0 commit comments

Comments
 (0)