Code for stepper motor

Zongxi Huang

http://www.cunningturtle.com/wiki/index.php?title=Radio_Controlled_Stepper

https://www.arduino.cc/en/Tutorial/StepperOneRevolution

code (basic):

/*
 Stepper Motor Control - one revolution

 This program drives a unipolar or bipolar stepper motor.
 The motor is attached to digital pins 8 - 11 of the Arduino.

 The motor should revolve one revolution in one direction, then
 one revolution in the other direction.


 Created 11 Mar. 2007
 Modified 30 Nov. 2009
 by Tom Igoe

 */

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);

  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}


code (for speed control):

/*
 Stepper Motor Control - speed control

 This program drives a unipolar or bipolar stepper motor.
 The motor is attached to digital pins 8 - 11 of the Arduino.
 A potentiometer is connected to analog input 0.

 The motor will rotate in a clockwise direction. The higher the potentiometer value,
 the faster the motor speed. Because setSpeed() sets the delay between steps,
 you may notice the motor is less responsive to changes in the sensor value at
 low speeds.

 Created 30 Nov. 2009
 Modified 28 Oct 2010
 by Tom Igoe

 */

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor


// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int stepCount = 0;  // number of steps the motor has taken

void setup() {
  // nothing to do inside the setup
}

void loop() {
  // read the sensor value:
  int sensorReading = analogRead(A0);
  // map it to a range from 0 to 100:
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
  // set the motor speed:
  if (motorSpeed > 0) {
    myStepper.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution / 100);
  }
}

11/07/18: Coding research/Testing materials

Zongxi Huang

Today, I worked on researching online for Arduino coding for the stepper motors. I looked for ways to control the speed and the rotational movements of the motor using a RC controller. After some research, I found useful information on YouTube and on the official Arduino website. I also found links to forums which seemed to provide tutorials on what I want Tom achieve. Unfortunately, the school internet has barred me access to the ose websites. Hence, I decided that I would make further research at home on Friday. 

Aside from getting information on coding stepper motors with the Arduino, I thought about the type of material that I wanted to use. Some of the sources I found on the topic suggested that robots should use materials like aluminum or plastic. However, I found that the cost to acquire these materials was simply too high. Furthermore, I had little experience manipulating them Andy had no effective ways to cut them into the shapes I wanted. Hence, I decided to rely on thicker acrylic to ensure the integrity of the leg. After all, the stepper motors’ performance showed me that they have a lot of torque. 

Next class, I plan to start coding and linking the stepper motors with the Arduino board. I would borrow one of Lauren’s stepper motor for testing purposes if she allows for it. 

11/14/18: Midterm review/presentation

Zongxi Huang

In today's period, I presented gave my midterm presentations. From the feedback I received, I learned that I could use brushless motors to power legs. By adding a 3D printed gearbox, the motors would have the same amount of torque as the stepper motors while also being much lighter. Other suggestions for my project included that I should add other limbs to increase the balance of the robot (such as tails, heads etc.) and that joints such as shoulders and knees should be able to move in other directions than forwards and backwards. This would increase the mobility of the robot and its capabilities to go over uneven terrain. 

From the feedback I received, I concluded that I should first attempt to use stepper motors to power my legs. For the moment, I would rely on the Arduino and motor shield setup, as it is the most accessible and I am most acquainted with it.

Next class, I plan on testing out the viability of my design and of the arduino and motor shield setup. I will then use the results to determine where to go next. 

code

Zongxi Huang

//Controlling rotation angle/steps of a stepper motor from serial port

#include <AFMotor.h>
int steps = 32;
int ext_step = steps*64;


AF_Stepper motor(ext_step,2);
int Reset = 1024;

void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("BinuZकेHobbieZ");
motor.setSpeed(10);

motor.step(Reset, FORWARD,SINGLE);
Serial.println("Motor angle reset to centre");

}

void loop()
{
// put your main code here, to run repeatedly:

if(Serial.available() > 0);
{
if (Serial.peek()== ('R'))
{
Serial.read();
int data = Serial.parseInt();
int NegAngle = map(data, 0, 360, 0, 2048);
motor.step(NegAngle, BACKWARD,SINGLE);
Serial.print("Motor turns right ");
Serial.print(data);
Serial.print(" Degrees and steps ");
Serial.println(NegAngle);
}

if (Serial.peek()== ('L'))
{
Serial.read();
int data = Serial.parseInt();
int PosAngle = map(data, 0, 360, 0, 2048);
motor.step(PosAngle, FORWARD,SINGLE);
Serial.print("Motor turns Left ");
Serial.print(data);
Serial.print(" Degrees and steps ");
Serial.println(PosAngle);

}

}

}

28/11/18: meeting, and sketch of the planned robot

Zongxi Huang

During today's class time, I met with Mr. Luis to discuss about the electronics part and other aspects of my project. I first showed him the full robot leg and foot that I had been working on for the past week. Then I showed him the research I made on how to control the stepper motors and the progress I have made so far. After analyzing the situation, he pointed out the code I had been relying on were outdated or incompatible with my current Arduino-motor shield setup. 

Some of the codes I used were made for older versions of the motor shield (v1) and others required a stepper motor driver to work. As such, he suggested that I should wait until the stepper motor drivers to arrive and work it out from there. He also also suggested that I experiment more with servos, as some servos could be strong enough to support my robot and fast enough to move my robot. He pointed out that my final iteration could use both stepper motors and servos. 

At the end of our meeting, he asked that I draw a sketch of the final iteration of my robot. Tomorrow, as I still have to wait for the motor drivers to arrive, I plan on working on the support wall for the stepper motor shaft in the meantime.

Demo Day Poster

Zongxi Huang
Legged Robot Demo Day Poster.pdf
Legged robot 3.0.indd

Demo Day Final Poster 2

Zongxi Huang
Legged Robot Demo Day Poster.pdf
Legged robot 3.0.indd

Term 1 final code

Zongxi Huang

/*
* Attach an Adafruit Motor Shield to the Arduino Uno/Mega.
* In addition to powering the RC Receiver (with GND and 5V), connect
* one of the RC Receiver's signal pins to the Arduino's digital pin 2.
* The duration of the high RC pulse will be used to control a motor
* attached to Motor Shield motor port 1.
*
* Author: David Wang, NuVu Studio
*/
#include <RCPulseIn.h>
#include <EnableInterrupt.h>


#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_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 = AFMStop.getMotor(1);


int val = 3;
// define an interrupt for digital pin 2.
// that will need to read an RC pulse train.
defineRC(val);


void setup() {
//while (!Serial);
Serial.begin(9600);           // set up Serial library at 9600 bps
// setup the RC interrupt for digital pin 2.
// (You need to use "defineRC(pin)" for each pin, see above.)
setupRC(val);

//AFMSbot.begin(); // Start the bottom shield
AFMStop.begin(); // Start the top shield
myStepper3->setSpeed(10);  // 10 rpm
myStepper2->setSpeed(10);  // 10 rpm

myStepper3->step(12, BACKWARD, MICROSTEP);
delay(500);
myStepper2->step(23, BACKWARD, MICROSTEP);

// turn on the DC motor
//myMotor1->setSpeed(5);
//myMotor1->run(RELEASE);
}

int i;

void loop() {
//myMotor1->run(FORWARD);

// Use the function "readRC(pinNumber)" to read the duration  
// of the high voltage pulse to digital pin 2.
// (You need to use "defineRC(pin)" and "setupRC(pin)" for each pin, see above.)
Serial.print(readRC(val));
Serial.println();
int SM = deadbandMap(readRC(val),1000,200,2000,-255,0,255);

if(SM>10){
//myStepper3->step(12, FORWARD, DOUBLE);
myStepper3->step(17, FORWARD, MICROSTEP);

delay(100);

for (i=0; i<16; i++) {
//myStepper2->setSpeed(1000);
//myStepper3->setSpeed(1000);
//myMotor1->setSpeed(i);  
//myStepper1->onestep(FORWARD, INTERLEAVE);
myStepper2->onestep(FORWARD, DOUBLE);
myStepper3->onestep(FORWARD, DOUBLE);
//myStepper2->onestep(FORWARD, MICROSTEP);
//myStepper3->onestep(FORWARD, MICROSTEP);
delay(100);
}

//myStepper2->step(15, FORWARD, DOUBLE);
myStepper2->step(15, FORWARD, MICROSTEP);

delay(100);

/////////////

//myStepper3->step(12, BACKWARD, DOUBLE);
myStepper3->step(17, BACKWARD, MICROSTEP);

delay(100);

for (i=16; i!=0; i--) {
//myStepper2->setSpeed(1000);
//myStepper3->setSpeed(i);
//myMotor1->setSpeed(i);  
//myStepper1->onestep(BACKWARD, INTERLEAVE);
myStepper2->onestep(BACKWARD, DOUBLE);
myStepper3->onestep(BACKWARD, DOUBLE);
//myStepper2->onestep(BACKWARD, MICROSTEP);
//myStepper3->onestep(BACKWARD, MICROSTEP);
delay(100);
}
//myStepper2->step(15, BACKWARD, DOUBLE);
myStepper2->step(15, BACKWARD, MICROSTEP);

delay(100);

//myMotor1->run(BACKWARD);

}
else if(SM<-10){
//myStepper3->step(12, FORWARD, DOUBLE);
myStepper3->step(20, FORWARD, MICROSTEP);

delay(100);

for (i=0; i<18; i++) {
//myStepper2->setSpeed(1000);
//myStepper3->setSpeed(1000);
//myMotor1->setSpeed(i);  
//myStepper1->onestep(FORWARD, INTERLEAVE);
myStepper2->onestep(FORWARD, DOUBLE);
myStepper3->onestep(FORWARD, DOUBLE);
//myStepper2->onestep(FORWARD, MICROSTEP);
//myStepper3->onestep(FORWARD, MICROSTEP);
delay(100);
}

//myStepper2->step(15, FORWARD, DOUBLE);
myStepper2->step(35, FORWARD, MICROSTEP);

delay(100);

/////////////

//myStepper3->step(12, BACKWARD, DOUBLE);
myStepper3->step(20, BACKWARD, MICROSTEP);

delay(100);

for (i=18; i!=0; i--) {
//myStepper2->setSpeed(1000);
//myStepper3->setSpeed(i);
//myMotor1->setSpeed(i);  
//myStepper1->onestep(BACKWARD, INTERLEAVE);
myStepper2->onestep(BACKWARD, DOUBLE);
myStepper3->onestep(BACKWARD, DOUBLE);
//myStepper2->onestep(BACKWARD, MICROSTEP);
//myStepper3->onestep(BACKWARD, MICROSTEP);
delay(100);
}
//myStepper2->step(15, BACKWARD, DOUBLE);
myStepper2->step(35, BACKWARD, MICROSTEP);

delay(100);

//myMotor1->run(BACKWARD);
}
else {
myStepper2->release();
myStepper3->release();
}
}