JMotor
Loading...
Searching...
No Matches
JDrivetrainSwerve.h
Go to the documentation of this file.
1#ifndef JDRIVETRAIN_SWERVE_H
2#define JDRIVETRAIN_SWERVE_H
3#include "JDrivetrainBasic.h"
5#include "JTwoDTransform.h"
6#include <Arduino.h>
7// TODO: COMMENT
11template <byte N>
18public:
20 float modPosL[N]; // left right position of swerve modules
21 float modPosF[N]; // forwards backwards position of swerve modules
22 float targetTheta[N];
23 float targetSpeed[N];
24 float deadzone = 0.0001;
25 float centerL = 0;
26 float centerF = 0;
27
28protected:
29public:
41 JDrivetrainSwerve(JSwerveModule* _swerveModule[N], float _modPosForwards[N], float _modPosLeft[N], float _centerL = 0, float _centerF = 0)
42 {
43 centerL = _centerL;
44 centerF = _centerF;
45 for (byte i = 0; i < N; i++) {
46 swerveModule[i] = _swerveModule[i];
47 modPosL[i] = _modPosLeft[i];
48 modPosF[i] = _modPosForwards[i];
49 }
50 }
51 void setCenter(float forwards, float left)
52 {
53 centerF = forwards;
54 centerL = left;
55 }
56 void run()
57 {
58 for (byte i = 0; i < N; i++) {
59 swerveModule[i]->run();
60 }
61 }
62
63 bool setEnable(bool _enable)
64 {
65 bool ret = false;
66 for (byte i = 0; i < N; i++) {
67 ret = swerveModule[i]->setEnable(_enable) || ret;
68 }
69 return ret;
70 }
71 bool enable()
72 {
73 return setEnable(true);
74 }
75 bool disable()
76 {
77 return setEnable(false);
78 }
79 bool getEnable()
80 {
81 bool ret = true;
82 for (byte i = 0; i < N; i++) {
83 ret = swerveModule[i]->getEnable() && ret;
84 }
85 return ret;
86 }
87
88 void setVel(JTwoDTransform _vel, bool _run = true)
89 {
90 if (_vel.sumAbs() <= deadzone) { // robot not told to move? angle wheels to lock in place.
91 for (byte i = 0; i < N; i++) {
92 targetSpeed[i] = 0;
93 targetTheta[i] = calcClosestAngle(degrees(atan2(modPosF[i] - centerF, modPosL[i] - centerL)), swerveModule[i]->getAngle());
94 }
95 } else { // swerve
96 for (byte i = 0; i < N; i++) {
97 float distance = sqrt(sq(modPosL[i] - centerL) + sq(modPosF[i] - centerF));
98 float vx = _vel.y + _vel.theta * (modPosF[i] - centerF) / distance;
99 float vy = _vel.x + _vel.theta * (modPosL[i] - centerL) / distance;
100 if (abs(vx) + abs(vy) > deadzone) { // don't change direction if speed is tiny
101 float targetThetaHeading = degrees(atan2(vx, vy));
102 targetTheta[i] = calcClosestAngle(targetThetaHeading, swerveModule[i]->getAngle());
103 targetSpeed[i] = sqrt(sq(vx) + sq(vy)) * cos(radians(swerveModule[i]->getAngle() - targetThetaHeading));
104 }
105 }
106 }
107 float overspeed = 1; // module's target speed / module's max speed
108 for (byte i = 0; i < N; i++) { // find highest overspeed
109 overspeed = max(overspeed, abs(targetSpeed[i]) / swerveModule[i]->getMaxSpeed());
110 }
111 for (byte i = 0; i < N; i++) { // adjust target speed (post scaling)
112 targetSpeed[i] = targetSpeed[i] / overspeed;
113 }
114 for (byte i = 0; i < N; i++) { // set modules to calculated speeds and angles
115 swerveModule[i]->set(targetSpeed[i], targetTheta[i], false);
116 }
117 }
118 static float calcClosestAngle(float target, float theta)
119 {
120 return fmod(fmod((target - theta + 90), 180) + 360, 180) - 90 + theta;
121 }
122};
123
124#endif // JDRIVETRAIN_SWERVE_H
interface for a drivetrain with only simple velocity control
Definition JDrivetrainBasic.h:8
number of swerve modules
Definition JDrivetrainSwerve.h:17
void setVel(JTwoDTransform _vel, bool _run=true)
sets a velocity for the drivetrain to move at
Definition JDrivetrainSwerve.h:88
float modPosF[N]
Definition JDrivetrainSwerve.h:21
bool getEnable()
gets the current state (enabled or disabled)
Definition JDrivetrainSwerve.h:79
float modPosL[N]
Definition JDrivetrainSwerve.h:20
float centerF
Definition JDrivetrainSwerve.h:26
void run()
call this in loop. it calls any encoder or motor update functions
Definition JDrivetrainSwerve.h:56
static float calcClosestAngle(float target, float theta)
Definition JDrivetrainSwerve.h:118
JDrivetrainSwerve(JSwerveModule *_swerveModule[N], float _modPosForwards[N], float _modPosLeft[N], float _centerL=0, float _centerF=0)
Definition JDrivetrainSwerve.h:41
float targetTheta[N]
Definition JDrivetrainSwerve.h:22
bool setEnable(bool _enable)
enables or disables movement
Definition JDrivetrainSwerve.h:63
bool enable()
enables movement
Definition JDrivetrainSwerve.h:71
float deadzone
Definition JDrivetrainSwerve.h:24
void setCenter(float forwards, float left)
Definition JDrivetrainSwerve.h:51
float targetSpeed[N]
Definition JDrivetrainSwerve.h:23
JSwerveModule * swerveModule[N]
Definition JDrivetrainSwerve.h:19
float centerL
Definition JDrivetrainSwerve.h:25
bool disable()
disables movement
Definition JDrivetrainSwerve.h:75
defines a common interface for swerve modules
Definition JSwerveModule.h:7
virtual bool setEnable(bool _enable)
set enable state of swerve module motors
virtual void run()
call this in loop() so that the swerve module can update its motors and angle control loop
virtual void set(float _speed, float _angle, bool _run=true)
set speed and angle setpoints for wheel
virtual bool getEnable()
get enable state of module
struct for storing a 2 dimensional transformation. Used for telling a drivetrain how to move.
Definition JTwoDTransform.h:9
float theta
clockwise- | counterclockwise+
Definition JTwoDTransform.h:21
float sumAbs()
sum of absolute value of the three values
Definition JTwoDTransform.h:194
float x
backwards- | forward+
Definition JTwoDTransform.h:13
float y
right- | left+
Definition JTwoDTransform.h:17