File: motorshieldv2.h

package info (click to toggle)
octave-arduino 0.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,616 kB
  • sloc: cpp: 3,221; python: 438; makefile: 152; xml: 22; sh: 1
file content (303 lines) | stat: -rw-r--r-- 7,455 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * Copyright (C) 2018 John Donoghue <john.donoghue@ieee.org>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "LibraryBase.h"
#define MTRV2_INIT        0x00
#define MTRV2_FREE        0x01

#define MTRV2_INIT_STEPPER 0x10
#define MTRV2_FREE_STEPPER 0x11
#define MTRV2_MOVE_STEPPER 0x12
#define MTRV2_RELEASE_STEPPER 0x13

#define MTRV2_INIT_DCMOTOR 0x20
#define MTRV2_FREE_DCMOTOR 0x21
#define MTRV2_START_DCMOTOR 0x22
#define MTRV2_STOP_DCMOTOR 0x23

// must add the adatfruit morotrshieldv2 library to arduino for this to compile
#include <Adafruit_MotorShield.h>

// Create the motor shield object with the default I2C address
//Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);

// Select which 'port' M1, M2, M3 or M4. In this case, M1
//Adafruit_DCMotor *myMotor = AFMS.getMotor(0);

class MotorShieldV2Addon : public LibraryBase
{
  Adafruit_MotorShield *AFMS;
  Adafruit_StepperMotor *stepperMotor[2];
  Adafruit_DCMotor *dcMotor[4];
public:
  MotorShieldV2Addon(OctaveArduinoClass& a)
  {
    libName = "adafruit/MotorShieldV2";
    a.registerLibrary(this);
  }
  void init() {
    byte i;

    AFMS = 0;
    for (i=0;i<2;i++) {
      stepperMotor[i] = 0;
    }
    for (i=0;i<4;i++) {
      dcMotor[i] = 0;
    }
    // AFMS.getStepper(200, 2);
    //  myMotor->setSpeed(10);  // 10 rpm
    //   myMotor->step(100, FORWARD, SINGLE);
    //
    //   Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
    //   // turn on motor M1
    //myMotor->setSpeed(200);
    //myMotor->run(RELEASE);
    //
    //   servo1.attach(10);
  }
  void commandHandler(uint8_t cmdId, uint8_t* data, uint8_t datasz)
  {
    switch(cmdId)
    {
      case MTRV2_INIT:
      {
	// send in the address
	// (currently) we only support a single control board
	if(datasz != 3) {
	   sendInvalidNumArgsMsg();
	}
	else {
          // TODO: pwm speed as well
          AFMS = new Adafruit_MotorShield(data[0]); 

          uint16_t freq = data[1];
	  freq = (freq<<8)|data[2];

	  AFMS->begin(freq); // input freq
          sendResponseMsg(cmdId, data, 1);
	}
        break;
      }
      case MTRV2_FREE:
      {
        if(AFMS) {
          //AFMS->release();
	  delete AFMS;
	  AFMS = 0;
	}
        sendResponseMsg(cmdId, data, 1);
        break;
      }
      case MTRV2_INIT_DCMOTOR:
      { 
	// 0 = shieldid (spiaddress)
	// 1 = motor num
	if(datasz != 2) {
	   sendInvalidNumArgsMsg();
	} else if(data[1] > 3) {
           // TODO invalid value
	   sendInvalidNumArgsMsg();
	} else if(AFMS) {
          dcMotor[data[1]] = AFMS->getMotor(data[1]+1);
	  if(dcMotor[data[1]]) {
	  dcMotor[data[1]]->setSpeed(0);
	  dcMotor[data[1]]->run(FORWARD);
	  dcMotor[data[1]]->run(RELEASE);
          sendResponseMsg(cmdId, data, 2);
	  } else {
	    sendInvalidNumArgsMsg();
	  }
	} else {
	   sendInvalidNumArgsMsg();
	}
	break;
      }
      case MTRV2_FREE_DCMOTOR:
      { 
	// 0 = shieldid (spiaddress)
	// 1 = motor num
	if(datasz != 2) {
	   sendInvalidNumArgsMsg();
	} else if(data[1] > 3) {
           // TODO invalid value
	   sendInvalidNumArgsMsg();
	} else {
          if(dcMotor[data[1]]) {
	    //dcMotor[data[1]]->run(RELEASE);
	    dcMotor[data[1]]->setSpeed(0);
	    dcMotor[data[1]]->run(RELEASE);
            dcMotor[data[1]] = 0;
	  }
          sendResponseMsg(cmdId, data, 2);
	}
	break;
      }
      case MTRV2_STOP_DCMOTOR:
      { 
	// 0 = shieldid (spiaddress)
	// 1 = motor num
	if(datasz != 2) {
	   sendInvalidNumArgsMsg();
	} else if(data[1] > 3) {
           // TODO invalid value
	   sendInvalidNumArgsMsg();
	} else {
          if(dcMotor[data[1]]) {
	    dcMotor[data[1]]->setSpeed(0);
	    dcMotor[data[1]]->run(RELEASE);
	  }
          sendResponseMsg(cmdId, data, 2);
	}
	break;
      }
      case MTRV2_START_DCMOTOR:
      { 
	// 0 = shieldid (spiaddress)
	// 1 = motor num
	// 2 = dir 1=forwrf, else reverse
	// 3 = speed (0..255)
	if(datasz != 4) {
	   sendInvalidNumArgsMsg();
	} else if(data[1] > 3) {
           // TODO invalid value
	   sendInvalidNumArgsMsg();
	} else {
          if(dcMotor[data[1]]) {
	    dcMotor[data[1]]->setSpeed(data[3]);
	    dcMotor[data[1]]->run((data[2] == 1) ? FORWARD : BACKWARD);
            sendResponseMsg(cmdId, data, 2);
	  }
	  else {
	   sendInvalidNumArgsMsg();
	  }
	}
	break;
      }

      case MTRV2_INIT_STEPPER:
      { 
	// 0 = shieldid (spiaddress)
	// 1 = motor num
	// 2,3 = stepsprerev
	if(datasz != 4) {
	   sendInvalidNumArgsMsg();
	} else if(data[1] > 1) {
           // TODO invalid value
	   sendInvalidNumArgsMsg();
	} else if(AFMS) {
          uint16_t cnt = data[2];
	  cnt = (cnt<<8)|data[3];

          stepperMotor[data[1]] = AFMS->getStepper(cnt, data[1]+1);
	  if(stepperMotor[data[1]]) {
            sendResponseMsg(cmdId, data, 2);
          } else {
	    sendInvalidNumArgsMsg();
          }
        } else {
	   sendInvalidNumArgsMsg();
	}
	break;
      }
      case MTRV2_FREE_STEPPER:
      { 
	// 0 = shieldid (spiaddress)
	// 1 = motor num
	if(datasz != 2) {
	   sendInvalidNumArgsMsg();
	} else if(data[1] > 1) {
           // TODO invalid value
	   sendInvalidNumArgsMsg();
	} else {
          if(stepperMotor[data[1]]) {
	    //dcMotor[data[1]]->run(RELEASE);
	    stepperMotor[data[1]]->release();
            stepperMotor[data[1]] = 0;
	  }
          sendResponseMsg(cmdId, data, 2);
	}
	break;
      }
      case MTRV2_RELEASE_STEPPER:
      { 
	// 0 = shieldid (spiaddress)
	// 1 = motor num
	if(datasz != 2) {
	   sendInvalidNumArgsMsg();
	} else if(data[1] > 1) {
           // TODO invalid value
	   sendInvalidNumArgsMsg();
	} else {
          if(stepperMotor[data[1]]) {
	    stepperMotor[data[1]]->release();
	  }
          sendResponseMsg(cmdId, data, 2);
	}
	break;
      }
      case MTRV2_MOVE_STEPPER:
      { 
	// 0 = shieldid (spiaddress)
	// 1 = motor num
	// 2 = dir 1=forward, else reverse
	// 3,4 uint16 rpm
	// 5,6 steps
	// 7 steptype
	if(datasz != 8) {
	   sendInvalidNumArgsMsg();
	} else if(data[1] > 1) {
           // TODO invalid value
	   sendInvalidNumArgsMsg();
	} else {
          if(stepperMotor[data[1]]) {
	    uint16_t rpm = data[3];
	    rpm = rpm<<8 | data[4];

	    uint16_t steps = data[5];
	    steps = steps<<8 | data[6];

	    uint8_t style = data[7];
	    style = SINGLE;
	    if(style == 1) style = DOUBLE;
	    if(style == 2) style = INTERLEAVE;
	    if(style == 3) style = MICROSTEP;

	    stepperMotor[data[1]]->setSpeed(rpm);
	    sendWaitMsg();
	    stepperMotor[data[1]]->step(steps, (data[2] == 1) ? FORWARD : BACKWARD, style);
            sendResponseMsg(cmdId, data, 2);
	  }
	  else {
	   sendInvalidNumArgsMsg();
	  }
	}
	break;
      }

      default:
      {
        // notify of invalid cmd
        sendUnknownCmdIDMsg();
      }
      break;
    }
  }
};