Monday, March 7, 2016

Sciborg, Intro

Last week we played around with Arduinos and seeing how they could control things.
This assignment we put that control to use to control sciborg. what is a sciborg?
This is a sciborg.

Its just a little robot lego cart with a computer.

This assignment was meant to introduce us to functions.  Functions are useful.  Learn to use functions judiciously.
What are functions? Functions are procedures or routines that may or may not return a result.  you can give them names and then call the functions.   when you call the function, the program does everything in the function that the function is supposed to do.   Functions are particularly useful for procedures or routines that are or can be expected to run more in more than one place in the code.  rather than typing the same code in two parts of the code file, you can place the commonalities into a function and call the same function from the two places that the function is needed and that will not only reduce the amount of code that one needs to write, but it increases abstraction of tasks and it makes finding mistakes and changing mistakes easier,


We explored functions in our SOS assignment:
In this assignment we had to rig up a LED to flash a SOS signal:
This was the code that enabled us to flash an SOS.

After this we moved on to the actually running the sciborgs.
before running the sciborgs we had to download and use a lot of libraries, that contained functions that allowed us to control and get feedback the different systems on a Sciborg.
These are the downloaded libraries.
what are libraries?  Libraries are collections of functions for a purpose.  Libraries exist so that person B, the user, can use code that person A, the author, wrote without having to rewrite that code themselves.   Ideally, the libraries are well abstracted enough the user won't need to even look at the code that author wrote, but simply use the functions and systems of the author as something that just happens.

here is the code that enabled us to run one motor:


This code enables us to run both motors simultaneously at the same (theoretical) speed.

/// Include the Bricktronics Motor library and helper libraries

#include <Encoder.h>
#include <PID_v1.h>
#include <BricktronicsMotor.h>


// Select the motor port (MOTOR_1 or MOTOR_2) in the constructor below.
//
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <BricktronicsShield.h>
BricktronicsMotor m1(BricktronicsShield::MOTOR_1);
BricktronicsMotor m2(BricktronicsShield::MOTOR_2);

void setup()
{
  // Be sure to set your serial console to 115200 baud
  Serial.begin(115200);

  BricktronicsShield::begin();

  // Initialize the motor connections
  m1.begin();
  m2.begin();
}

void loop() 
{
  Serial.println("Going forward slowly then fast.");
  m1.setFixedDrive(75);  // speed of the motor
  m2.setFixedDrive(75);  // speed of the motor
  delay(1000);
  
  m1.setFixedDrive(255);  // speed of the motor
  m2.setFixedDrive(255);  // speed of the motor
  delay(1000);

  Serial.println("Going in reverse slowly then fast.");
  m1.setFixedDrive(-75);   // speed of the motor 
  m2.setFixedDrive(-75);   // speed of the motor 
  delay(1000);
  
  m1.setFixedDrive(-255);  // increased speed of the motor in reverse
  m2.setFixedDrive(-255);  // increased speed of the motor in reverse
  delay(1000);
}

The speed goes from -256 to 255, with the sign indicating direction
The minimum speed that allows our sciborg to move on the smooth surface of a hard floor or of a hard desk is about 80

This code gets the SciBorg to make a hard turn.  A hard turn is where the sciborg has one motor full power forward, the other full power reverse.

//code for Hard_Turn 
// Include the Bricktronics Motor library and helper libraries

#include <Encoder.h>
#include <PID_v1.h>
#include <BricktronicsMotor.h>


// Select the motor port (MOTOR_1 or MOTOR_2) in the constructor below.
//
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <BricktronicsShield.h>
BricktronicsMotor m1(BricktronicsShield::MOTOR_1);
BricktronicsMotor m2(BricktronicsShield::MOTOR_2);

void setup()
{
  // Be sure to set your serial console to 115200 baud
  Serial.begin(115200);

  BricktronicsShield::begin();

  // Initialize the motor connections
  m1.begin();
  m2.begin();
}

void loop() 
{
  Serial.println("Going forward slowly then fast.");
  m1.setFixedDrive(-255);  // speed of the motor
  m2.setFixedDrive(255);  // speed of the motor
  delay(1000);

}
//end code for Hard_Turn


The previous code makes for a harsh turn where the sciborg turns almost like a tank(in place).  If one wanted to see a gentler turn like one might see in a car, we would want a softer turn.  This softer  turn is accomplished by making the speed of one motor slightly slower than the other.  The difference between the two motor speeds determines the arc of the turn where a smaller difference between the two motors means a larger turn radius.
// code for Soft_Turn // Include the Bricktronics Motor library and helper libraries

#include <Encoder.h>
#include <PID_v1.h>
#include <BricktronicsMotor.h>


// Select the motor port (MOTOR_1 or MOTOR_2) in the constructor below.
//
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <BricktronicsShield.h>
BricktronicsMotor m1(BricktronicsShield::MOTOR_1);
BricktronicsMotor m2(BricktronicsShield::MOTOR_2);

void setup()
{
  // Be sure to set your serial console to 115200 baud
  Serial.begin(115200);

  BricktronicsShield::begin();

  // Initialize the motor connections
  m1.begin();
  m2.begin();
}

void loop() 
{
  Serial.println("Going forward slowly then fast.");
  m1.setFixedDrive(80);  // speed of the motor
  m2.setFixedDrive(120);  // speed of the motor
  delay(1000);

}
//end code for Soft_Turn

The way that our Sciborg is currently set, it takes about 12.3seconds to travel 10 feet, if both motors are running at full speed.
Although theoretically, if both motors are set to the same speed in the code, the sciborg should travel in a straight path.  however we found that in fact the same speed setting in the code produced different observed speed in different motors, and this observed speed changed from observation to observation.  What this meant was that in order to make the sciborg travel straight, we would have to decrease the speed of one motor in the code so that the outputted speed of that motor can match the outputted speed of the other motor.
Thus, if we wanted for our sciborg to travel exactly 10 feet, we would have to have the sciborg run for that specified amount of time(12.3 seconds) and then stop.

//code for 10_ft_using_timer// Include the Bricktronics Motor library and helper libraries

#include <Encoder.h>
#include <PID_v1.h>
#include <BricktronicsMotor.h>


// Select the motor port (MOTOR_1 or MOTOR_2) in the constructor below.
//
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <BricktronicsShield.h>
BricktronicsMotor m1(BricktronicsShield::MOTOR_1);
BricktronicsMotor m2(BricktronicsShield::MOTOR_2);

void setup()
{
  // Be sure to set your serial console to 115200 baud
  Serial.begin(115200);

  BricktronicsShield::begin();

  // Initialize the motor connections
  m1.begin();
  m2.begin();
}

void loop() 
{
  Serial.println("Going forward slowly then fast.");
  m1.setFixedDrive(255);  // speed of the motor
  m2.setFixedDrive(245);  // speed of the motor
  delay(12300);
  stop();
}

void stop()
{
  while (true){
    m2.setFixedDrive(0);  // speed of the motor
    m1.setFixedDrive(0);  // speed of the motor
  }
}

//

The Sciborg didn't really stop at 10 feet.  In fact, it stopped 8 inches short.  It is difficult to get the sciborg to stop perfectly at 10 feet because the outputted speed is different with each run and so the distance traveled in 12.3 seconds is different for each run

No comments:

Post a Comment