Search This Blog

Friday, 2 January 2015

Teensy 3.1 based Repstrap control board initial wiring completed - test #1

Sorry that this is taking so long, but... (continued from last week...)
I finally have enough of the control board wired up that I can test the I2C library with Adafruit's Motor Shield V2 as well as the Adafruit LCD Backpack.
I simply took the DCMotorTest2 arduino example from the Motor Shield library, and added the LCD functionality. 

Note: Both the Motor Shield Library, as well as the LiquidCrystal libraries needed to be modified to work with Teensy 3.1.

As the Teensy is not AVR based, the I2C functions a bit different (better, trust me!)


In both instances, references to the "Wire" library had to be replaced with Teensy's  "I2C_T3" library.  ie:

//#include <Wire.h>
#include <i2c_t3.h>    // Replacement I2C library for Teensy 3.1


Also in the Motorshield library as well as it's underlying PWMServoDriver library, I had to force it to use the correct I2C channel.   As the ARM processor in the Teensy has two separate I2C channels, it was defaulting to the second one.


I found this little snippet at the beginning of each library, and modified it to use the first I2C channel.

#include "Adafruit_MotorShield.h"
#include <Adafruit_PWMServoDriver.h>
#ifdef __AVR__                        // Teensy definitely is not AVR, so it defaults
 #define WIRE Wire
#else // Arduino Due               // to wire1 as per the next line.
 // #define WIRE Wire1           // Wire1 in Teensy world is the second I2C
 #define WIRE Wire                // So i simply commented out and replaced.
#endif                                      // Not elegant, but...

And here is the example code to simply ramp the DC motor from 0-255 forward, back down to 0 and then do it again in reverse.  All the while displaying status on the 20x4 LCD panel.



/*
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 <i2c_t3.h> 
// wire for Teensy 3.1 per https://forum.pjrc.com/threads/21680-New-I2C-library-for-Teensy3
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_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);

/*
Using with the Adafruit LCD Backpack to display information
---->    http://www.adafruit.com/products/292
*/

#include "LiquidCrystal.h"

// Connect LiquidCrystal display via i2c, default address #0x20 (A0-A2 not jumpered)
LiquidCrystal lcd(0);

int spd = 150;          // Motor PWM speed from 0 - 255
int mState = FORWARD;   // State of the motor

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Adafruit Motorshield v2 - DC Motor test!");

  // set up the LCD's number of rows and columns:
  lcd.begin(20, 4);
  lcd.print("Adafruit Motorshield v2");
  lcd.setCursor(0, 1);
  lcd.print("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(spd);
  myMotor->run(mState);
  mState = RELEASE;
  // turn on motor
  myMotor->run(mState);
}

void loop() {
 
  Serial.print("tick");
  lcd.setCursor(0, 3);
  lcd.print("tick");

  mState = FORWARD;
  myMotor->run(mState);
  lcd.setCursor(9, 2);
  lcd.print(" Dir = FWD ");         // Display direction on LCD
 
  for (spd=0; spd<255; spd++) {
    myMotor->setSpeed(spd); 
    lcd.setCursor(0, 2);
    lcd.print("PWM = "); lcd.print(spd); lcd.print(" ");
  }
  for (spd=255; spd!=0; spd--) {
    myMotor->setSpeed(spd); 
    lcd.setCursor(0, 2);
    lcd.print("PWM = "); lcd.print(spd); lcd.print(" ");
  }
 
  Serial.print("tock");
  lcd.setCursor(0, 3);
  lcd.print("tock");

  mState = BACKWARD;
  myMotor->run(mState);
  lcd.setCursor(9, 2);
  lcd.print(" Dir = REV ");         // Display direction on LCD

  for (spd=0; spd<255; spd++) {
    myMotor->setSpeed(spd); 
    lcd.setCursor(0, 2);
    lcd.print("PWM = "); lcd.print(spd); lcd.print(" ");
  }
  for (spd=255; spd!=0; spd--) {
    myMotor->setSpeed(spd); 
    lcd.setCursor(0, 2);
    lcd.print("PWM = "); lcd.print(spd); lcd.print(" ");
  }
 

  Serial.print("tech");
  lcd.setCursor(0, 3);
  lcd.print("tech");
 
  myMotor->run(RELEASE);
  delay(1000);
}

Again, trivial, but it validated that my wiring is correct this far, and that the I2C libraries are functional.

Over the weekend, I'll get the Quadrature Decoders and PID control running, and post another update then.


No comments:

Post a comment