firmata with pololu dual vnh5019 motor shield for arduino uno

edited April 2016 in Arduino

The goal is to create a rectangle in Processing that increases the speed up to max at the top of the y coordinate max reverse speed at the bottom of the y coordinate. The servo fimata aspect works fine as the servo swings from 180 to 0 degrees based on the position of the mouse within the rectangle.

Right now the dc motor goes in the correct direction based upon if the mouse is above or below the middle of the rectangle but it jumps and is not consistent.

The use for this is for a phone controlled rover due in three weeks. I'm freaking out this is my ME senior design project. I think something is wrong with my arduino sketch.

Additionally I'm wondering how I use the serial monitor in firmata... arduino says my com port is busy due to it already being used by processing's sketch. Is there a work around for this? I'm kind of freaking out.

Thanks, Lucas

PS I have some random comments I left in from another firmata sketch that I built based off the servo firmata example and a working adafruit motorshield firmata sketch. If these comments are confusing I can repost cleaner versions of this codes.

    //PROCESSING CODE       

    import processing.serial.*; 
    import cc.arduino.*;


    int input1 = 5;
    int input2 = 6;
    Arduino arduino; // Setup an arduino object to communicate with Firmata firmware

    // variables for the sampling
    long time;
    int servoDelay = 2;

    // variables for the servo position
    int xPos;
    int yPos;
    int changePosX,changePosY;

    // sets a maximum change of 5 degrees
    int deltaMax = 5;

    void setup()
    {
      size(180, 800); 
      //println(Serial.list());   
      // Using Firmata library
      arduino = new Arduino(this, Arduino.list()[0], 57600);

      // initializes variables
      time = millis();
      xPos = mouseX;
      yPos = mouseY;
    }
    /*void userevent() {

      if (key=='1') {
        arduino.analogWrite(input1, 1);
        changePosX = xPos;
          changePosY = yPos;

        println("forward");
      } else if (key=='2') {
        arduino.analogWrite(input1, 0);
        changePosX = 180-xPos;
          changePosY = 180-yPos;

         println("backward");
      }
    }*/



      void draw() {
        // local variables for the change in servo positions
        //int changePosX, changePosY;
       // print(millis() +","+(time +servoDelay)+"---");

        // timer check statement to make the servo update at a lower frequency
        if (millis() > (time + servoDelay)) {
          // constrains the servo angle change to +/- deltaMax
          changePosX = constrain((mouseX-xPos), -deltaMax, deltaMax);
          changePosY = constrain((mouseY-yPos), -deltaMax, deltaMax);
          println(changePosX+","+changePosY);

          // sets the new motor positions
          xPos += changePosX;
          yPos += changePosY;

          //resets timer
          time = millis();
        }

        //Writes to Arduino pins for it to intercept and transfer to servo motion
        arduino.analogWrite(input1, yPos);
        delay(2);
        arduino.analogWrite(input2, xPos);
        delay(2);
        println(xPos+","+yPos);
      }

//The arduino code is:

    #include <Wire.h>
    //#include <Adafruit_MotorShield.h>
    //#include "utility/Adafruit_PWMServoDriver.h"
    #include <Servo.h>
    #include <Firmata.h>
    #include "DualVNH5019MotorShield.h"

    //Adafruit_MotorShield AFMS = Adafruit_MotorShield();
    //Adafruit_DCMotor *myMotor = AFMS.getMotor(1);


    Servo servo1;
    DualVNH5019MotorShield md; //Servo servo2;


    int input1 = 5;
    int input2 = 6;

    void stopIfFault()
    {
      if (md.getM1Fault())
      {
        Serial.println("M1 fault");
        while (1);
      }

    }

    void setup() {
      Serial.begin(9600);
      Serial.println("Dual VNH5019 Motor Shield");
      md.init();
      servo1.attach(9);


      //  servo2.attach(10);
      //  myMotor->setSpeed(0);
      //  myMotor->run(FORWARD);
      // turn on motor
      //  myMotor->run(RELEASE);

      Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
      Firmata.attach(ANALOG_MESSAGE, analogWriteCallback); // Call this function when analog writes are received

      Firmata.begin(57600);

    }

    void analogWriteCallback(byte pin, int value)
    {

      // If data is sent to reserved pin, execute code
      if (pin == input1) {
        if ( value > 410 || value < 390) {
          md.setM1Speed((400 - value)); //speed max is 400
          stopIfFault();


        }


        else {
          md.setM1Speed(0);

          stopIfFault();


        }
      }
      else if (pin == input2) {

        servo1.write(value);
      }
    }


    void loop() {
      while (Firmata.available()) { // Handles Firmata serial input
        Firmata.processInput();
      }

    }

Answers

Sign In or Register to comment.