- #include "DualVNH5019MotorShield.h"
- DualVNH5019MotorShield md;
- //these speed variables are the ones that you need to set yourself
- //through experimentation based on your specific motors in your DL2.
- const int upSpeedA = 360;
- const int downSpeedA = -360;
- const int upSpeedB = 378; // must be positive < 400
- const int downSpeedB = -362; // must be negative < -400
- const int A = 0;
- const int B = 1;
- const int AB = 3;
- const int NOSTOP = 0;
- const int STOP = 1;
- const int downButtonPin = 3;
- const int upButtonPin = 5;
- const int STEP = 10;
- void stopIfFault()
- {
- if (md.getM1Fault())
- {
- Serial.println("M1 fault");
- while(1);
- }
- if (md.getM2Fault())
- {
- Serial.println("M2 fault");
- while(1);
- }
- }
- void up(int motor, int msec = 0, int stopmotor = STOP) {
- int upspeed;
- if(motor == A) {
- upspeed = upSpeedA;
- Serial.print("A going UP.\n");
- if(stopmotor == NOSTOP) {
- Serial.print(" and not stopping.\n");
- }
- }
- if(motor == B) {
- upspeed = upSpeedB;
- Serial.print("B going UP.\n");
- if(stopmotor == NOSTOP) {
- Serial.print(" and not stopping.\n");
- }
- }
- if(motor == AB) {
- if (upSpeedB <= upSpeedA) {
- upspeed = upSpeedA;
- } else {
- upspeed = upSpeedB;
- }
- Serial.print("A and B going UP.\n");
- if(stopmotor == NOSTOP) {
- Serial.print(" and not stopping.\n");
- }
- }
- for (int i = 0; i <= upspeed; i+=STEP)
- {
- if (motor == A) md.setM1Speed(i);
- if (motor == B) md.setM2Speed(i);
- if (motor == AB) {
- if (i <= upSpeedA) md.setM1Speed(i);
- if (i <= upSpeedB) md.setM2Speed(i);
- }
- stopIfFault();
- delay(2);
- }
- if (msec > 0) delay(msec);
- if (motor == A || motor == AB) {
- Serial.print("A current: ");
- Serial.println(md.getM1CurrentMilliamps());
- if (stopmotor == STOP) md.setM1Speed(0);
- }
- if (motor == B || motor == AB) {
- Serial.print("B current: ");
- Serial.println(md.getM2CurrentMilliamps());
- if (stopmotor == STOP) md.setM2Speed(0);
- }
- }
- void down(int motor, int msec = 0, int stopmotor = STOP) {
- int downspeed;
- if(motor == A) {
- downspeed = downSpeedA;
- Serial.print("A going DOWN\n");
- if(stopmotor == NOSTOP) {
- Serial.print(" and not stopping.\n");
- }
- }
- if(motor == B) {
- downspeed = downSpeedB;
- Serial.print("B going UP\n");
- if(stopmotor == NOSTOP) {
- Serial.print(" and not stopping.\n");
- }
- }
- if(motor == AB) {
- if (downSpeedB <= downSpeedA) {
- downspeed = downSpeedB;
- } else {
- downspeed = downSpeedA;
- }
- Serial.print("A and B going DOWN.\n");
- if(stopmotor == NOSTOP) {
- Serial.print(" and not stopping.\n");
- }
- }
- for (int i = 0; i >= downspeed; i-=STEP)
- {
- if (motor == A) md.setM1Speed(i);
- if (motor == B) md.setM2Speed(i);
- if (motor == AB) {
- if (i >= downSpeedA) md.setM1Speed(i);
- if (i >= downSpeedB) md.setM2Speed(i);
- }
- stopIfFault();
- delay(2);
- }
- if (msec > 0) delay(msec);
- if (motor == A || motor == AB) {
- Serial.print("A current: ");
- Serial.println(md.getM1CurrentMilliamps());
- if (stopmotor == STOP) md.setM1Speed(0);
- }
- if (motor == B || motor == AB) {
- Serial.print("B current: ");
- Serial.println(md.getM2CurrentMilliamps());
- if (stopmotor == STOP) md.setM2Speed(0);
- }
- }
- void stop(int motor) {
- if(motor == A) md.setM1Brake(0);
- if(motor == B) md.setM2Brake(0);
- if(motor == AB) md.setBrakes(0,0);
- }
- int donealready = 0;
- void manualMode() {
- int downButtonState;
- int upButtonState;
- while(1) {
- downButtonState = digitalRead(downButtonPin);
- if (downButtonState == HIGH) {
- Serial.print("downButton is pressed.\n");
- down(AB,5);
- }
- int upButtonState = digitalRead(upButtonPin);
- if (upButtonState == HIGH) {
- Serial.print("upButton is pressed.\n");
- up(AB,5);
- }
- // delay();
- }
- }
- // the time and downfactor are very sensitive variables
- // you must determine and set for your own setup.
- int time = 11850;
- float downfactor = 1.012;
- void exerciseMode() {
- delay(60000);
- up(AB, 0, NOSTOP);
- delay(time);
- stop(AB);
- delay(60000);
- down(AB, 0, NOSTOP);
- delay(time*downfactor);
- stop(AB);
- delay(60000);
- }
- void setup()
- {
- Serial.begin(115200);
- Serial.println("Dual VNH5019 Motor Shield starting in 5 seconds\n");
- pinMode(downButtonPin, INPUT);
- pinMode(upButtonPin, INPUT);
- md.init();
- delay(5000);
- }
- void loop()
- {
- //manualMode();
- exerciseMode();
- }