JMotor
Loading...
Searching...
No Matches
JDrivetrainTwoSide.h
Go to the documentation of this file.
1#ifndef JDRIVETRAIN_TWO_SIDE_H
2#define JDRIVETRAIN_TWO_SIDE_H
3#include "JDrivetrain.h"
5#include "JTwoDTransform.h"
6#include <Arduino.h>
12public:
15
16protected:
17 float width;
18 float leftVel;
19 float rightVel;
20 float leftDist;
21 float rightDist;
22
26
27public:
29 : left(_left)
30 , right(_right)
31 {
32 width = _width;
33
34 leftVel = 0;
35 rightVel = 0;
36 leftDist = 0;
37 rightDist = 0;
38 }
39 void run()
40 {
41 left.run();
42 right.run();
43
48
49 vel = { (leftVel + rightVel) / (float)2.0, 0, (rightVel - leftVel) / width };
50 dist = { (leftDist + rightDist) / (float)2.0, 0, (rightDist - leftDist) / width };
51 maxVel = { min(left.getMaxVel(), right.getMaxVel()), 0, min(left.getMaxVel(), right.getMaxVel()) * (float)2.0 / width };
52 }
53 bool setEnable(bool _enable)
54 {
55 bool retl = left.setEnable(_enable);
56 bool retr = right.setEnable(_enable);
57 return retl || retr;
58 }
59 bool enable()
60 {
61 return setEnable(true);
62 }
63 bool disable()
64 {
65 return setEnable(false);
66 }
67 bool getEnable()
68 {
69 return left.getEnable() || right.getEnable();
70 }
71 JTwoDTransform getVel(bool _run = false)
72 {
73 if (_run)
74 run();
75 return vel;
76 }
77 JTwoDTransform getDist(bool _run = false)
78 {
79 if (_run)
80 run();
81 return dist;
82 }
83 void setVel(JTwoDTransform _vel, bool _run = false)
84 {
85 float rotation = _vel.theta * width / 2;
86 left.setVel(_vel.x - rotation, false);
87 right.setVel(_vel.x + rotation, false);
88 if (_run)
89 run();
90 }
91 void setDistSetpoint(JTwoDTransform _dist, bool _run = false)
92 {
93 float rotation = _dist.theta * width / 2;
94 left.setPosSetpoint(_dist.x - rotation, false);
95 right.setPosSetpoint(_dist.x + rotation, false);
96 if (_run)
97 run();
98 }
99 void setDistDelta(JTwoDTransform _dist, bool _run = false)
100 {
101 float rotation = _dist.theta * width / 2;
102 left.setPosDelta(_dist.x - rotation, false);
103 right.setPosDelta(_dist.x + rotation, false);
104 if (_run)
105 run();
106 }
108 {
109 dist = { 0, 0, 0 };
110 left.resetPos();
111 right.resetPos();
112 }
114 {
115 return maxVel;
116 }
117 float getMotorVel(unsigned char i)
118 {
119 if (i == 0)
120 return leftVel;
121 if (i == 1)
122 return rightVel;
123 return 0;
124 }
125 void setMotorVel(float vel, unsigned char i, bool _run = false)
126 {
127 if (i == 0)
128 left.setVel(vel, _run);
129 if (i == 1)
130 right.setVel(vel, _run);
131 }
132 void setMotorDistSetpoint(float distSetpoint, unsigned char i, bool _run = false)
133 {
134 if (i == 0)
135 left.setPosSetpoint(distSetpoint, _run);
136 if (i == 1)
137 right.setPosSetpoint(distSetpoint, _run);
138 }
139 void setMotorDistDelta(float distDelta, unsigned char i, bool _run = false)
140 {
141 if (i == 0)
142 left.setPosDelta(distDelta, _run);
143 if (i == 1)
144 right.setPosDelta(distDelta, _run);
145 }
146 unsigned char getNumberMotors()
147 {
148 return 2;
149 }
150
151 float getWidth()
152 {
153 return width;
154 }
155
156 void setWidth(float _width)
157 {
158 width = _width;
159 }
160};
161#endif // JDRIVETRAIN_TWO_SIDE_H
defines interface for controlling any ground-based drivetrain
Definition JDrivetrain.h:10
child class of JDrivetrain for controlling a drivetrain with one JMotorController for each side of th...
Definition JDrivetrainTwoSide.h:11
JTwoDTransform vel
Definition JDrivetrainTwoSide.h:23
float leftDist
Definition JDrivetrainTwoSide.h:20
JTwoDTransform maxVel
Definition JDrivetrainTwoSide.h:25
void resetDist()
Definition JDrivetrainTwoSide.h:107
void setMotorDistSetpoint(float distSetpoint, unsigned char i, bool _run=false)
Definition JDrivetrainTwoSide.h:132
JDrivetrainTwoSide(JMotorController &_left, JMotorController &_right, float _width)
Definition JDrivetrainTwoSide.h:28
JTwoDTransform getMaxVel()
returns the approximate maximum velocity of the drivetrain, if the drivetrain is told to move at max ...
Definition JDrivetrainTwoSide.h:113
float rightVel
Definition JDrivetrainTwoSide.h:19
void setMotorVel(float vel, unsigned char i, bool _run=false)
Definition JDrivetrainTwoSide.h:125
float width
Definition JDrivetrainTwoSide.h:17
bool setEnable(bool _enable)
enables or disables movement
Definition JDrivetrainTwoSide.h:53
JTwoDTransform getVel(bool _run=false)
Definition JDrivetrainTwoSide.h:71
bool getEnable()
gets the current state (enabled or disabled)
Definition JDrivetrainTwoSide.h:67
void setDistSetpoint(JTwoDTransform _dist, bool _run=false)
Definition JDrivetrainTwoSide.h:91
JTwoDTransform dist
Definition JDrivetrainTwoSide.h:24
float leftVel
Definition JDrivetrainTwoSide.h:18
float getWidth()
Definition JDrivetrainTwoSide.h:151
bool disable()
disables movement
Definition JDrivetrainTwoSide.h:63
void setVel(JTwoDTransform _vel, bool _run=false)
sets a velocity for the drivetrain to move at
Definition JDrivetrainTwoSide.h:83
void run()
call this in loop. it calls any encoder or motor update functions
Definition JDrivetrainTwoSide.h:39
JMotorController & left
Definition JDrivetrainTwoSide.h:13
void setWidth(float _width)
Definition JDrivetrainTwoSide.h:156
float getMotorVel(unsigned char i)
Definition JDrivetrainTwoSide.h:117
bool enable()
enables movement
Definition JDrivetrainTwoSide.h:59
JTwoDTransform getDist(bool _run=false)
Definition JDrivetrainTwoSide.h:77
float rightDist
Definition JDrivetrainTwoSide.h:21
void setMotorDistDelta(float distDelta, unsigned char i, bool _run=false)
Definition JDrivetrainTwoSide.h:139
unsigned char getNumberMotors()
Definition JDrivetrainTwoSide.h:146
void setDistDelta(JTwoDTransform _dist, bool _run=false)
Definition JDrivetrainTwoSide.h:99
JMotorController & right
Definition JDrivetrainTwoSide.h:14
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
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