JMotor
Loading...
Searching...
No Matches
JDrivetrainFieldOriented.h
Go to the documentation of this file.
1#ifndef JDRIVETRAIN_FIELD_ORIENTED_H
2#define JDRIVETRAIN_FIELD_ORIENTED_H
4#include "JTwoDTransform.h"
5#include <Arduino.h>
8
10// void run()
11// {
12// JTwoDTransform velocity = { 0, 0, 0 };
13// velocity.theta = vel.theta;
14// velocity.x = vel.x * cos(-theta) - vel.y * sin(-theta);
15// velocity.y = vel.x * sin(-theta) + vel.y * cos(-theta);
16// drivetrain.setVel(velocity);
17// }
18// /**
19// * @brief for field-oriented mode to work, the imu heading, or an inaccurate encoder calculated heading must be given
20// * @param _heading: (float) degrees
21// */
22// void giveImuReading(float _heading)
23// {
24// imuHeading = _heading - imuHeadingOffset;
25// if (imuHeading - lastImuHeading > 180)
26// fullRotations--;
27// else if (imuHeading - lastImuHeading < -180)
28// fullRotations++;
29// lastImuHeading = imuHeading;
30// theta = imuHeading + fullRotations * 360;
31// }
32
38protected:
40 float imuHeading = 0;
42 float lastImuHeading = 0;
43 float theta = 0;
45 JTwoDTransform dist = { 0, 0, 0 };
47
48public:
50 : drivetrain(_drivetrain)
51 {
52 }
53 float getHeading()
54 {
55 return theta;
56 }
57 void giveHeading(float _heading)
58 {
59 imuHeading = _heading - imuHeadingOffset;
60 if (imuHeading - lastImuHeading > PI)
62 else if (imuHeading - lastImuHeading < -PI)
65 theta = imuHeading + fullRotations * TWO_PI;
66 }
67 void run()
68 {
70 JTwoDTransform drivetrainMovement = drivetrain.getDist(false) - lastDrivetrainDist;
71 dist += JTwoDTransform::rotate(drivetrainMovement, -theta);
73 theta += drivetrainMovement.theta;
75 }
76 bool setEnable(bool _enable)
77 {
78 return drivetrain.setEnable(_enable);
79 }
80 bool enable()
81 {
82 return drivetrain.enable();
83 }
84 bool disable()
85 {
86 return drivetrain.disable();
87 }
88 bool getEnable()
89 {
90 return drivetrain.getEnable();
91 }
92 void setVel(JTwoDTransform _vel, bool _run = false)
93 {
95 }
97 {
98 return drivetrain.getMaxVel();
99 }
100 JTwoDTransform getVel(bool _run = false)
101 {
102 JTwoDTransform _vel = drivetrain.getVel(_run);
103 return JTwoDTransform::rotate(_vel, theta);
104 }
105 JTwoDTransform getDist(bool _run = false)
106 {
107 if (_run)
108 run();
109 return dist;
110 }
118 void setDistSetpoint(JTwoDTransform _dist, bool _run = false)
119 {
120 JTwoDTransform dist = _dist - getDist(_run);
121 setDistDelta(dist, _run);
122 }
123 void setDistDelta(JTwoDTransform _dist, bool _run = false)
124 {
126 }
128 {
130 dist = { 0, 0, 0 };
131 lastDrivetrainDist = { 0, 0, 0 };
132 fullRotations = 0;
133 theta = 0;
134 }
135 void setMotorVel(float vel, unsigned char i, bool _run = false)
136 {
137 drivetrain.setMotorVel(vel, i, _run);
138 }
139 float getMotorVel(unsigned char i)
140 {
141 return drivetrain.getMotorVel(i);
142 }
143 void setMotorDistSetpoint(float distSetpoint, unsigned char i, bool _run = false)
144 {
145 drivetrain.setMotorDistSetpoint(distSetpoint, i, _run);
146 }
147 void setMotorDistDelta(float distDelta, unsigned char i, bool _run = false)
148 {
149 drivetrain.setMotorDistDelta(distDelta, i, _run);
150 }
151 unsigned char getNumberMotors()
152 {
154 }
155};
156
157#endif // JDRIVETRAIN_FIELD_ORIENTED_H
virtual bool getEnable()=0
gets the current state (enabled or disabled)
virtual void setVel(JTwoDTransform _vel, bool _run=false)=0
sets a velocity for the drivetrain to move at
virtual bool enable()=0
enables movement
virtual void run()=0
call this in loop. it calls any encoder or motor update functions
virtual bool setEnable(bool _enable)=0
enables or disables movement
virtual JTwoDTransform getMaxVel()=0
returns the approximate maximum velocity of the drivetrain, if the drivetrain is told to move at max ...
virtual bool disable()=0
disables movement
interface for a drivetrain that makes it into field oriented
Definition JDrivetrainFieldOriented.h:37
void setVel(JTwoDTransform _vel, bool _run=false)
sets a velocity for the drivetrain to move at
Definition JDrivetrainFieldOriented.h:92
void setDistDelta(JTwoDTransform _dist, bool _run=false)
Definition JDrivetrainFieldOriented.h:123
unsigned char getNumberMotors()
Definition JDrivetrainFieldOriented.h:151
void setMotorDistDelta(float distDelta, unsigned char i, bool _run=false)
Definition JDrivetrainFieldOriented.h:147
bool setEnable(bool _enable)
enables or disables movement
Definition JDrivetrainFieldOriented.h:76
JTwoDTransform getMaxVel()
returns the approximate maximum velocity of the drivetrain, if the drivetrain is told to move at max ...
Definition JDrivetrainFieldOriented.h:96
void resetDist()
Definition JDrivetrainFieldOriented.h:127
float theta
Definition JDrivetrainFieldOriented.h:43
void setMotorDistSetpoint(float distSetpoint, unsigned char i, bool _run=false)
Definition JDrivetrainFieldOriented.h:143
bool enable()
enables movement
Definition JDrivetrainFieldOriented.h:80
JTwoDTransform dist
Definition JDrivetrainFieldOriented.h:45
void setDistSetpoint(JTwoDTransform _dist, bool _run=false)
Definition JDrivetrainFieldOriented.h:118
float imuHeading
Definition JDrivetrainFieldOriented.h:40
bool disable()
disables movement
Definition JDrivetrainFieldOriented.h:84
float getMotorVel(unsigned char i)
Definition JDrivetrainFieldOriented.h:139
void run()
call this in loop. it calls any encoder or motor update functions
Definition JDrivetrainFieldOriented.h:67
float lastImuHeading
Definition JDrivetrainFieldOriented.h:42
JDrivetrainFieldOriented(JDrivetrain &_drivetrain)
Definition JDrivetrainFieldOriented.h:49
JDrivetrain & drivetrain
Definition JDrivetrainFieldOriented.h:39
JTwoDTransform getVel(bool _run=false)
Definition JDrivetrainFieldOriented.h:100
float getHeading()
Definition JDrivetrainFieldOriented.h:53
JTwoDTransform getDist(bool _run=false)
Definition JDrivetrainFieldOriented.h:105
bool getEnable()
gets the current state (enabled or disabled)
Definition JDrivetrainFieldOriented.h:88
void setMotorVel(float vel, unsigned char i, bool _run=false)
Definition JDrivetrainFieldOriented.h:135
JTwoDTransform lastDrivetrainDist
Definition JDrivetrainFieldOriented.h:46
int fullRotations
Definition JDrivetrainFieldOriented.h:44
float imuHeadingOffset
Definition JDrivetrainFieldOriented.h:41
void giveHeading(float _heading)
Definition JDrivetrainFieldOriented.h:57
defines interface for controlling any ground-based drivetrain
Definition JDrivetrain.h:10
virtual void setMotorDistDelta(float distDelta, unsigned char i, bool _run=false)
virtual JTwoDTransform getVel(bool _run=false)
virtual JTwoDTransform getDist(bool _run=false)
virtual unsigned char getNumberMotors()
virtual void setMotorVel(float vel, unsigned char i, bool _run=false)
virtual void setDistDelta(JTwoDTransform _dist, bool _run=false)
virtual float getMotorVel(unsigned char i)
virtual void resetDist()
virtual void setMotorDistSetpoint(float distSetpoint, unsigned char i, bool _run=false)
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
static JTwoDTransform rotate(JTwoDTransform tIn, float theta)
Definition JTwoDTransform.h:23