RSeries astromech firmware
CytronSmartDriveDuoDriver.h
Go to the documentation of this file.
1 /*
2 Arduino Library for Cytron SmartDriveDuo
3 
4 Permission to use, copy, modify, and/or distribute this software for any
5 purpose with or without fee is hereby granted, provided that the above
6 copyright notice and this permission notice appear in all copies.
7 
8 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
12 RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
13 NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
14 USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16 
18 {
19 public:
26  CytronSmartDriveDuoDriver(byte address, Stream& port, uint8_t initialByte = 0x80) :
27  fPort(&port),
28  fAddress(address & 0x7),
29  fInitialByte(initialByte)
30  {
31  }
32 
37  inline byte address() const
38  {
39  return fAddress;
40  }
41 
46  inline void setAddress(byte addr)
47  {
48  fAddress = (addr & 0x7);
49  }
50 
55  void autobaud(boolean dontWait = false) const
56  {
57  if (fPort != nullptr)
58  {
59  if (!dontWait)
60  {
61  delay(1000);
62  }
63  fPort->write(&fInitialByte, 1);
64  fPort->flush();
65  if (!dontWait)
66  {
67  delay(500);
68  }
69  }
70  }
71 
76  void turn(int power)
77  {
78  fTurnPower = power;
79  }
80 
85  void drive(int power)
86  {
87  int turn = map(fTurnPower, -127, 127, -255, 255);
88  int throttle = map(power, -127, 127, -255, 255);
89  int left = throttle + turn;
90  int right = throttle - turn;
91  float scaleLeft = abs(left/255.0);
92  float scaleRight = abs(right/255.0);
93  float scaleMax = max(scaleLeft, scaleRight);
94  scaleMax = max(1.0f, scaleMax);
95  left = int(constrain(left/scaleMax, -255, 255));
96  right = int(constrain(right/scaleMax, -255, 255));
97  motor(map(left, -255, 255, -127, 127),
98  map(right, -255, 255, -127, 127));
99  }
100 
101  void motor(int power)
102  {
103  const uint8_t headerByte = 0x85;
104 
105  // Left motor
106  uint8_t addressByte = fAddress;
107  uint8_t commandByte = map(power, -127, 127, 0, 255);
108  uint8_t checksum = headerByte + addressByte + commandByte;
109  fPort->write(&headerByte, 1);
110  fPort->write(&addressByte, 1);
111  fPort->write(&commandByte, 1);
112  fPort->write(&checksum, 1);
113  }
114 
119  void motor(int powerLeft, int powerRight)
120  {
121  printf("MOTOR{%d}:%d:%d\n", address(), powerLeft, powerRight);
122  const uint8_t headerByte = 0x85;
123 
124  // Left motor
125  uint8_t addressByte = fAddress;
126  uint8_t commandByte = map(powerLeft, -127, 127, 0, 255);
127  uint8_t checksum = headerByte + addressByte + commandByte;
128  printf(" LEFT: address=%02X cmd=%02X checksum=%02X\n", addressByte, commandByte, checksum);
129  fPort->write(&headerByte, 1);
130  fPort->write(&addressByte, 1);
131  fPort->write(&commandByte, 1);
132  fPort->write(&checksum, 1);
133 
134  // Right motor
135  addressByte = (fAddress | 0x40);
136  commandByte = map(powerRight, -127, 127, 0, 255);
137  checksum = headerByte + addressByte + commandByte;
138  printf(" RIGHT: address=%02X cmd=%02X checksum=%02X\n", addressByte, commandByte, checksum);
139  fPort->write(&headerByte, 1);
140  fPort->write(&addressByte, 1);
141  fPort->write(&commandByte, 1);
142  fPort->write(&checksum, 1);
143 
144  fTurnPower = 0;
145  }
146 
147  void stop()
148  {
149  motor(0, 0);
150  }
151 
152  // Not supported
153  void setDeadband(byte value)
154  {
155  }
156 
157  // Not supported
158  void setTimeout(int milliseconds)
159  {
160  }
161 
162 private:
163  Stream* fPort;
164  uint8_t fAddress;
165  uint8_t fInitialByte;
166  uint32_t fStartTime;
167  int fTurnPower = 0;
168 };
169 
171 {
172 public:
174  CytronSmartDriveDuoDriver(address, port, 0x55) {}
175 };
176 
178 {
179 public:
181  CytronSmartDriveDuoDriver(address, port, 0x80) {}
182 };
183 
185 {
186 public:
188  CytronSmartDriveDuoDriver(address, port, 0x55) {}
189 };
CytronSmartDriveDuoDriver::setDeadband
void setDeadband(byte value)
Definition: CytronSmartDriveDuoDriver.h:153
CytronSmartDriveDuoDriver::drive
void drive(int power)
Definition: CytronSmartDriveDuoDriver.h:85
CytronSmartDriveDuoDriver::turn
void turn(int power)
Definition: CytronSmartDriveDuoDriver.h:76
CytronSmartDriveDuoMDDS10Driver::CytronSmartDriveDuoMDDS10Driver
CytronSmartDriveDuoMDDS10Driver(byte address, Stream &port)
Definition: CytronSmartDriveDuoDriver.h:173
CytronSmartDriveDuoDriver::stop
void stop()
Definition: CytronSmartDriveDuoDriver.h:147
CytronSmartDriveDuoDriver::setTimeout
void setTimeout(int milliseconds)
Definition: CytronSmartDriveDuoDriver.h:158
CytronSmartDriveDuoMDDS30Driver
Definition: CytronSmartDriveDuoDriver.h:177
CytronSmartDriveDuoDriver::setAddress
void setAddress(byte addr)
Definition: CytronSmartDriveDuoDriver.h:46
CytronSmartDriveDuoMDDS60Driver::CytronSmartDriveDuoMDDS60Driver
CytronSmartDriveDuoMDDS60Driver(byte address, Stream &port)
Definition: CytronSmartDriveDuoDriver.h:187
CytronSmartDriveDuoMDDS10Driver
Definition: CytronSmartDriveDuoDriver.h:170
CytronSmartDriveDuoDriver::CytronSmartDriveDuoDriver
CytronSmartDriveDuoDriver(byte address, Stream &port, uint8_t initialByte=0x80)
Definition: CytronSmartDriveDuoDriver.h:26
CytronSmartDriveDuoDriver::autobaud
void autobaud(boolean dontWait=false) const
Definition: CytronSmartDriveDuoDriver.h:55
CytronSmartDriveDuoDriver::address
byte address() const
Definition: CytronSmartDriveDuoDriver.h:37
CytronSmartDriveDuoDriver
Definition: CytronSmartDriveDuoDriver.h:17
CytronSmartDriveDuoDriver::motor
void motor(int power)
Definition: CytronSmartDriveDuoDriver.h:101
CytronSmartDriveDuoDriver::motor
void motor(int powerLeft, int powerRight)
Definition: CytronSmartDriveDuoDriver.h:119
CytronSmartDriveDuoMDDS60Driver
Definition: CytronSmartDriveDuoDriver.h:184
CytronSmartDriveDuoMDDS30Driver::CytronSmartDriveDuoMDDS30Driver
CytronSmartDriveDuoMDDS30Driver(byte address, Stream &port)
Definition: CytronSmartDriveDuoDriver.h:180