JMotor
Loading...
Searching...
No Matches
JDrivetrainMecanum.h
Go to the documentation of this file.
1#ifndef JDRIVETRAIN_MECANUM_H
2#define JDRIVETRAIN_MECANUM_H
3#include "JDrivetrain.h"
5#include "JTwoDTransform.h"
6#include <Arduino.h>
10// TODO: COMMENT
12public:
19
20 JDrivetrainMecanum(JMotorController& _FRmotor, JMotorController& _FLmotor, JMotorController& _BLmotor, JMotorController& _BRmotor, JTwoDTransform _robotToWheelScalar)
21 : motors({ _FRmotor, _FLmotor, _BLmotor, _BRmotor })
22 {
23 robotToWheelScalar = _robotToWheelScalar;
24 }
25 void run()
26 {
31 }
32 bool setEnable(bool _enable)
33 {
34 bool ret1 = motors.FRmotor.setEnable(_enable);
35 bool ret2 = motors.FLmotor.setEnable(_enable);
36 bool ret3 = motors.BLmotor.setEnable(_enable);
37 bool ret4 = motors.BRmotor.setEnable(_enable);
38 return ret1 || ret2 || ret3 || ret4;
39 }
40 bool enable()
41 {
42 return setEnable(true);
43 }
44 bool disable()
45 {
46 return setEnable(false);
47 }
48 bool getEnable()
49 {
50 bool ret1 = motors.FRmotor.getEnable();
51 bool ret2 = motors.FLmotor.getEnable();
52 bool ret3 = motors.BLmotor.getEnable();
53 bool ret4 = motors.BRmotor.getEnable();
54 return ret1 || ret2 || ret3 || ret4;
55 }
56 JTwoDTransform getVel(bool _run = false)
57 {
58 if (_run)
59 run();
66 toTransform(m, t);
67 return t;
68 }
69 JTwoDTransform getDist(bool _run = false)
70 {
71 if (_run)
72 run();
79 toTransform(m, t);
80 return t;
81 }
82 void setVel(JTwoDTransform _vel, bool _run = false)
83 {
85 toWheels(_vel, m);
86 motors.FRmotor.setVel(m.FRmotor, false);
87 motors.FLmotor.setVel(m.FLmotor, false);
88 motors.BLmotor.setVel(m.BLmotor, false);
89 motors.BRmotor.setVel(m.BRmotor, false);
90 if (_run)
91 run();
92 }
93 void setDistSetpoint(JTwoDTransform _dist, bool _run = false)
94 {
96 toWheels(_dist, m);
101 if (_run)
102 run();
103 }
104 void setDistDelta(JTwoDTransform _dist, bool _run = false)
105 {
106 MotorFloats m;
107 toWheels(_dist, m);
112 if (_run)
113 run();
114 }
123 {
124 // assumes all the max vels are similar, doesn't find a minimum
125 // can't use toTransform since toTransform adds negatives that causes calculations to say x and theta max vel are zero
127 float v = abs(motors.FRmotor.getMaxVel()) + abs(motors.FLmotor.getMaxVel()) + abs(motors.BLmotor.getMaxVel()) + abs(motors.BRmotor.getMaxVel());
128 t.x = v / 4.0 / robotToWheelScalar.x;
129 t.y = v / 4.0 / robotToWheelScalar.y;
130 t.theta = v / 4.0 / robotToWheelScalar.theta;
131 return t;
132 }
133 float getMotorVel(unsigned char i)
134 {
135 if (i == 0)
136 return motors.FRmotor.getVel();
137 if (i == 1)
138 return motors.FLmotor.getVel();
139 if (i == 2)
140 return motors.BLmotor.getVel();
141 if (i == 3)
142 return motors.BRmotor.getVel();
143 return 0;
144 }
145 void setMotorVel(float vel, unsigned char i, bool _run = false)
146 {
147 if (i == 0)
148 motors.FRmotor.setVel(vel, _run);
149 if (i == 1)
150 motors.FLmotor.setVel(vel, _run);
151 if (i == 2)
152 motors.BLmotor.setVel(vel, _run);
153 if (i == 3)
154 motors.BRmotor.setVel(vel, _run);
155 }
156 void setMotorDistSetpoint(float distSetpoint, unsigned char i, bool _run = false)
157 {
158 if (i == 0)
159 motors.FRmotor.setPosSetpoint(distSetpoint, _run);
160 if (i == 1)
161 motors.FLmotor.setPosSetpoint(distSetpoint, _run);
162 if (i == 2)
163 motors.BLmotor.setPosSetpoint(distSetpoint, _run);
164 if (i == 3)
165 motors.BRmotor.setPosSetpoint(distSetpoint, _run);
166 }
167 void setMotorDistDelta(float distDelta, unsigned char i, bool _run = false)
168 {
169 if (i == 0)
170 motors.FRmotor.setPosDelta(distDelta, _run);
171 if (i == 1)
172 motors.FLmotor.setPosDelta(distDelta, _run);
173 if (i == 2)
174 motors.BLmotor.setPosDelta(distDelta, _run);
175 if (i == 3)
176 motors.BRmotor.setPosDelta(distDelta, _run);
177 }
178 unsigned char getNumberMotors()
179 {
180 return 4;
181 }
187 {
188 robotToWheelScalar = scalar;
189 }
190
191protected:
193 struct MotorFloats {
194 float FRmotor;
195 float FLmotor;
196 float BLmotor;
197 float BRmotor;
198 };
207 {
208 w.x = (m.FRmotor + m.FLmotor + m.BLmotor + m.BRmotor) / 4.0 / robotToWheelScalar.x;
209 w.y = (m.FRmotor - m.FLmotor + m.BLmotor - m.BRmotor) / 4.0 / robotToWheelScalar.y;
210 w.theta = (m.FRmotor - m.FLmotor - m.BLmotor + m.BRmotor) / 4.0 / robotToWheelScalar.theta;
211 }
212};
213#endif // JDRIVETRAIN_MECANUM_H
defines interface for controlling any ground-based drivetrain
Definition JDrivetrain.h:10
Drivetrain that controls a mecanum wheeled robot.
Definition JDrivetrainMecanum.h:11
void setDistSetpoint(JTwoDTransform _dist, bool _run=false)
Definition JDrivetrainMecanum.h:93
JDrivetrainMecanum(JMotorController &_FRmotor, JMotorController &_FLmotor, JMotorController &_BLmotor, JMotorController &_BRmotor, JTwoDTransform _robotToWheelScalar)
Definition JDrivetrainMecanum.h:20
void setMotorDistDelta(float distDelta, unsigned char i, bool _run=false)
Definition JDrivetrainMecanum.h:167
JTwoDTransform getVel(bool _run=false)
Definition JDrivetrainMecanum.h:56
JTwoDTransform getDist(bool _run=false)
Definition JDrivetrainMecanum.h:69
void setRobotToWheelScalar(JTwoDTransform scalar)
Definition JDrivetrainMecanum.h:186
void toTransform(MotorFloats m, JTwoDTransform &w)
Definition JDrivetrainMecanum.h:206
void setVel(JTwoDTransform _vel, bool _run=false)
sets a velocity for the drivetrain to move at
Definition JDrivetrainMecanum.h:82
bool enable()
enables movement
Definition JDrivetrainMecanum.h:40
JTwoDTransform robotToWheelScalar
Definition JDrivetrainMecanum.h:192
void setDistDelta(JTwoDTransform _dist, bool _run=false)
Definition JDrivetrainMecanum.h:104
void run()
call this in loop. it calls any encoder or motor update functions
Definition JDrivetrainMecanum.h:25
bool getEnable()
gets the current state (enabled or disabled)
Definition JDrivetrainMecanum.h:48
struct JDrivetrainMecanum::Motors motors
void setMotorVel(float vel, unsigned char i, bool _run=false)
Definition JDrivetrainMecanum.h:145
JTwoDTransform getRobotToWheelScalar()
Definition JDrivetrainMecanum.h:182
unsigned char getNumberMotors()
Definition JDrivetrainMecanum.h:178
void toWheels(JTwoDTransform t, MotorFloats &m)
Definition JDrivetrainMecanum.h:199
bool setEnable(bool _enable)
enables or disables movement
Definition JDrivetrainMecanum.h:32
void setMotorDistSetpoint(float distSetpoint, unsigned char i, bool _run=false)
Definition JDrivetrainMecanum.h:156
bool disable()
disables movement
Definition JDrivetrainMecanum.h:44
float getMotorVel(unsigned char i)
Definition JDrivetrainMecanum.h:133
void resetDist()
Definition JDrivetrainMecanum.h:115
JTwoDTransform getMaxVel()
returns the approximate maximum velocity of the drivetrain, if the drivetrain is told to move at max ...
Definition JDrivetrainMecanum.h:122
virtual float getVel()=0
get current velocity of motor
virtual void setVel(float vel, bool _run=true)=0
set velocity for controller
virtual bool getEnable()=0
is the controller enabled
virtual float getMaxVel()=0
How fast of a motor speed setting would get adjusted to full motor power.
virtual void run()=0
update driver
virtual bool setEnable(bool _enable)=0
change whether motor controller is enabled
This class defines a common interface for classes which control velocity and position of a motor cont...
Definition JMotorController.h:8
virtual float resetPos()=0
reset what position the controller thinks it's in
virtual float getPos()=0
get what position the motor is currently at
virtual bool setPosSetpoint(float _posSetpoint, bool _run=true)=0
set position for motor to drive towards position as fast as possible (setpoint for control loop if av...
virtual bool setPosDelta(float _posDelta, bool _run=true, bool _resetPos=false)=0
alternative method for setting velocity that uses setPosSetpoint
Definition JDrivetrainMecanum.h:193
float BLmotor
Definition JDrivetrainMecanum.h:196
float FRmotor
Definition JDrivetrainMecanum.h:194
float BRmotor
Definition JDrivetrainMecanum.h:197
float FLmotor
Definition JDrivetrainMecanum.h:195
Definition JDrivetrainMecanum.h:13
JMotorController & FLmotor
Definition JDrivetrainMecanum.h:15
JMotorController & BLmotor
Definition JDrivetrainMecanum.h:16
JMotorController & BRmotor
Definition JDrivetrainMecanum.h:17
JMotorController & FRmotor
Definition JDrivetrainMecanum.h:14
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 x
backwards- | forward+
Definition JTwoDTransform.h:13
float y
right- | left+
Definition JTwoDTransform.h:17