CE 432
Robot Arm Basics
James Ferguson

Introduction
This lab follows the tutorials laid out here by Dr. Li. It involves using stepper motors and rotery encoders to create the basis for a robot arm.

Methods
The following scripts were written over the course of this lab.

Master Script
#include<Wire.h>
const int ENCODER_CLK = 3;
const int ENCODER_DT = 2;
void setup() {
  Serial.begin(9600);
  pinMode(ENCODER_CLK, INPUT);
  pinMode(ENCODER_DT, INPUT);
  Wire.begin();
}

void loop() {
  static int8_t counter[3];
  static bool lastClock;
  bool clock = digitalRead(ENCODER_CLK);
  if (lastClock && !clock) { // did the encoder rotate?
    if (digitalRead(ENCODER_DT)) { // what was the direction of the rotation?
      counter[0]++;
    } else {
      counter[0]--;
    }
    Serial.println(counter[0]);
    Wire.beginTransmission(9); // send through I2C to slave, 9 here is the address of the slave boar
    Wire.write(counter[0]); // Transfers the value of potentiometer to the slave boar
    Wire.endTransmission();
  }
  lastClock = clock;
  delay(10);
}

Slave Script
#include<Wire.h>
#include <Stepper.h>
const int stepsPerRevolution = 2038;
Stepper stepper(stepsPerRevolution, 13, 11, 12, 10); // 8, 10, 9, 1
int8_t target;
 
void receiveEvent(int bytes){
  for (int i = 0; i < bytes; i++) { // get all the bytes only use the most up to date one
    target = Wire.read();
  }
}
 
void setup() {
  Serial.begin(9600);
  Wire.begin(9);// matching the address defined in the master.
  Wire.onReceive(receiveEvent);// whenever receives anything will trigger the receiveEvent() function
  stepper.setSpeed(5);
}
 
void loop() {
  // rotate twords the target
  // each increment of the target is 18 degrees
  // each increment of the stepper is 0.1766437684 degrees
  static float position;
  if (position + 0.2 < target * 18) {
    stepper.step(1);
    position +=  0.1766437684;
    // print status
    Serial.print(target * 18);
    Serial.print(" ");
    Serial.println(position);
  } else if (position - 0.2  > target * 18) {
    stepper.step(-1);
    position -= 0.1766437684;
    // print status
    Serial.print(target * 18);
    Serial.print(" ");
    Serial.println(position);
  }
}

Results


Discussion
These components and scripts form the basis for creating a robot arm that replicates the position of another arm.