Thursday, June 11, 2015

Day 17

The last few days, Tim and I have been doing our best to figure out to control the motors using the Arduino board connected to the rep-rap board. Our starting point was the example code in the Arduino software to move the MotorKnob of a stepper. The example code was only for one stepper, and after some research we figured out ways of plugging in all 3 motors to the Arduino board which will be used to move the X, Y and Z axises in the platform for the HDPrime.

We were able to move the motors using the Arduino board. However there were a few limitations in moving the motors. First, Arduino code used a loop function called 'void loop()' which was needed to move the motor. This would continuously move the motor until there was no power in the Board or a different code was uploaded to the board. Second of, every time we need to change the amount of movement in the motor, we need to go through the Arduino code and change the value of the amount of movement. The method was not practical, so we needed an interface to interact with the Arduino board in order to input the values we needed.

After a briefly brainstorming, we decided to use RaspberryPi as an interface to send signals to the Arduino board. When we were learning Arduino code previously, when I barely stumbled upon a relationship between Arduino and RaspberryPi, so I knew it as a possibility. I had previous knowledge of RaspberryPi and Perl language beforehand, so Tim and I decided to give it a chance. Next day, we  had thorough research of RaspberryPi and Arduino in internet. This was challenging, because we used Perl language and had to initialize the connection between Arduino and RaspberryPi from the scratch. Initializing the connection between the boards was exhausting, because we found different instructions on each website we looked into. It was something that nobody had tried before. After spending quite some time on it, we came to a better understanding. We realized that we can actually send the values of the amount we need the motors to move, from the RaspberryPi to the Arduino board. So, through the code that we wrote, and is already uploaded, in the Arduino board, it will receive these values and then move the motors. However, the values Arduino receives from the RaspberryPi are in the ASCII format. Hence, after Dr McColgan's advice, we started calibrating the movement of the motors with ASCII and decimal values that the Arduino receives.

While doing so, we came across another issue. The motor did not receive enough power to provide the toque that is needed to move a pulley/belt which makes the platform move. So we are in process of searching suitable power sources to provide enough power to move the 3 motors.

Following is the code that was uploaded to Arduino board to test the connection between the RaspberryPi and the Arduino board.

/*
  Mega multple serial test

 Receives from the main serial port, sends to the others. 
 Receives from serial port 1, sends to the main serial (Serial 0).

 This example works only on the Arduino Mega

 The circuit: 
 * Any serial device attached to Serial port 1
 * Serial monitor open on Serial port 0:

 created 30 Dec. 2008
 modified 20 May 2012
 by Tom Igoe & Jed Roach

 This example code is in the public domain.

 */


void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    byte inByte = Serial1.read();
    Serial1.print("I received p1: ");
    Serial1.println(inByte, DEC); 
  }
  
  // read from port 0, send to port 1:
  if (Serial.available()) {
    byte inByte = Serial.read();
    Serial.print("I received p2: ");
    Serial.println(inByte);
  }

}

It WORKS! we received the transmitted data...

Following is the Perl code that we used to set the connection in RaspberryPi with the Arduino Board. We used this code to send data values to the Arduino.

#!/usr/bin/perl
# MoveMotor.pl

use Device::SerialPort;

my $port = Device::SerialPort->new("/dev/ttyACM0");

$port->baudrate(9600);
$port->databits(8);
$port->parity("none");
$port->stopbits(1);
$port->handshake("xoff");
$port->write_settings;
$port->dtr_active(0);

#$port->write('include <Stepper.h>');
#$port->write('define STEPS 100');

#my $motorX = $port->write('Stepper stepperX(STEPS, 10,11,12,13));
#my $motorY = $port->write('Stepper stepperY(STEPS, 6,7,8,9));
#my $motorZ = $port->write('Stepper stepperZ(STEPS, 2,3,4,5));

#my $sx = 30;
#my $sy = 30;
#my $sz = 30;

#while (1) {
#    $port->write('$sx');
#    $port->write('$sy');
#    $port->write('$sz');
#    print($port->receive(), "\n");
#    sleep(1);
#}

while (1) {
    
    print "Move in X Direction: ";
    my $x = <STDIN>;
    chomp($x);
    print "Sending $x...\n";

    print "Move in Y Direction: ";
    my $y = <STDIN>;
    chomp($y);
    print "Sending $y...\n";

    print "Move in Z Direction: ";
    my $z = <STDIN>;
    chomp($z);
    print "Sending $z...\n\n\n";

    $port->write("$x");
    $port->write("$y");
    $port->write("$z");
}
$port->close();

Perl language: '#' lines are comments that are not effected to overall program execution.
They were several approaches we used.

Following is the Arduino code that we used to move the motors. This is the code that reads data from RaspberryPi and hence moves the motors.

#include <Stepper.h>

#define STEPS 200

Stepper stepperX(STEPS, 10,11,12,13);
Stepper stepperY(STEPS, 6,7,8,9);
Stepper stepperZ(STEPS, 2,3,4,5);

void setup()
{
  Serial.begin(9600);
  // set the speed of the motor to 30 RPMs
  stepperX.setSpeed(30);
  stepperY.setSpeed(30);
  stepperZ.setSpeed(60);
}

//int mx = 10;
//int my = 20;
//int mz = 50;

/* void loop() {
 MoveX();
 MoveY();
 MoveZ();
 delay(12);
}
*/

void loop() {
  if (Serial.available()) {  
   int mx = Serial.read();
   Serial.print("I received mx: ");
   Serial.println (mx);
   stepperX.step(mx);
  }

  if (Serial.available()) {  
   int my = Serial.read();
   Serial.print("I received my: ");
   Serial.println (my);
   stepperY.step(my);
  }

  if (Serial.available()) {  
   int mz = Serial.read();
   Serial.print("I received mz: ");
   Serial.println (mz);
   stepperZ.step(mz);
  }
}
Arduino Board connected to the reprap which is in link with the motors

Our attempt to move the Printrbot print bed
using the motors connected to the Arduino,
RaspberryPi and the reprap

RaspberryPi in Action

No comments:

Post a Comment