RSeries astromech firmware
TankDriveRoboteq.h
Go to the documentation of this file.
1 #ifndef TankDriveRoboteq_h
2 #define TankDriveRoboteq_h
3 
4 #include "ServoDispatch.h"
5 #include "drive/TankDrive.h"
6 #include "drive/TurtleDrive.h"
7 
23 class TankDriveRoboteq : public TankDrive, public TurtleDrive
24 {
25 public:
32  TankDriveRoboteq(HardwareSerial& serial, JoystickController& driveStick) :
34  fSerial(&serial)
35  {
36  }
37 
42  TankDriveRoboteq(HardwareSerial& serial, ServoDispatch& dispatch, int leftNum, int rightNum, JoystickController& driveStick) :
43  TankDriveRoboteq(serial, dispatch, leftNum, rightNum, -1, driveStick)
44  {
45  }
46 
51  TankDriveRoboteq(HardwareSerial& serial, ServoDispatch& dispatch, int leftNum, int rightNum, int throttleNum, JoystickController& driveStick) :
53  fSerial(&serial),
54  fDispatch(&dispatch),
55  fLeft(leftNum),
56  fRight(rightNum),
57  fThrottle(throttleNum)
58  {
59  setMaxSpeed(1.0f);
60  }
61 
66  TankDriveRoboteq(ServoDispatch& dispatch, int leftNum, int rightNum, JoystickController& driveStick) :
67  TankDriveRoboteq(dispatch, leftNum, rightNum, -1, driveStick)
68  {
69  }
70 
75  TankDriveRoboteq(ServoDispatch& dispatch, int leftNum, int rightNum, int throttleNum, JoystickController& driveStick) :
77  fDispatch(&dispatch),
78  fLeft(leftNum),
79  fRight(rightNum),
80  fThrottle(throttleNum)
81  {
82  setMaxSpeed(1.0f);
83  }
84 
85  virtual void setup() override
86  {
87  // stop odometry
88  write("#\r");
89 
90  // stop motors
91  write("!G 1 0\r");
92  write("!S 1 0\r");
93  write("!G 2 0\r");
94  write("!S 2 0\r");
95 
96  // disable echo
97  write("^ECHOF 1\r");
98 
99  // enable watchdog timer (100 ms)
100  write("^RWD 100\r");
101  }
102 
103  virtual void stop() override
104  {
105  if (fCommandMode)
106  {
107  writeIntCmd("!S", 1, 0);
108  writeIntCmd("!S", 2, 0);
109  writeIntCmd("!MS", 1);
110  writeIntCmd("!MS", 2);
111  }
112  TankDrive::stop();
113  }
114 
115  virtual bool enterCommandMode()
116  {
117  if (!isCommandModeActive() && fSerial != nullptr)
118  {
120  if (stick == nullptr)
121  {
122  stop();
123  // Closed Loop Count
124  write("^MMOD 1 3\r");
125  write("^MMOD 2 3\r");
126  fCommandMode = true;
127  DEBUG_PRINTLN("ENTER COMMAND MODE");
128  }
129  }
130  return fCommandMode;
131  }
132 
133  virtual void leaveCommandMode()
134  {
135  if (fCommandMode)
136  {
137  // The Roboteq script should switch modes automatically if PWM is enabled
138  stop();
139  write("^MMOD 1 6\r");
140  write("^MMOD 2 6\r");
141  fCommandMode = false;
142  DEBUG_PRINTLN("LEAVE COMMAND MODE");
143  }
144  }
145 
146  virtual bool isCommandModeActive()
147  {
149  if (fCommandMode && stick != nullptr)
150  {
151  // Disable command mode if the stick becomes active
153  }
154  return fCommandMode;
155  }
156 
157  virtual void moveMillimeters(double mm, float speed = 0.1)
158  {
159  if (isCommandModeActive())
160  {
161  int count = getMoveDistanceCount(mm);
162  speed = max(min(speed, 1.0f), 0.0f);
163  writeIntCmd("!S", 1, 1000 * speed);
164  writeIntCmd("!S", 2, 1000 * speed);
165  writeIntCmd("!PR", 1, count);
166  writeIntCmd("!PR", 2, count);
167  }
168  }
169 
170  virtual void turnDegrees(double degrees, float speed = 0.1)
171  {
172  if (isCommandModeActive())
173  {
174  writeIntCmd("!S", 1, 1000 * speed);
175  writeIntCmd("!S", 2, 1000 * speed);
176  speed = max(min(speed, 1.0f), 0.0f);
177  if (degrees < 0)
178  {
179  int count = getTurnDistanceCount(-degrees);
180  writeIntCmd("!PR", 1, count);
181  writeIntCmd("!PR", 2, -count);
182  }
183  else
184  {
185  int count = getTurnDistanceCount(degrees);
186  writeIntCmd("!PR", 1, -count);
187  writeIntCmd("!PR", 2, count);
188  }
189  }
190  }
191 
192 protected:
193  HardwareSerial* fSerial = NULL;
195  bool fCommandMode = false;
196  int fLeft = -1;
197  int fRight = -1;
198  int fThrottle = -1;
199 
200  virtual void motor(float left, float right, float throttle) override
201  {
202  if (fDispatch != NULL && fLeft != -1 && fRight != -1)
203  {
204  left = map(left, -1.0f, 1.0f, 0.0f, 1.0f);
205  right = map(right, -1.0f, 1.0f, 0.0f, 1.0f);
206 
207  // Serial.print("M "); Serial.print(left); Serial.print(", "); Serial.print(right);Serial.print(", "); Serial.println(throttle);
208  if (fThrottle != -1)
209  {
210  fDispatch->moveTo(fLeft, left);
211  fDispatch->moveTo(fRight, right);
212  fDispatch->moveTo(fThrottle, throttle);
213  }
214  else
215  {
216  left *= throttle;
217  right *= throttle;
218  fDispatch->moveTo(fLeft, left);
219  fDispatch->moveTo(fRight, right);
220  }
221  }
222  else if (fSerial != NULL && !isCommandModeActive())
223  {
224  left *= throttle;
225  right *= throttle;
226  writeIntCmd("!S", 1, left * 1000);
227  writeIntCmd("!S", 2, right * 1000);
228  }
229  }
230 
231  virtual bool hasThrottle() override
232  {
233  return (fDispatch != NULL && fThrottle != -1);
234  }
235 
236  void writeIntCmd(const char* cmd, int arg1)
237  {
238  char buf[100];
239  snprintf(buf, sizeof(buf), "%s %d\r", cmd, arg1);
240  write(buf);
241  #ifdef USE_MOTOR_DEBUG
242  {
243  // remove carriage return
244  buf[strlen(buf)-1] = 0;
245  MOTOR_DEBUG_PRINT("[ROBOTEQ] : ");
246  MOTOR_DEBUG_PRINTLN(buf);
247  }
248  #endif
249  }
250 
251  void writeIntCmd(const char* cmd, int arg1, int arg2)
252  {
253  char buf[100];
254  snprintf(buf, sizeof(buf), "%s %d %d\r", cmd, arg1, arg2);
255  write(buf);
256  #ifdef USE_MOTOR_DEBUG
257  {
258  // remove carriage return
259  buf[strlen(buf)-1] = 0;
260  MOTOR_DEBUG_PRINT("[ROBOTEQ] : ");
261  MOTOR_DEBUG_PRINTLN(buf);
262  }
263  #endif
264  }
265 
266  void write(const char* cmd)
267  {
268  if (fSerial != NULL)
269  {
270  fSerial->write(cmd);
271  fSerial->flush();
272  }
273  }
274 
275  static float map(float x, float in_min, float in_max, float out_min, float out_max)
276  {
277  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
278  }
279 };
280 #endif
TankDriveRoboteq::isCommandModeActive
virtual bool isCommandModeActive()
Definition: TankDriveRoboteq.h:146
ServoDispatch.h
TankDriveRoboteq::hasThrottle
virtual bool hasThrottle() override
Definition: TankDriveRoboteq.h:231
TankDriveRoboteq::leaveCommandMode
virtual void leaveCommandMode()
Definition: TankDriveRoboteq.h:133
TurtleDrive
Definition: TurtleDrive.h:1
TankDriveRoboteq::map
static float map(float x, float in_min, float in_max, float out_min, float out_max)
Definition: TankDriveRoboteq.h:275
TurtleDrive::getMoveDistanceCount
int getMoveDistanceCount(double millimeters)
Definition: TurtleDrive.h:13
TankDriveRoboteq
Definition: TankDriveRoboteq.h:23
TankDriveRoboteq::stop
virtual void stop() override
Definition: TankDriveRoboteq.h:103
DEBUG_PRINTLN
#define DEBUG_PRINTLN(s)
Definition: ReelTwo.h:188
TankDriveRoboteq::TankDriveRoboteq
TankDriveRoboteq(ServoDispatch &dispatch, int leftNum, int rightNum, int throttleNum, JoystickController &driveStick)
Constructor.
Definition: TankDriveRoboteq.h:75
TankDriveRoboteq::fSerial
HardwareSerial * fSerial
Definition: TankDriveRoboteq.h:193
TankDriveRoboteq::TankDriveRoboteq
TankDriveRoboteq(ServoDispatch &dispatch, int leftNum, int rightNum, JoystickController &driveStick)
Constructor.
Definition: TankDriveRoboteq.h:66
TankDrive::driveStick
void driveStick(JoystickController *stick, float speedModifier)
Definition: TankDrive.h:346
ServoDispatch::moveTo
void moveTo(uint16_t num, uint32_t startDelay, uint32_t moveTime, float pos)
Definition: ServoDispatch.h:132
TankDrive::setMaxSpeed
void setMaxSpeed(float modifier)
Definition: TankDrive.h:137
TurtleDrive::getTurnDistanceCount
int getTurnDistanceCount(double turnDegrees)
Definition: TurtleDrive.h:18
TankDriveRoboteq::writeIntCmd
void writeIntCmd(const char *cmd, int arg1)
Definition: TankDriveRoboteq.h:236
TankDrive
Base template of automatic forwarder from i2c to CommandEvent.
Definition: TankDrive.h:37
TankDriveRoboteq::fThrottle
int fThrottle
Definition: TankDriveRoboteq.h:198
TankDriveRoboteq::fDispatch
ServoDispatch * fDispatch
Definition: TankDriveRoboteq.h:194
TankDriveRoboteq::turnDegrees
virtual void turnDegrees(double degrees, float speed=0.1)
Definition: TankDriveRoboteq.h:170
MOTOR_DEBUG_PRINTLN
#define MOTOR_DEBUG_PRINTLN(s)
Definition: TankDrive.h:17
TankDriveRoboteq::TankDriveRoboteq
TankDriveRoboteq(HardwareSerial &serial, ServoDispatch &dispatch, int leftNum, int rightNum, int throttleNum, JoystickController &driveStick)
Constructor.
Definition: TankDriveRoboteq.h:51
TankDriveRoboteq::fRight
int fRight
Definition: TankDriveRoboteq.h:197
MOTOR_DEBUG_PRINT
#define MOTOR_DEBUG_PRINT(s)
Definition: TankDrive.h:16
TankDriveRoboteq::TankDriveRoboteq
TankDriveRoboteq(HardwareSerial &serial, JoystickController &driveStick)
Constructor.
Definition: TankDriveRoboteq.h:32
ServoDispatch
Servo interace implemented eitehr by ServoDispatchPCA9685 or ServoDispatchDirect.
Definition: ServoDispatch.h:83
TankDriveRoboteq::fLeft
int fLeft
Definition: TankDriveRoboteq.h:196
JoystickController
Definition: JoystickController.h:4
TurtleDrive.h
TankDrive::getActiveStick
JoystickController * getActiveStick()
Definition: TankDrive.h:268
TankDriveRoboteq::motor
virtual void motor(float left, float right, float throttle) override
Definition: TankDriveRoboteq.h:200
TankDrive.h
TankDriveRoboteq::enterCommandMode
virtual bool enterCommandMode()
Definition: TankDriveRoboteq.h:115
TankDriveRoboteq::moveMillimeters
virtual void moveMillimeters(double mm, float speed=0.1)
Definition: TankDriveRoboteq.h:157
TankDriveRoboteq::TankDriveRoboteq
TankDriveRoboteq(HardwareSerial &serial, ServoDispatch &dispatch, int leftNum, int rightNum, JoystickController &driveStick)
Constructor.
Definition: TankDriveRoboteq.h:42
TankDrive::stop
virtual void stop()
Definition: TankDrive.h:261
TankDriveRoboteq::setup
virtual void setup() override
Subclasses must implement this function to perform any necessary setup that cannot happen in the cons...
Definition: TankDriveRoboteq.h:85
TankDriveRoboteq::fCommandMode
bool fCommandMode
Definition: TankDriveRoboteq.h:195
TankDriveRoboteq::writeIntCmd
void writeIntCmd(const char *cmd, int arg1, int arg2)
Definition: TankDriveRoboteq.h:251
TankDriveRoboteq::write
void write(const char *cmd)
Definition: TankDriveRoboteq.h:266