RSeries astromech firmware
TankDrivePWM.h
Go to the documentation of this file.
1 #ifndef TankDrivePWM_h
2 #define TankDrivePWM_h
3 
4 #include "ServoDispatch.h"
5 #include "drive/TankDrive.h"
6 
22 class TankDrivePWM : public TankDrive
23 {
24 public:
29  TankDrivePWM(ServoDispatch& dispatch, uint8_t leftNum, uint8_t rightNum, JoystickController& driveStick) :
30  TankDrivePWM(dispatch, leftNum, rightNum, 0, driveStick)
31  {
32  }
33 
38  TankDrivePWM(ServoDispatch& dispatch, uint8_t leftNum, uint8_t rightNum, int throttleNum, JoystickController& driveStick) :
40  fDispatch(dispatch),
41  fLeft(leftNum),
42  fRight(rightNum),
43  fThrottle(throttleNum)
44  {
45  setMaxSpeed(1.0f);
46  }
47 
48  virtual void setup() override
49  {
50  }
51 
52  virtual void stop() override
53  {
54  fDispatch.moveTo(fLeft, 0.5);
55  fDispatch.moveTo(fRight, 0.5);
57  }
58 
59 protected:
61  uint8_t fLeft;
62  uint8_t fRight;
63  int fThrottle;
64 
65  virtual void motor(float left, float right, float throttle) override
66  {
67  left = map(left, -1.0f, 1.0f, 0.0f, 1.0f);
68  right = map(right, -1.0f, 1.0f, 0.0f, 1.0f);
69 
70  Serial.print("M "); Serial.print(left); Serial.print(", "); Serial.print(right);Serial.print(", "); Serial.println(throttle);
71  throttle = 1.0;
72  if (fThrottle != -1)
73  {
74  fDispatch.moveTo(fLeft, left);
75  fDispatch.moveTo(fRight, right);
76  fDispatch.moveTo(fThrottle, throttle);
77  }
78  else
79  {
80  left *= throttle;
81  right *= throttle;
82  fDispatch.moveTo(fLeft, left);
83  fDispatch.moveTo(fRight, right);
84  }
85  }
86 
87  virtual bool hasThrottle() override
88  {
89  return (fThrottle != -1);
90  }
91 
92  static float map(float x, float in_min, float in_max, float out_min, float out_max)
93  {
94  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
95  }
96 };
97 #endif
TankDrivePWM::hasThrottle
virtual bool hasThrottle() override
Definition: TankDrivePWM.h:87
ServoDispatch.h
TankDrivePWM::fLeft
uint8_t fLeft
Definition: TankDrivePWM.h:61
TankDrivePWM::fDispatch
ServoDispatch & fDispatch
Definition: TankDrivePWM.h:60
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
TankDrivePWM::motor
virtual void motor(float left, float right, float throttle) override
Definition: TankDrivePWM.h:65
TankDrive
Base template of automatic forwarder from i2c to CommandEvent.
Definition: TankDrive.h:37
TankDrivePWM::fThrottle
int fThrottle
Definition: TankDrivePWM.h:63
TankDrivePWM
Base template of automatic forwarder from i2c to CommandEvent.
Definition: TankDrivePWM.h:22
ServoDispatch
Servo interace implemented eitehr by ServoDispatchPCA9685 or ServoDispatchDirect.
Definition: ServoDispatch.h:83
JoystickController
Definition: JoystickController.h:4
TankDrivePWM::stop
virtual void stop() override
Definition: TankDrivePWM.h:52
TankDrivePWM::TankDrivePWM
TankDrivePWM(ServoDispatch &dispatch, uint8_t leftNum, uint8_t rightNum, int throttleNum, JoystickController &driveStick)
Constructor.
Definition: TankDrivePWM.h:38
TankDrivePWM::map
static float map(float x, float in_min, float in_max, float out_min, float out_max)
Definition: TankDrivePWM.h:92
TankDrivePWM::fRight
uint8_t fRight
Definition: TankDrivePWM.h:62
TankDrive.h
TankDrive::stop
virtual void stop()
Definition: TankDrive.h:261
TankDrivePWM::TankDrivePWM
TankDrivePWM(ServoDispatch &dispatch, uint8_t leftNum, uint8_t rightNum, JoystickController &driveStick)
Constructor.
Definition: TankDrivePWM.h:29
TankDrivePWM::setup
virtual void setup() override
Subclasses must implement this function to perform any necessary setup that cannot happen in the cons...
Definition: TankDrivePWM.h:48