Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

A series of motor tests -- different ways to drive small motors from …

…an Arduino
  • Loading branch information...
commit 61f3a829cdb99d6ae1f50f624824cf9fd00927bf 1 parent e0a7d8d
@akkana authored
View
87 motor-tests/ESCmotor/ESCmotor.ino
@@ -0,0 +1,87 @@
+/* -*- c -*- */
+
+// Drive a toy truck using two electronic speed controllers,
+// one for driving and one for steering,
+// and the Servo library.
+
+#include <Servo.h>
+
+#define MOTOR_PIN 8
+#define STEER_PIN 10
+
+Servo motorServo;
+Servo steerServo;
+
+// The GWS ESCs I'm using have a weird arming sequence:
+// move to low throttle, wait a second, then move to zero.
+void armESC(Servo servo)
+{
+ digitalWrite(13, HIGH);
+ servo.write(0);
+ delay(1200);
+ servo.write(70);
+ delay(1200);
+ servo.write(0);
+ delay(1200);
+ digitalWrite(13, LOW);
+}
+
+void setup()
+{
+ motorServo.attach(MOTOR_PIN);
+ steerServo.attach(STEER_PIN);
+
+ armESC(motorServo);
+ armESC(steerServo);
+}
+
+/* Drive at a speed from 1 to 10 */
+void drive(int speed)
+{
+ if (speed == 0) {
+ motorServo.write(0);
+ return;
+ }
+
+// Speeds. Servo simulates angles, so range is 0 to 180.
+// But the ESC only does anything between 70 and 140.
+// Add load, and anything under 100 really doesn't do anything noticable.
+#define SLOW 100
+#define FAST 140
+ int ESCspeed = speed * (FAST - SLOW) / 10 + SLOW;
+ motorServo.write(ESCspeed);
+}
+
+// Move the steering motor. direc can be -1, 1, or 0 --
+// but unfortunately I don't currently have a way to drive a motor
+// in both directions, so any nonzero value will steer the same way.
+// 0 moves back to center.
+#define STEER_SPEED 120
+void steer(int direc)
+{
+ if (direc != 0) {
+ digitalWrite(13, HIGH);
+ steerServo.write(STEER_SPEED);
+ }
+ else {
+ steerServo.write(0);
+ digitalWrite(13, LOW);
+ }
+}
+
+int steering = 0;
+
+void loop()
+{
+ //digitalWrite(13, HIGH);
+ steer(steering);
+ steering = !steering;
+ drive(5);
+ delay(1000); // drive
+
+ // Stop everything
+ //digitalWrite(13, LOW);
+ motorServo.write(0);
+ delay(2000); // sit 2 secs
+}
+
View
1  motor-tests/ESCmotor/Makefile
View
61 motor-tests/ESCtest/ESCtest.ino
@@ -0,0 +1,61 @@
+/* -*- c -*- */
+
+// Test an electronic speed controller (ESC) to find out its arming sequence.
+// Wouldn't it be nice if these were standard?
+//
+// How to use:
+// Connect Arduino digital pin 8 to the input of the ESC.
+// wire up a motor (with some load on it).
+// Connect the Arduino to USB, but can be powered either from USB
+// or from the ESC battery.
+// Switch on, and connect to usb-serial at 9600 baud.
+// Watch the numbers as the Arduino counts up, and see where the
+// motor first beeps.
+// If nothing has happened by the time you get to 180, you should
+// probably give up (the Servo library uses angles from 0 to 180).
+
+#include <Servo.h>
+
+// Pin 8 goes to the input of the ESC.
+#define MOTOR_PIN 8
+
+Servo motorServo;
+
+int counter = 0;
+
+void setup()
+{
+ motorServo.attach(MOTOR_PIN);
+ Serial.begin(9600);
+ motorServo.write(0);
+ delay(1000);
+ motorServo.write(70);
+ delay(1000);
+ motorServo.write(0);
+ delay(1000);
+ Serial.println("Counting up");
+ counter = 60;
+}
+
+void loop()
+{
+ Serial.println(counter);
+
+ digitalWrite(13, counter%5);
+ motorServo.write(counter++);
+
+ delay(200);
+
+ if (counter > 180) {
+ Serial.println("Hit 180 -- waiting 2 seconds");
+ delay(1800);
+ counter = 0;
+ Serial.println("Zero -- waiting 2 seconds");
+ motorServo.write(counter);
+ delay(2000);
+ Serial.println("Restarting count.");
+ }
+
+ return;
+}
+
View
1  motor-tests/ESCtest/Makefile
View
1  motor-tests/README
@@ -0,0 +1 @@
+Sketches to drive motors from an Arduino in lots of different ways.
View
1  motor-tests/motor-direct/Makefile
View
35 motor-tests/motor-direct/motor-direct.ino
@@ -0,0 +1,35 @@
+/* -*- c -*- */
+
+/*
+ * Arduino basic motor control
+ */
+
+#define DELAYTIME 1000 // milliseconds
+
+int motorPins[2] = { 5, 6 }; // plug the motor leads into these pins
+int direction = 0; // toggle between 0 and 1
+
+void setup()
+{
+ pinMode(motorPins[0], OUTPUT);
+ digitalWrite(motorPins[0], LOW);
+ pinMode(motorPins[1], OUTPUT);
+ digitalWrite(motorPins[1], LOW);
+}
+
+/* Alternate between two directions and motionless.
+ * Assume we start with both pins low, motor motionless.
+ */
+void loop()
+{
+ delay(DELAYTIME);
+
+ digitalWrite(motorPins[direction], HIGH);
+
+ delay(DELAYTIME);
+
+ digitalWrite(motorPins[direction], LOW);
+
+ direction = !direction;
+}
+
View
1  motor-tests/motordriver/Makefile
View
118 motor-tests/motordriver/motordriver.ino
@@ -0,0 +1,118 @@
+/* -*- c -*- */
+
+// Test of Pololu TB6612FNG Dual Motor Driver Carrier
+// http://www.pololu.com/catalog/product/713
+
+int stdby = 7; // standby pin, must be high for anything to work
+
+int dist_sensor = 0; // distance sensor on analog pin 0
+
+// And a few other constants:
+int driveSpeed = 80; // 0 to 255, I think
+int steerSpeed = 200;
+int backingTime = 2500; // msec
+
+////////////////////////////////////////////////////////////
+// Some generic code for handling the motor driver:
+
+// Motor pins
+// Each motor has two direction pins (both high: brake, both low: coast)
+// plus one PWM speed pin.
+struct motor {
+ int speed;
+ int dir[2];
+};
+
+// motors[0] is the drive motor; [1] is the steering motor.
+struct motor motors[] = {
+ { 9, { 2, 3 } },
+ { 10, { 4, 5 } }
+};
+
+void initMotor(int whichmotor)
+{
+ pinMode(motors[whichmotor].dir[0], OUTPUT);
+ pinMode(motors[whichmotor].dir[1], OUTPUT);
+ pinMode(motors[whichmotor].speed, OUTPUT);
+}
+
+void stop(int whichmotor)
+{
+ struct motor m = motors[whichmotor];
+ digitalWrite(m.speed, 0);
+ digitalWrite(m.dir[0], true);
+ digitalWrite(m.dir[1], true);
+}
+
+// Drive a motor in a direction, 0 or 1
+void drive(int whichmotor, int direc, int speed)
+{
+ if (direc == 0) {
+ digitalWrite(motors[whichmotor].dir[1], true);
+ digitalWrite(motors[whichmotor].dir[0], false);
+ } else {
+ digitalWrite(motors[whichmotor].dir[0], true);
+ digitalWrite(motors[whichmotor].dir[1], false);
+ }
+ analogWrite(motors[whichmotor].speed, speed);
+}
+
+// End of generic code for handling the motor driver
+////////////////////////////////////////////////////////////
+
+void back_and_turn()
+{
+ // Light the LED to show we're still in this routine
+ // (in case we get stuck):
+ digitalWrite(13, true);
+
+ // Brake the motor
+ stop(0);
+
+ // Turn the steering wheel
+ drive(1, 0, steerSpeed);
+
+ // Back up, relatively slowly
+ drive(0, 1, driveSpeed);
+ delay(backingTime);
+
+ // brake and un-steer:
+ stop(0);
+ stop(1);
+
+ // and turn off the LED
+ digitalWrite(13, false);
+}
+
+void setup()
+{
+ initMotor(0);
+ initMotor(1);
+
+ pinMode(stdby, OUTPUT);
+
+ pinMode(13, OUTPUT);
+
+ // Start with both motors braked:
+ stop(0);
+ stop(1);
+
+ delay(2000);
+
+ // Set the standby pin high so we can drive the motors:
+ digitalWrite(stdby, true);
+}
+
+void loop()
+{
+ // Are we about to run into something?
+ // The sensor returns around 1023 until we're within about 5-10cm
+ // of an object, at which point it goes down under 300.
+ int dist = analogRead(dist_sensor);
+ if (dist < 500)
+ back_and_turn();
+
+ drive(0, 0, driveSpeed);
+ delay(10);
+}
+
View
1  motor-tests/motorshield/Makefile
View
43 motor-tests/motorshield/motorshield.ino
@@ -0,0 +1,43 @@
+/* -*- c -*- */
+
+// Motor Shield test
+// by NKC Electronics
+// Test Motor B
+// http://mcukits.com/2009/03/12/assembling-the-freeduino-arduino-motor-shield/
+
+// Motor pins
+int dirApin = 13; // Direction pin for motor A is Digital 13
+int dirBpin = 12; // Direction pin for motor B is Digital 12
+int speedApin = 10; // Speed pin for motor A is Digital 10 (PWM)
+int speedBpin = 9; // Speed pin for motor B is Digital 9 (PWM)
+
+int speed = 255; // 0 to 255, I think
+int dir = 0;
+
+void setup()
+{
+ pinMode(speedApin, OUTPUT);
+ pinMode(speedBpin, OUTPUT);
+ pinMode(dirApin, OUTPUT);
+ pinMode(dirBpin, OUTPUT);
+ delay(2000);
+}
+
+void loop()
+{
+ // Turn and move:
+ digitalWrite(dirApin, dir); // set direction
+ digitalWrite(dirBpin, dir); // set direction
+ analogWrite(speedApin, speed); // set speed (PWM)
+ analogWrite(speedBpin, speed); // set speed (PWM)
+ delay(1200);
+
+ // Stop for half a sec
+ analogWrite(speedApin, 9);
+ analogWrite(speedBpin, 9);
+ delay(500);
+
+ // reverse direction:
+ dir = ((dir == 0) ? 1 : 0); // change direction
+}
+
View
1  motor-tests/transistor/Makefile
View
35 motor-tests/transistor/transistor.ino
@@ -0,0 +1,35 @@
+/* -*- c -*- */
+
+/*
+ * Arduino basic motor control, using a transistor.
+ * One direction only.
+ * Wiring diagram: http://itp.nyu.edu/physcomp/Tutorials/HighCurrentLoads
+ */
+
+#define DELAYTIME 1500 // milliseconds
+
+#define MOTOR 8 // pin controlling the motor (via the transistor)
+
+#define SPEED 150
+
+void setup()
+{
+ pinMode(MOTOR, OUTPUT);
+}
+
+/* Alternate between two directions and motionless.
+ * Assume we start with both pins low, motor motionless.
+ */
+void loop()
+{
+ delay(DELAYTIME);
+
+ analogWrite(MOTOR, SPEED);
+
+ delay(DELAYTIME);
+
+ analogWrite(MOTOR, 0);
+
+ delay(DELAYTIME);
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.