motorshield library should be a submodule, enhanced the motorshield plugin... (#162)

* added missing sensor types: LONG & WIND

* did a bit of code cleanup, like you suggested... removed a lot of code, and works as before! :)

* fixed typo

* added date to the time displays... changed password fields to type=password

* motorshield library should be a submodule, enhanced the motorshield plugin, with better command parsing, but still work to do..

* fully implemented steppers and DCMotors
This commit is contained in:
krikk
2017-03-16 21:58:03 +01:00
committed by DatuX
parent 9402fb73a9
commit 45ad68cd61
19 changed files with 247 additions and 1445 deletions
+3
View File
@@ -7,3 +7,6 @@
[submodule "lib/IRremoteESP8266"]
path = lib/IRremoteESP8266
url = https://github.com/sebastienwarin/IRremoteESP8266.git
[submodule "lib/Adafruit_Motor_Shield_V2"]
path = lib/Adafruit_Motor_Shield_V2
url = https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library.git
@@ -1,47 +0,0 @@
{
"description": "Library for the Adafruit Motor Shield V2 for Arduino. It supports DC motors & stepper motors with microstepping as well as stacking-support.",
"repository": {
"url": "https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library",
"type": "git"
},
"platforms": [
"atmelavr",
"atmelsam",
"espressif8266",
"intel_arc32",
"microchippic32",
"nordicnrf51",
"teensy",
"timsp430"
],
"export": {
"exclude": [
"extras",
"docs",
"tests",
"test",
"*.doxyfile",
"*.pdf"
],
"include": null
},
"authors": [
{
"maintainer": true,
"name": "Adafruit",
"url": null,
"email": "info@adafruit.com"
}
],
"keywords": [
"device",
"control"
],
"id": 783,
"name": "Adafruit Motor Shield V2 Library",
"frameworks": [
"arduino"
],
"version": "1.0.4",
"homepage": null
}
@@ -1,397 +0,0 @@
/******************************************************************
This is the library for the Adafruit Motor Shield V2 for Arduino.
It supports DC motors & Stepper motors with microstepping as well
as stacking-support. It is *not* compatible with the V1 library!
It will only work with https://www.adafruit.com/products/1483
Adafruit invests time and resources providing this open
source code, please support Adafruit and open-source hardware
by purchasing products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, check license.txt for more information.
All text above must be included in any redistribution.
******************************************************************/
#if (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include "Adafruit_MotorShield.h"
#include <Adafruit_MS_PWMServoDriver.h>
#if defined(ARDUINO_SAM_DUE)
#define WIRE Wire1
#else
#define WIRE Wire
#endif
#if (MICROSTEPS == 8)
uint8_t microstepcurve[] = {0, 50, 98, 142, 180, 212, 236, 250, 255};
#elif (MICROSTEPS == 16)
uint8_t microstepcurve[] = {0, 25, 50, 74, 98, 120, 141, 162, 180, 197, 212, 225, 236, 244, 250, 253, 255};
#endif
Adafruit_MotorShield::Adafruit_MotorShield(uint8_t addr) {
_addr = addr;
_pwm = Adafruit_MS_PWMServoDriver(_addr);
}
void Adafruit_MotorShield::begin(uint16_t freq) {
// init PWM w/_freq
WIRE.begin();
_pwm.begin();
_freq = freq;
_pwm.setPWMFreq(_freq); // This is the maximum PWM frequency
for (uint8_t i=0; i<16; i++)
_pwm.setPWM(i, 0, 0);
}
void Adafruit_MotorShield::setPWM(uint8_t pin, uint16_t value) {
if (value > 4095) {
_pwm.setPWM(pin, 4096, 0);
} else
_pwm.setPWM(pin, 0, value);
}
void Adafruit_MotorShield::setPin(uint8_t pin, boolean value) {
if (value == LOW)
_pwm.setPWM(pin, 0, 0);
else
_pwm.setPWM(pin, 4096, 0);
}
Adafruit_DCMotor *Adafruit_MotorShield::getMotor(uint8_t num) {
if (num > 4) return NULL;
num--;
if (dcmotors[num].motornum == 0) {
// not init'd yet!
dcmotors[num].motornum = num;
dcmotors[num].MC = this;
uint8_t pwm, in1, in2;
if (num == 0) {
pwm = 8; in2 = 9; in1 = 10;
} else if (num == 1) {
pwm = 13; in2 = 12; in1 = 11;
} else if (num == 2) {
pwm = 2; in2 = 3; in1 = 4;
} else if (num == 3) {
pwm = 7; in2 = 6; in1 = 5;
}
dcmotors[num].PWMpin = pwm;
dcmotors[num].IN1pin = in1;
dcmotors[num].IN2pin = in2;
}
return &dcmotors[num];
}
Adafruit_StepperMotor *Adafruit_MotorShield::getStepper(uint16_t steps, uint8_t num) {
if (num > 2) return NULL;
num--;
if (steppers[num].steppernum == 0) {
// not init'd yet!
steppers[num].steppernum = num;
steppers[num].revsteps = steps;
steppers[num].MC = this;
uint8_t pwma, pwmb, ain1, ain2, bin1, bin2;
if (num == 0) {
pwma = 8; ain2 = 9; ain1 = 10;
pwmb = 13; bin2 = 12; bin1 = 11;
} else if (num == 1) {
pwma = 2; ain2 = 3; ain1 = 4;
pwmb = 7; bin2 = 6; bin1 = 5;
}
steppers[num].PWMApin = pwma;
steppers[num].PWMBpin = pwmb;
steppers[num].AIN1pin = ain1;
steppers[num].AIN2pin = ain2;
steppers[num].BIN1pin = bin1;
steppers[num].BIN2pin = bin2;
}
return &steppers[num];
}
/******************************************
MOTORS
******************************************/
Adafruit_DCMotor::Adafruit_DCMotor(void) {
MC = NULL;
motornum = 0;
PWMpin = IN1pin = IN2pin = 0;
}
void Adafruit_DCMotor::run(uint8_t cmd) {
switch (cmd) {
case FORWARD:
MC->setPin(IN2pin, LOW); // take low first to avoid 'break'
MC->setPin(IN1pin, HIGH);
break;
case BACKWARD:
MC->setPin(IN1pin, LOW); // take low first to avoid 'break'
MC->setPin(IN2pin, HIGH);
break;
case RELEASE:
MC->setPin(IN1pin, LOW);
MC->setPin(IN2pin, LOW);
break;
}
}
void Adafruit_DCMotor::setSpeed(uint8_t speed) {
MC->setPWM(PWMpin, speed*16);
}
/******************************************
STEPPERS
******************************************/
Adafruit_StepperMotor::Adafruit_StepperMotor(void) {
revsteps = steppernum = currentstep = 0;
}
/*
uint16_t steps, Adafruit_MotorShield controller) {
revsteps = steps;
steppernum = 1;
currentstep = 0;
if (steppernum == 1) {
latch_state &= ~_BV(MOTOR1_A) & ~_BV(MOTOR1_B) &
~_BV(MOTOR2_A) & ~_BV(MOTOR2_B); // all motor pins to 0
// enable both H bridges
pinMode(11, OUTPUT);
pinMode(3, OUTPUT);
digitalWrite(11, HIGH);
digitalWrite(3, HIGH);
// use PWM for microstepping support
MC->setPWM(1, 255);
MC->setPWM(2, 255);
} else if (steppernum == 2) {
latch_state &= ~_BV(MOTOR3_A) & ~_BV(MOTOR3_B) &
~_BV(MOTOR4_A) & ~_BV(MOTOR4_B); // all motor pins to 0
// enable both H bridges
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
// use PWM for microstepping support
// use PWM for microstepping support
MC->setPWM(3, 255);
MC->setPWM(4, 255);
}
}
*/
void Adafruit_StepperMotor::setSpeed(uint16_t rpm) {
//Serial.println("steps per rev: "); Serial.println(revsteps);
//Serial.println("RPM: "); Serial.println(rpm);
usperstep = 60000000 / ((uint32_t)revsteps * (uint32_t)rpm);
}
void Adafruit_StepperMotor::release(void) {
MC->setPin(AIN1pin, LOW);
MC->setPin(AIN2pin, LOW);
MC->setPin(BIN1pin, LOW);
MC->setPin(BIN2pin, LOW);
MC->setPWM(PWMApin, 0);
MC->setPWM(PWMBpin, 0);
}
void Adafruit_StepperMotor::step(uint16_t steps, uint8_t dir, uint8_t style) {
uint32_t uspers = usperstep;
uint8_t ret = 0;
if (style == INTERLEAVE) {
uspers /= 2;
}
else if (style == MICROSTEP) {
uspers /= MICROSTEPS;
steps *= MICROSTEPS;
#ifdef MOTORDEBUG
Serial.print("steps = "); Serial.println(steps, DEC);
#endif
}
while (steps--) {
//Serial.println("step!"); Serial.println(uspers);
ret = onestep(dir, style);
delayMicroseconds(uspers);
}
}
uint8_t Adafruit_StepperMotor::onestep(uint8_t dir, uint8_t style) {
uint8_t a, b, c, d;
uint8_t ocrb, ocra;
ocra = ocrb = 255;
// next determine what sort of stepping procedure we're up to
if (style == SINGLE) {
if ((currentstep/(MICROSTEPS/2)) % 2) { // we're at an odd step, weird
if (dir == FORWARD) {
currentstep += MICROSTEPS/2;
}
else {
currentstep -= MICROSTEPS/2;
}
} else { // go to the next even step
if (dir == FORWARD) {
currentstep += MICROSTEPS;
}
else {
currentstep -= MICROSTEPS;
}
}
} else if (style == DOUBLE) {
if (! (currentstep/(MICROSTEPS/2) % 2)) { // we're at an even step, weird
if (dir == FORWARD) {
currentstep += MICROSTEPS/2;
} else {
currentstep -= MICROSTEPS/2;
}
} else { // go to the next odd step
if (dir == FORWARD) {
currentstep += MICROSTEPS;
} else {
currentstep -= MICROSTEPS;
}
}
} else if (style == INTERLEAVE) {
if (dir == FORWARD) {
currentstep += MICROSTEPS/2;
} else {
currentstep -= MICROSTEPS/2;
}
}
if (style == MICROSTEP) {
if (dir == FORWARD) {
currentstep++;
} else {
// BACKWARDS
currentstep--;
}
currentstep += MICROSTEPS*4;
currentstep %= MICROSTEPS*4;
ocra = ocrb = 0;
if ( (currentstep >= 0) && (currentstep < MICROSTEPS)) {
ocra = microstepcurve[MICROSTEPS - currentstep];
ocrb = microstepcurve[currentstep];
} else if ( (currentstep >= MICROSTEPS) && (currentstep < MICROSTEPS*2)) {
ocra = microstepcurve[currentstep - MICROSTEPS];
ocrb = microstepcurve[MICROSTEPS*2 - currentstep];
} else if ( (currentstep >= MICROSTEPS*2) && (currentstep < MICROSTEPS*3)) {
ocra = microstepcurve[MICROSTEPS*3 - currentstep];
ocrb = microstepcurve[currentstep - MICROSTEPS*2];
} else if ( (currentstep >= MICROSTEPS*3) && (currentstep < MICROSTEPS*4)) {
ocra = microstepcurve[currentstep - MICROSTEPS*3];
ocrb = microstepcurve[MICROSTEPS*4 - currentstep];
}
}
currentstep += MICROSTEPS*4;
currentstep %= MICROSTEPS*4;
#ifdef MOTORDEBUG
Serial.print("current step: "); Serial.println(currentstep, DEC);
Serial.print(" pwmA = "); Serial.print(ocra, DEC);
Serial.print(" pwmB = "); Serial.println(ocrb, DEC);
#endif
MC->setPWM(PWMApin, ocra*16);
MC->setPWM(PWMBpin, ocrb*16);
// release all
uint8_t latch_state = 0; // all motor pins to 0
//Serial.println(step, DEC);
if (style == MICROSTEP) {
if ((currentstep >= 0) && (currentstep < MICROSTEPS))
latch_state |= 0x03;
if ((currentstep >= MICROSTEPS) && (currentstep < MICROSTEPS*2))
latch_state |= 0x06;
if ((currentstep >= MICROSTEPS*2) && (currentstep < MICROSTEPS*3))
latch_state |= 0x0C;
if ((currentstep >= MICROSTEPS*3) && (currentstep < MICROSTEPS*4))
latch_state |= 0x09;
} else {
switch (currentstep/(MICROSTEPS/2)) {
case 0:
latch_state |= 0x1; // energize coil 1 only
break;
case 1:
latch_state |= 0x3; // energize coil 1+2
break;
case 2:
latch_state |= 0x2; // energize coil 2 only
break;
case 3:
latch_state |= 0x6; // energize coil 2+3
break;
case 4:
latch_state |= 0x4; // energize coil 3 only
break;
case 5:
latch_state |= 0xC; // energize coil 3+4
break;
case 6:
latch_state |= 0x8; // energize coil 4 only
break;
case 7:
latch_state |= 0x9; // energize coil 1+4
break;
}
}
#ifdef MOTORDEBUG
Serial.print("Latch: 0x"); Serial.println(latch_state, HEX);
#endif
if (latch_state & 0x1) {
// Serial.println(AIN2pin);
MC->setPin(AIN2pin, HIGH);
} else {
MC->setPin(AIN2pin, LOW);
}
if (latch_state & 0x2) {
MC->setPin(BIN1pin, HIGH);
// Serial.println(BIN1pin);
} else {
MC->setPin(BIN1pin, LOW);
}
if (latch_state & 0x4) {
MC->setPin(AIN1pin, HIGH);
// Serial.println(AIN1pin);
} else {
MC->setPin(AIN1pin, LOW);
}
if (latch_state & 0x8) {
MC->setPin(BIN2pin, HIGH);
// Serial.println(BIN2pin);
} else {
MC->setPin(BIN2pin, LOW);
}
return currentstep;
}
@@ -1,102 +0,0 @@
/******************************************************************
This is the library for the Adafruit Motor Shield V2 for Arduino.
It supports DC motors & Stepper motors with microstepping as well
as stacking-support. It is *not* compatible with the V1 library!
It will only work with https://www.adafruit.com/products/1483
Adafruit invests time and resources providing this open
source code, please support Adafruit and open-source hardware
by purchasing products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, check license.txt for more information.
All text above must be included in any redistribution.
******************************************************************/
#ifndef _Adafruit_MotorShield_h_
#define _Adafruit_MotorShield_h_
#include <inttypes.h>
#include <Wire.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
//#define MOTORDEBUG
#define MICROSTEPS 16 // 8 or 16
#define MOTOR1_A 2
#define MOTOR1_B 3
#define MOTOR2_A 1
#define MOTOR2_B 4
#define MOTOR4_A 0
#define MOTOR4_B 6
#define MOTOR3_A 5
#define MOTOR3_B 7
#define FORWARD 1
#define BACKWARD 2
#define BRAKE 3
#define RELEASE 4
#define SINGLE 1
#define DOUBLE 2
#define INTERLEAVE 3
#define MICROSTEP 4
class Adafruit_MotorShield;
class Adafruit_DCMotor
{
public:
Adafruit_DCMotor(void);
friend class Adafruit_MotorShield;
void run(uint8_t);
void setSpeed(uint8_t);
private:
uint8_t PWMpin, IN1pin, IN2pin;
Adafruit_MotorShield *MC;
uint8_t motornum;
};
class Adafruit_StepperMotor {
public:
Adafruit_StepperMotor(void);
friend class Adafruit_MotorShield;
void step(uint16_t steps, uint8_t dir, uint8_t style = SINGLE);
void setSpeed(uint16_t);
uint8_t onestep(uint8_t dir, uint8_t style);
void release(void);
uint32_t usperstep;
private:
uint8_t PWMApin, AIN1pin, AIN2pin;
uint8_t PWMBpin, BIN1pin, BIN2pin;
uint16_t revsteps; // # steps per revolution
uint8_t currentstep;
Adafruit_MotorShield *MC;
uint8_t steppernum;
};
class Adafruit_MotorShield
{
public:
Adafruit_MotorShield(uint8_t addr = 0x60);
friend class Adafruit_DCMotor;
void begin(uint16_t freq = 1600);
void setPWM(uint8_t pin, uint16_t val);
void setPin(uint8_t pin, boolean val);
Adafruit_DCMotor *getMotor(uint8_t n);
Adafruit_StepperMotor *getStepper(uint16_t steps, uint8_t n);
private:
uint8_t _addr;
uint16_t _freq;
Adafruit_DCMotor dcmotors[4];
Adafruit_StepperMotor steppers[2];
Adafruit_MS_PWMServoDriver _pwm;
};
#endif
-45
View File
@@ -1,45 +0,0 @@
# Adafruit Motor Shield v2 Library
This is the library for the Adafruit Motor Shield V2 for Arduino.
It supports DC motors & Stepper motors with microstepping as well
as stacking-support. It is *not* compatible with the V1 library!
It will only work with https://www.adafruit.com/products/1438
<!-- START COMPATIBILITY TABLE -->
## Compatibility
MCU | Tested Works | Doesn't Work | Not Tested | Notes
----------------- | :----------: | :----------: | :---------: | -----
Atmega328 @ 16MHz | X | | |
Atmega328 @ 12MHz | X | | |
Atmega32u4 @ 16MHz | X | | |
Atmega32u4 @ 8MHz | X | | |
ESP8266 | | | X |
Atmega2560 @ 16MHz | X | | |
ATSAM3X8E | | X | |
ATSAM21D | | X | |
ATtiny85 @ 16MHz | | | X |
ATtiny85 @ 8MHz | | | X |
* ATmega328 @ 16MHz : Arduino UNO, Adafruit Pro Trinket 5V, Adafruit Metro 328, Adafruit Metro Mini
* ATmega328 @ 12MHz : Adafruit Pro Trinket 3V
* ATmega32u4 @ 16MHz : Arduino Leonardo, Arduino Micro, Arduino Yun, Teensy 2.0
* ATmega32u4 @ 8MHz : Adafruit Flora, Bluefruit Micro
* ESP8266 : Adafruit Huzzah
* ATmega2560 @ 16MHz : Arduino Mega
* ATSAM3X8E : Arduino Due
* ATSAM21D : Arduino Zero, M0 Pro
* ATtiny85 @ 16MHz : Adafruit Trinket 5V
* ATtiny85 @ 8MHz : Adafruit Gemma, Arduino Gemma, Adafruit Trinket 3V
<!-- END COMPATIBILITY TABLE -->
Adafruit invests time and resources providing this open
source code, please support Adafruit and open-source hardware
by purchasing products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, check license.txt for more information.
All text above must be included in any redistribution.
@@ -1,52 +0,0 @@
// ConstantSpeed.pde
// -*- mode: C++ -*-
//
// Shows how to run AccelStepper in the simplest,
// fixed speed mode with no accelerations
// Requires the Adafruit_Motorshield v2 library
// https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library
// And AccelStepper with AFMotor support
// https://github.com/adafruit/AccelStepper
// This tutorial is for Adafruit Motorshield v2 only!
// Will not work with v1 shields
#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.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);
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myStepper1 = AFMS.getStepper(200, 2);
// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
void forwardstep1() {
myStepper1->onestep(FORWARD, SINGLE);
}
void backwardstep1() {
myStepper1->onestep(BACKWARD, SINGLE);
}
AccelStepper Astepper1(forwardstep1, backwardstep1); // use functions to step
void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Stepper test!");
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
Astepper1.setSpeed(50);
}
void loop()
{
Astepper1.runSpeed();
}
@@ -1,90 +0,0 @@
// Shows how to run three Steppers at once with varying speeds
//
// Requires the Adafruit_Motorshield v2 library
// https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library
// And AccelStepper with AFMotor support
// https://github.com/adafruit/AccelStepper
// This tutorial is for Adafruit Motorshield v2 only!
// Will not work with v1 shields
#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
Adafruit_MotorShield AFMSbot(0x61); // Rightmost jumper closed
Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers
// Connect two steppers with 200 steps per revolution (1.8 degree)
// to the top shield
Adafruit_StepperMotor *myStepper1 = AFMStop.getStepper(200, 1);
Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(200, 2);
// Connect one stepper with 200 steps per revolution (1.8 degree)
// to the bottom shield
Adafruit_StepperMotor *myStepper3 = AFMSbot.getStepper(200, 2);
// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
// wrappers for the first motor!
void forwardstep1() {
myStepper1->onestep(FORWARD, SINGLE);
}
void backwardstep1() {
myStepper1->onestep(BACKWARD, SINGLE);
}
// wrappers for the second motor!
void forwardstep2() {
myStepper2->onestep(FORWARD, DOUBLE);
}
void backwardstep2() {
myStepper2->onestep(BACKWARD, DOUBLE);
}
// wrappers for the third motor!
void forwardstep3() {
myStepper3->onestep(FORWARD, INTERLEAVE);
}
void backwardstep3() {
myStepper3->onestep(BACKWARD, INTERLEAVE);
}
// Now we'll wrap the 3 steppers in an AccelStepper object
AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);
AccelStepper stepper3(forwardstep3, backwardstep3);
void setup()
{
AFMSbot.begin(); // Start the bottom shield
AFMStop.begin(); // Start the top shield
stepper1.setMaxSpeed(100.0);
stepper1.setAcceleration(100.0);
stepper1.moveTo(24);
stepper2.setMaxSpeed(200.0);
stepper2.setAcceleration(100.0);
stepper2.moveTo(50000);
stepper3.setMaxSpeed(300.0);
stepper3.setAcceleration(100.0);
stepper3.moveTo(1000000);
}
void loop()
{
// Change direction at the limits
if (stepper1.distanceToGo() == 0)
stepper1.moveTo(-stepper1.currentPosition());
if (stepper2.distanceToGo() == 0)
stepper2.moveTo(-stepper2.currentPosition());
if (stepper3.distanceToGo() == 0)
stepper3.moveTo(-stepper3.currentPosition());
stepper1.run();
stepper2.run();
stepper3.run();
}
@@ -1,68 +0,0 @@
/*
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control
For use with the Adafruit Motor Shield v2
----> http://www.adafruit.com/products/1438
*/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.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(1);
// You can also make another motor on port M2
//Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Adafruit Motorshield v2 - DC Motor test!");
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
// Set the speed to start, from 0 (off) to 255 (max speed)
myMotor->setSpeed(150);
myMotor->run(FORWARD);
// turn on motor
myMotor->run(RELEASE);
}
void loop() {
uint8_t i;
Serial.print("tick");
myMotor->run(FORWARD);
for (i=0; i<255; i++) {
myMotor->setSpeed(i);
delay(10);
}
for (i=255; i!=0; i--) {
myMotor->setSpeed(i);
delay(10);
}
Serial.print("tock");
myMotor->run(BACKWARD);
for (i=0; i<255; i++) {
myMotor->setSpeed(i);
delay(10);
}
for (i=255; i!=0; i--) {
myMotor->setSpeed(i);
delay(10);
}
Serial.print("tech");
myMotor->run(RELEASE);
delay(1000);
}
@@ -1,84 +0,0 @@
/*
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control
For use with the Adafruit Motor Shield v2
----> http://www.adafruit.com/products/1438
This sketch creates a fun motor party on your desk *whiirrr*
Connect a unipolar/bipolar stepper to M3/M4
Connect a DC motor to M1
Connect a hobby servo to SERVO1
*/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#include <Servo.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);
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myStepper = AFMS.getStepper(200, 2);
// And connect a DC motor to port M1
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
// We'll also test out the built in Arduino Servo library
Servo servo1;
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("MMMMotor party!");
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
// Attach a servo to pin #10
servo1.attach(10);
// turn on motor M1
myMotor->setSpeed(200);
myMotor->run(RELEASE);
// setup the stepper
myStepper->setSpeed(10); // 10 rpm
}
int i;
void loop() {
myMotor->run(FORWARD);
for (i=0; i<255; i++) {
servo1.write(map(i, 0, 255, 0, 180));
myMotor->setSpeed(i);
myStepper->step(1, FORWARD, INTERLEAVE);
delay(3);
}
for (i=255; i!=0; i--) {
servo1.write(map(i, 0, 255, 0, 180));
myMotor->setSpeed(i);
myStepper->step(1, BACKWARD, INTERLEAVE);
delay(3);
}
myMotor->run(BACKWARD);
for (i=0; i<255; i++) {
servo1.write(map(i, 0, 255, 0, 180));
myMotor->setSpeed(i);
myStepper->step(1, FORWARD, DOUBLE);
delay(3);
}
for (i=255; i!=0; i--) {
servo1.write(map(i, 0, 255, 0, 180));
myMotor->setSpeed(i);
myStepper->step(1, BACKWARD, DOUBLE);
delay(3);
}
}
@@ -1,76 +0,0 @@
/*
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control
For use with the Adafruit Motor Shield v2
----> http://www.adafruit.com/products/1438
*/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
Adafruit_MotorShield AFMSbot(0x61); // Rightmost jumper closed
Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers
// On the top shield, connect two steppers, each with 200 steps
Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(200, 1);
Adafruit_StepperMotor *myStepper3 = AFMStop.getStepper(200, 2);
// On the bottom shield connect a stepper to port M3/M4 with 200 steps
Adafruit_StepperMotor *myStepper1 = AFMSbot.getStepper(200, 2);
// And a DC Motor to port M1
Adafruit_DCMotor *myMotor1 = AFMSbot.getMotor(1);
void setup() {
while (!Serial);
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("MMMMotor party!");
AFMSbot.begin(); // Start the bottom shield
AFMStop.begin(); // Start the top shield
// turn on the DC motor
myMotor1->setSpeed(200);
myMotor1->run(RELEASE);
}
int i;
void loop() {
myMotor1->run(FORWARD);
for (i=0; i<255; i++) {
myMotor1->setSpeed(i);
myStepper1->onestep(FORWARD, INTERLEAVE);
myStepper2->onestep(BACKWARD, DOUBLE);
myStepper3->onestep(FORWARD, MICROSTEP);
delay(3);
}
for (i=255; i!=0; i--) {
myMotor1->setSpeed(i);
myStepper1->onestep(BACKWARD, INTERLEAVE);
myStepper2->onestep(FORWARD, DOUBLE);
myStepper3->onestep(BACKWARD, MICROSTEP);
delay(3);
}
myMotor1->run(BACKWARD);
for (i=0; i<255; i++) {
myMotor1->setSpeed(i);
myStepper1->onestep(FORWARD, DOUBLE);
myStepper2->onestep(BACKWARD, INTERLEAVE);
myStepper3->onestep(FORWARD, MICROSTEP);
delay(3);
}
for (i=255; i!=0; i--) {
myMotor1->setSpeed(i);
myStepper1->onestep(BACKWARD, DOUBLE);
myStepper2->onestep(FORWARD, INTERLEAVE);
myStepper3->onestep(BACKWARD, MICROSTEP);
delay(3);
}
}
@@ -1,51 +0,0 @@
/*
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control
For use with the Adafruit Motor Shield v2
----> http://www.adafruit.com/products/1438
*/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.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);
// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Stepper test!");
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
myMotor->setSpeed(10); // 10 rpm
}
void loop() {
Serial.println("Single coil steps");
myMotor->step(100, FORWARD, SINGLE);
myMotor->step(100, BACKWARD, SINGLE);
Serial.println("Double coil steps");
myMotor->step(100, FORWARD, DOUBLE);
myMotor->step(100, BACKWARD, DOUBLE);
Serial.println("Interleave coil steps");
myMotor->step(100, FORWARD, INTERLEAVE);
myMotor->step(100, BACKWARD, INTERLEAVE);
Serial.println("Microstep steps");
myMotor->step(50, FORWARD, MICROSTEP);
myMotor->step(50, BACKWARD, MICROSTEP);
}
-40
View File
@@ -1,40 +0,0 @@
#######################################
# Syntax Coloring Map for AFMotor
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
Adafruit_MotorShield KEYWORD1
Adafruit_DCMotor KEYWORD1
Adafruit_StepperMotor KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
enable KEYWORD2
run KEYWORD2
setSpeed KEYWORD2
step KEYWORD2
onestep KEYWORD2
release KEYWORD2
getMotor KEYWORD2
getStepper KEYWORD2
setPin KEYWORD2
setPWM KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################
MICROSTEPPING LITERAL1
FORWARD LITERAL1
BACKWARD LITERAL1
BRAKE LITERAL1
RELEASE LITERAL1
SINGLE LITERAL1
DOUBLE LITERAL1
INTERLEAVE LITERAL1
MICROSTEP LITERAL1
@@ -1,9 +0,0 @@
name=Adafruit Motor Shield V2 Library
version=1.0.4
author=Adafruit
maintainer=Adafruit <info@adafruit.com>
sentence=Library for the Adafruit Motor Shield V2 for Arduino. It supports DC motors & stepper motors with microstepping as well as stacking-support.
paragraph=Library for the Adafruit Motor Shield V2 for Arduino. It supports DC motors & stepper motors with microstepping as well as stacking-support.
category=Device Control
url=https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library
architectures=*
-25
View File
@@ -1,25 +0,0 @@
Software License Agreement (BSD License)
Copyright (c) 2012, Adafruit Industries. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -1,113 +0,0 @@
/***************************************************
This is a library for our Adafruit 16-channel PWM & Servo driver
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/products/815
These displays use I2C to communicate, 2 pins are required to
interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include <Adafruit_MS_PWMServoDriver.h>
#include <Wire.h>
#if defined(ARDUINO_SAM_DUE)
#define WIRE Wire1
#else
#define WIRE Wire
#endif
Adafruit_MS_PWMServoDriver::Adafruit_MS_PWMServoDriver(uint8_t addr) {
_i2caddr = addr;
}
void Adafruit_MS_PWMServoDriver::begin(void) {
WIRE.begin();
reset();
}
void Adafruit_MS_PWMServoDriver::reset(void) {
write8(PCA9685_MODE1, 0x0);
}
void Adafruit_MS_PWMServoDriver::setPWMFreq(float freq) {
//Serial.print("Attempting to set freq ");
//Serial.println(freq);
freq *= 0.9; // Correct for overshoot in the frequency setting (see issue #11).
float prescaleval = 25000000;
prescaleval /= 4096;
prescaleval /= freq;
prescaleval -= 1;
//Serial.print("Estimated pre-scale: "); Serial.println(prescaleval);
uint8_t prescale = floor(prescaleval + 0.5);
//Serial.print("Final pre-scale: "); Serial.println(prescale);
uint8_t oldmode = read8(PCA9685_MODE1);
uint8_t newmode = (oldmode&0x7F) | 0x10; // sleep
write8(PCA9685_MODE1, newmode); // go to sleep
write8(PCA9685_PRESCALE, prescale); // set the prescaler
write8(PCA9685_MODE1, oldmode);
delay(5);
write8(PCA9685_MODE1, oldmode | 0xa1); // This sets the MODE1 register to turn on auto increment.
// This is why the beginTransmission below was not working.
// Serial.print("Mode now 0x"); Serial.println(read8(PCA9685_MODE1), HEX);
}
void Adafruit_MS_PWMServoDriver::setPWM(uint8_t num, uint16_t on, uint16_t off) {
//Serial.print("Setting PWM "); Serial.print(num); Serial.print(": "); Serial.print(on); Serial.print("->"); Serial.println(off);
WIRE.beginTransmission(_i2caddr);
#if ARDUINO >= 100
WIRE.write(LED0_ON_L+4*num);
WIRE.write(on);
WIRE.write(on>>8);
WIRE.write(off);
WIRE.write(off>>8);
#else
WIRE.send(LED0_ON_L+4*num);
WIRE.send((uint8_t)on);
WIRE.send((uint8_t)(on>>8));
WIRE.send((uint8_t)off);
WIRE.send((uint8_t)(off>>8));
#endif
WIRE.endTransmission();
}
uint8_t Adafruit_MS_PWMServoDriver::read8(uint8_t addr) {
WIRE.beginTransmission(_i2caddr);
#if ARDUINO >= 100
WIRE.write(addr);
#else
WIRE.send(addr);
#endif
WIRE.endTransmission();
WIRE.requestFrom((uint8_t)_i2caddr, (uint8_t)1);
#if ARDUINO >= 100
return WIRE.read();
#else
return WIRE.receive();
#endif
}
void Adafruit_MS_PWMServoDriver::write8(uint8_t addr, uint8_t d) {
WIRE.beginTransmission(_i2caddr);
#if ARDUINO >= 100
WIRE.write(addr);
WIRE.write(d);
#else
WIRE.send(addr);
WIRE.send(d);
#endif
WIRE.endTransmission();
}
@@ -1,61 +0,0 @@
/***************************************************
This is a library for our Adafruit 16-channel PWM & Servo driver
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/products/815
These displays use I2C to communicate, 2 pins are required to
interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#ifndef _Adafruit_MS_PWMServoDriver_H
#define _Adafruit_MS_PWMServoDriver_H
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define PCA9685_SUBADR1 0x2
#define PCA9685_SUBADR2 0x3
#define PCA9685_SUBADR3 0x4
#define PCA9685_MODE1 0x0
#define PCA9685_PRESCALE 0xFE
#define LED0_ON_L 0x6
#define LED0_ON_H 0x7
#define LED0_OFF_L 0x8
#define LED0_OFF_H 0x9
#define ALLLED_ON_L 0xFA
#define ALLLED_ON_H 0xFB
#define ALLLED_OFF_L 0xFC
#define ALLLED_OFF_H 0xFD
class Adafruit_MS_PWMServoDriver {
public:
Adafruit_MS_PWMServoDriver(uint8_t addr = 0x40);
void begin(void);
void reset(void);
void setPWMFreq(float freq);
void setPWM(uint8_t num, uint16_t on, uint16_t off);
private:
uint8_t _i2caddr;
uint8_t read8(uint8_t addr);
void write8(uint8_t addr, uint8_t d);
};
#endif
@@ -1,26 +0,0 @@
Software License Agreement (BSD License)
Copyright (c) 2012, Adafruit Industries
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+243 -159
View File
@@ -5,7 +5,7 @@
// Adafruit Motorshield v2
// like this one: https://www.adafruit.com/products/1438
// based on this library: https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library
// Currently only DC Motors Part is implemented, Steppers and Servos are missing!!!
// Currently DC Motors and Steppers are implemented, Servos are missing!!!
#ifdef PLUGIN_BUILD_DEV
@@ -17,10 +17,9 @@
#define PLUGIN_VALUENAME1_048 "MotorShield v2"
uint8_t Plugin_048_MotorShield_address = 0x60;
byte Plugin_048_MotorType = 0;
byte Plugin_048_MotorNumber = 1;
byte Plugin_048_MotorSpeed = 255;
int Plugin_048_MotorStepsPerRevolution = 200;
int Plugin_048_StepperSpeed = 10;
boolean Plugin_048(byte function, struct EventStruct *event, String& string) {
boolean success = false;
@@ -31,181 +30,266 @@ boolean Plugin_048(byte function, struct EventStruct *event, String& string) {
switch (function) {
case PLUGIN_DEVICE_ADD: {
Device[++deviceCount].Number = PLUGIN_ID_048;
Device[deviceCount].Type = DEVICE_TYPE_I2C;
Device[deviceCount].VType = SENSOR_TYPE_SINGLE;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = false;
Device[deviceCount].ValueCount = 0;
Device[deviceCount].SendDataOption = false;
Device[deviceCount].TimerOption = true;
break;
}
case PLUGIN_GET_DEVICENAME: {
string = F(PLUGIN_NAME_048);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES: {
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0],
PSTR(PLUGIN_VALUENAME1_048));
break;
}
case PLUGIN_WEBFORM_LOAD: {
string +=
F("<TR><TD>I2C Address (Hex): <TD><input type='text' title='Set i2c Address of sensor' name='");
string += F("plugin_048_adr' value='0x");
string += String(Settings.TaskDevicePluginConfig[event->TaskIndex][0],HEX);
string += F("'>");
byte choice2 = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
String options2[3];
options2[0] = F("DCMotor");
options2[1] = F("Stepper [NOT IMPLEMETED]");
options2[2] = F("Servo [NOT IMPLEMETED]");
int optionValues2[3];
optionValues2[0] = 0;
optionValues2[1] = 1;
optionValues2[2] = 2;
string += F("<TR><TD>Motor Type:<TD><select name='plugin_048_motortype'>");
for (byte x = 0; x < 3; x++) {
string += F("<option value='");
string += optionValues2[x];
string += "'";
if (choice2 == optionValues2[x])
string += F(" selected");
string += ">";
string += options2[x];
string += F("</option>");
case PLUGIN_DEVICE_ADD: {
Device[++deviceCount].Number = PLUGIN_ID_048;
Device[deviceCount].Type = DEVICE_TYPE_I2C;
Device[deviceCount].VType = SENSOR_TYPE_SINGLE;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = false;
Device[deviceCount].ValueCount = 0;
Device[deviceCount].SendDataOption = false;
Device[deviceCount].TimerOption = false;
break;
}
string += F("</select>");
byte choice3 = Settings.TaskDevicePluginConfig[event->TaskIndex][3];
String options3[4];
options3[0] = F("1");
options3[1] = F("2");
options3[2] = F("3");
options3[3] = F("4");
int optionValues3[4];
optionValues3[0] = 1;
optionValues3[1] = 2;
optionValues3[2] = 3;
optionValues3[3] = 4;
string += F("<TR><TD>Motor Number:<TD><select name='plugin_048_motornumber'>");
for (byte x = 0; x < 4; x++) {
string += F("<option value='");
string += optionValues3[x];
string += "'";
if (choice3 == optionValues3[x])
string += F(" selected");
string += ">";
string += options3[x];
string += F("</option>");
case PLUGIN_GET_DEVICENAME: {
string = F(PLUGIN_NAME_048);
break;
}
string += F("</select>");
success = true;
break;
}
case PLUGIN_GET_DEVICEVALUENAMES: {
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0],
PSTR(PLUGIN_VALUENAME1_048));
break;
}
case PLUGIN_WEBFORM_SAVE: {
String plugin1 = WebServer.arg(F("plugin_048_adr"));
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = (int) strtol(plugin1.c_str(), 0, 16);
String plugin2 = WebServer.arg(F("plugin_048_motortype"));
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = plugin2.toInt();
String plugin4 = WebServer.arg(F("plugin_048_motornumber"));
Settings.TaskDevicePluginConfig[event->TaskIndex][3] = plugin4.toInt();
success = true;
break;
}
case PLUGIN_WEBFORM_LOAD: {
case PLUGIN_INIT: {
Plugin_048_MotorShield_address = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
Plugin_048_MotorType = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
Plugin_048_MotorNumber = Settings.TaskDevicePluginConfig[event->TaskIndex][3];
string +=
F("<TR><TD>I2C Address (Hex): <TD><input type='text' title='Set i2c Address of sensor' name='");
string += F("plugin_048_adr' value='0x");
string += String(Settings.TaskDevicePluginConfig[event->TaskIndex][0],HEX);
string += F("'>");
success = true;
break;
}
string +=
F("<TR><TD>Stepper: steps per revolution: <TD><input type='text' title='Set steps per revolution for steppers' name='");
string += F("plugin_048_MotorStepsPerRevolution' value='");
string += String(Settings.TaskDevicePluginConfig[event->TaskIndex][1]);
string += F("'>");
string +=
F("<TR><TD>Stepper speed (rpm): <TD><input type='text' title='Set speed of the stepper motor rotation in RPM' name='");
string += F("plugin_048_StepperSpeed' value='");
string += String(Settings.TaskDevicePluginConfig[event->TaskIndex][2]);
string += F("'>");
case PLUGIN_READ: {
success = false;
break;
}
case PLUGIN_WRITE: {
String tmpString = string;
int argIndex = tmpString.indexOf(',');
if (argIndex)
tmpString = tmpString.substring(0, argIndex);
if (tmpString.equalsIgnoreCase(F("MotorShieldCMD"))) {
// Create the motor shield object with the default I2C address
AFMS = Adafruit_MotorShield(Plugin_048_MotorShield_address);
if (Plugin_048_MotorType == 0) {
myMotor = AFMS.getMotor(Plugin_048_MotorNumber);
}
if (Plugin_048_MotorType == 1) {
// TODO remove hardcoded 200 steps per revolution (1.8 degree)
myStepper = AFMS.getStepper(200,Plugin_048_MotorNumber);
}
if (Plugin_048_MotorType == 2) {
// TODO Servo
}
String log = F("MotorShield: Address: 0x");
log += String(Plugin_048_MotorShield_address,HEX);
log += F(" Motortype: ");
log += String(Plugin_048_MotorNumber);
log += F(" Motor#: ");
log += String(Plugin_048_MotorNumber);
addLog(LOG_LEVEL_DEBUG, log);
AFMS.begin(); // create with the default frequency 1.6KHz
success = true;
argIndex = string.indexOf(',');
tmpString = string.substring(argIndex + 1);
break;
}
String cmdString = tmpString.substring(0,tmpString.indexOf(','));
case PLUGIN_WEBFORM_SAVE: {
String plugin1 = WebServer.arg(F("plugin_048_adr"));
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = (int) strtol(plugin1.c_str(), 0, 16);
String plugin2 = WebServer.arg(F("plugin_048_MotorStepsPerRevolution"));
Settings.TaskDevicePluginConfig[event->TaskIndex][1] = plugin2.toInt();
String plugin3 = WebServer.arg(F("plugin_048_StepperSpeed"));
Settings.TaskDevicePluginConfig[event->TaskIndex][2] = plugin3.toInt();
success = true;
break;
}
if (cmdString.equalsIgnoreCase(F("Forward"))) {
if (event->Par2 >= 0 && event->Par2 <= 255) {
Plugin_048_MotorSpeed = event->Par2;
addLog(LOG_LEVEL_INFO, "DCMotor->Forward Speed: " + String(Plugin_048_MotorSpeed));
myMotor->setSpeed(Plugin_048_MotorSpeed);
myMotor->run(FORWARD);
case PLUGIN_INIT: {
Plugin_048_MotorShield_address = Settings.TaskDevicePluginConfig[event->TaskIndex][0];
Plugin_048_MotorStepsPerRevolution = Settings.TaskDevicePluginConfig[event->TaskIndex][1];
Plugin_048_StepperSpeed = Settings.TaskDevicePluginConfig[event->TaskIndex][2];
success = true;
break;
}
case PLUGIN_READ: {
success = false;
break;
}
case PLUGIN_WRITE: {
String tmpString = string;
String cmd = parseString(tmpString, 1);
String param1 = parseString(tmpString, 2);
String param2 = parseString(tmpString, 3);
String param3 = parseString(tmpString, 4);
String param4 = parseString(tmpString, 5);
String param5 = parseString(tmpString, 6);
// String log = "Debug parsesting: ";
// log += tmpString;
// log += " cmd: ";
// log += cmd;
// log += " param1: ";
// log += param1;
// log += " param2: ";
// log += param2;
// log += " param3: ";
// log += param3;
// log += " param4: ";
// log += param4;
// log += " param5: ";
// log += param5;
// addLog(LOG_LEVEL_DEBUG_MORE, log);
// Commands:
// MotorShieldCMD,<DCMotor>,<Motornumber>,<Forward/Backward/Release>,<Speed>
if (cmd.equalsIgnoreCase(F("MotorShieldCMD")))
{
// Create the motor shield object with the default I2C address
AFMS = Adafruit_MotorShield(Plugin_048_MotorShield_address);
String log = F("MotorShield: Address: 0x");
log += String(Plugin_048_MotorShield_address,HEX);
addLog(LOG_LEVEL_DEBUG, log);
if (param1.equalsIgnoreCase(F("DCMotor"))) {
if (param2.toInt() > 0 && param2.toInt() < 5)
{
myMotor = AFMS.getMotor(param2.toInt());
if (param3.equalsIgnoreCase(F("Forward")))
{
byte speed = 255;
if (param4.toInt() >= 0 && param4.toInt() <= 255)
speed = param4.toInt();
AFMS.begin();
addLog(LOG_LEVEL_INFO, "DCMotor" + param2 + "->Forward Speed: " + String(speed));
myMotor->setSpeed(speed);
myMotor->run(FORWARD);
success = true;
}
if (param3.equalsIgnoreCase(F("Backward")))
{
byte speed = 255;
if (param4.toInt() >= 0 && param4.toInt() <= 255)
speed = param4.toInt();
AFMS.begin();
addLog(LOG_LEVEL_INFO, "DCMotor" + param2 + "->Backward Speed: " + String(speed));
myMotor->setSpeed(speed);
myMotor->run(BACKWARD);
success = true;
}
if (param3.equalsIgnoreCase(F("Release")))
{
AFMS.begin();
addLog(LOG_LEVEL_INFO, "DCMotor" + param2 + "->Release");
myMotor->run(RELEASE);
success = true;
}
}
}
}
else if (cmdString.equalsIgnoreCase(F("Backward"))) {
if (event->Par2 >= 0 && event->Par2 <= 255) {
Plugin_048_MotorSpeed = event->Par2;
addLog(LOG_LEVEL_INFO, "DCMotor->Backward Speed: " + String(Plugin_048_MotorSpeed));
myMotor->setSpeed(Plugin_048_MotorSpeed);
myMotor->run(BACKWARD);
// MotorShieldCMD,<Stepper>,<Motornumber>,<Forward/Backward/Release>,<Steps>,<SINGLE/DOUBLE/INTERLEAVE/MICROSTEP>
if (param1.equalsIgnoreCase(F("Stepper")))
{
// Stepper# is which port it is connected to. If you're using M1 and M2, its port 1.
// If you're using M3 and M4 indicate port 2
if (param2.toInt() > 0 && param2.toInt() < 3)
{
myStepper = AFMS.getStepper(Plugin_048_MotorStepsPerRevolution, param2.toInt());
myStepper->setSpeed(Plugin_048_StepperSpeed);
String log = F("MotorShield: StepsPerRevolution: ");
log += String(Plugin_048_MotorStepsPerRevolution);
log += F(" Stepperspeed: ");
log += String(Plugin_048_StepperSpeed);
addLog(LOG_LEVEL_DEBUG_MORE, log);
if (param3.equalsIgnoreCase(F("Forward")))
{
int steps = 0;
if (param4.toInt())
{
steps = param4.toInt();
if (param5.equalsIgnoreCase(F("SINGLE")))
{
AFMS.begin();
addLog(LOG_LEVEL_INFO, "Stepper" + param2 + "->Forward Steps: " + steps + " SINGLE");
myStepper->step(steps, FORWARD, SINGLE);
success = true;
}
if (param5.equalsIgnoreCase(F("DOUBLE")))
{
AFMS.begin();
addLog(LOG_LEVEL_INFO, "Stepper" + param2 + "->Forward Steps: " + steps + " DOUBLE");
myStepper->step(steps, FORWARD, DOUBLE);
success = true;
}
if (param5.equalsIgnoreCase(F("INTERLEAVE")))
{
AFMS.begin();
addLog(LOG_LEVEL_INFO, "Stepper" + param2 + "->Forward Steps: " + steps + " INTERLEAVE");
myStepper->step(steps, FORWARD, INTERLEAVE);
success = true;
}
if (param5.equalsIgnoreCase(F("MICROSTEP")))
{
AFMS.begin();
addLog(LOG_LEVEL_INFO, "Stepper" + param2 + "->Forward Steps: " + steps + " MICROSTEP");
myStepper->step(steps, FORWARD, MICROSTEP);
success = true;
}
}
}
if (param3.equalsIgnoreCase(F("Backward")))
{
int steps = 0;
if (param4.toInt())
{
steps = param4.toInt();
if (param5.equalsIgnoreCase(F("SINGLE")))
{
AFMS.begin();
addLog(LOG_LEVEL_INFO, "Stepper" + param2 + "->Backward Steps: " + steps + " SINGLE");
myStepper->step(steps, BACKWARD, SINGLE);
success = true;
}
if (param5.equalsIgnoreCase(F("DOUBLE")))
{
AFMS.begin();
addLog(LOG_LEVEL_INFO, "Stepper" + param2 + "->Backward Steps: " + steps + " DOUBLE");
myStepper->step(steps, BACKWARD, DOUBLE);
success = true;
}
if (param5.equalsIgnoreCase(F("INTERLEAVE")))
{
AFMS.begin();
addLog(LOG_LEVEL_INFO, "Stepper" + param2 + "->Backward Steps: " + steps + " INTERLEAVE");
myStepper->step(steps, BACKWARD, INTERLEAVE);
success = true;
}
if (param5.equalsIgnoreCase(F("MICROSTEP")))
{
AFMS.begin();
addLog(LOG_LEVEL_INFO, "Stepper" + param2 + "->Backward Steps: " + steps + " MICROSTEP");
myStepper->step(steps, BACKWARD, MICROSTEP);
success = true;
}
}
}
if (param3.equalsIgnoreCase(F("Release")))
{
AFMS.begin();
addLog(LOG_LEVEL_INFO, "Stepper" + param2 + "->Release.");
myStepper->release();
success = true;
}
}
}
}
else if (tmpString.equalsIgnoreCase(F("Release"))) {
addLog(LOG_LEVEL_INFO, "DCMotor->Release");
myMotor->run(RELEASE);
}
break;
}
break;
}
}
return success;
}
#endif