how to build a robot- robot assembly

Upload: adalberto-sharpley

Post on 03-Jun-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 How to Build a Robot- Robot Assembly

    1/25

    How to Build a Robot: Robot Assembly

    How to Build Robot

    Lets look athow to build robot,first I gathered all the pieces and just kind of loosely placed

    them on the chassis to get a rough idea of how they would fit. I placed the four AA batteries in

    the middle because they are the heaviest element, we want the heaviest thing over the center of

    gravity, and thus the robot will be stable and not want to tip over. Learn powerful info on how to

    build robot.

    The receiver for the IR remote has also been connected. There are 3 wires that need to be

    connected, PWR, which is connected to 5V on the Arduino, GND which is connected to GND onthe Arduino, and IN, which is connected to port 11 on theArduino.

    We wont need too much of the breadboard just enough to hold the ultrasonic sensor so we can

    later put the 9 V battery on top of it. The breadboard that I ordered came with some sticky tape

    http://2manyprojects.net/assemblyhttp://2manyprojects.net/assemblyhttp://2manyprojects.net/assemblyhttp://2manyprojects.net/assemblyhttp://2manyprojects.net/assemblyhttp://2manyprojects.net/assemblyhttp://2manyprojects.net/assemblyhttp://2manyprojects.net/assembly
  • 8/11/2019 How to Build a Robot- Robot Assembly

    2/25

    on the back the which is actually quite strong and holds very well even against wood, which is

    not usually a very good material for adhering to with tape. Peel the paper off to reveal the sticky

    part.

    Then place the breadboard as close to the front of the robot as you can.

  • 8/11/2019 How to Build a Robot- Robot Assembly

    3/25

    This is the box of Velcro that I use. This Velcro is nice because it comes in a long strip and you

    actually get quite a lot for the money compared with buying little packages. You can cut

    this Velcro with a pair of scissors, or a sharp utility knife.

  • 8/11/2019 How to Build a Robot- Robot Assembly

    4/25

  • 8/11/2019 How to Build a Robot- Robot Assembly

    5/25

    The backside of the Arduino Uno has all the solder connections exposed its possible that some

    of these can get shorted if you laid it on top of something that was metal, its mostly plastic on

    top of our gearbox, but just to be sure I took the small piece of foam that came with the Arduino

    and mounted it on top of the foam with some little screws, but you can use just about anything

    else like tape or some hot melt glue if you dont have screws. I then put the Arduino with the

    motor shield attached on top of the motor gearbox and fastened this down with some Velcro.

    Here is a view from the back of the robot you can see the small strip of Velcro and foam that the

    Arduino and motor shield are mounted to.

    I went ahead and placed the ultrasonic sensor on the breadboard even though we havent yet

    got to programming it. We just need to get a sense of how much space it takes up, we want to

    leave the first two or three rows of the breadboard accessible after the ultrasonic sensor for

    wires to plug-in to, there is a total of 3 wires that will need to be connected to the ultrasonic

    sensor when we get to it.

    I made sure to cut the wires connected to the motor a little bit long so that I can trim them here. I

    cut them close so theres not a lot of slack wire laying around.

  • 8/11/2019 How to Build a Robot- Robot Assembly

    6/25

  • 8/11/2019 How to Build a Robot- Robot Assembly

    7/25

  • 8/11/2019 How to Build a Robot- Robot Assembly

    8/25

    I also used Velcro to attach the battery holder for the four AA batteries. I put on a piece of

    Velcro which was a little too wide then used a hobby knife to trim it. Velcro is particularly useful

    here because when we want to remove the battery pack we can just remove it and not have to

    worry about scraping off tape or glue.

    http://2manyprojects.net/wp-content/uploads/robot/assembly_15_16_17.jpghttp://2manyprojects.net/wp-content/uploads/robot/assembly_15_16_17.jpg
  • 8/11/2019 How to Build a Robot- Robot Assembly

    9/25

  • 8/11/2019 How to Build a Robot- Robot Assembly

    10/25

    Lastly we need to figure out a way to attach the infrared receiver, there was not a real obvious

    way to do this so I end up using a small piece of Velcro and just attaching it to the back one of

    the plastic connectors on the motor shield.

    Also for the infrared receiver, the part that does the receiving is only on one side of the device,

    but if the robot is making a turn, this could end up putting the backside of the receiver towards

    us and it would not receive the signal from the remote. To fix this I took the receiver and bent itso that the portion that actually receives the IR signal is pointed up. Since Ill mostly be standing

    when Im controlling the robot and Im pointing the remote downward, this turned out to work

    quite well, and had a surprisingly good range as long as you dont go behind an obj ect which

    would block the infrared signal.

  • 8/11/2019 How to Build a Robot- Robot Assembly

    11/25

    Lastly I took the wires and bent them around in a manner so they wouldnt get hung up on the

    tracks or snagged on something else. In this picture, everything is connected except the

    ultrasonic sensor.

  • 8/11/2019 How to Build a Robot- Robot Assembly

    12/25

    With the robot assembled, we are ready to write a bit more sophisticated code to actually be

    able to control the robot using the remote. We will get it working with the remote, then add the

    ultrasonic sensor as an enhancement.

    First lets write a short program just to establish communication with the robot. This program will

    start one track in motion when a button on the remote is pressed.

    #include

    // variables for IR remote

    intRECV_PIN = 4;

    IRrecvirrecv(RECV_PIN);

    decode_resultsresults;

    // hex code assigned to a button

  • 8/11/2019 How to Build a Robot- Robot Assembly

    13/25

  • 8/11/2019 How to Build a Robot- Robot Assembly

    14/25

  • 8/11/2019 How to Build a Robot- Robot Assembly

    15/25

    constintbrakePinA = 9;

    constintbrakePinB = 8;

    // hex code assigned to a button pressconstlongforward_button = 0xFD807F; // uses 'VOL+' button

    constlongstop_button = 0xFDA05F; // uses 'Play' button

    constlongreverse_button = 0xFD906F; // uses 'VOL-' button

    constlongleft_button = 0xFD20DF; // uses 'prev' button

    constlongright_button = 0xFD609F; // uses 'next' button

    constlongrepeat = 0xFFFFFFFF;

    // global vars

    constintAFORWARD = LOW;

    constintBFORWARD = HIGH;

    IRrecvirrecv(RECV_PIN);

    decode_resultsresults;

    longcurrent_command = 0;

    longprevious_command = 0;

    // speed is used globally for motion and steering

    constintMAX_SPEED = 255; // use speed 255

    // main class to represent our robot

    classRobot

    {

    private:

    int_lts; // left track speed

    int_rts; // right track speed

    int_ltd; // left track direction

    int_rtd; // right track direction

    voiddrive(intlts, intrts, intltd, intrtd);

    public:

    Robot(intlts, intrts, intltd, intrtd);

    voidStop(); // engage both brakes

  • 8/11/2019 How to Build a Robot- Robot Assembly

    16/25

    voidDriveForward(); // both forward

    voidDriveBackward(); // both backward

    voidTurnLeft(); // opposite directions

    voidTurnRight(); // opposite directions};

    // create a new robot

    Robot tracky(0, 0, AFORWARD, BFORWARD); // our robot is named tracky

    voidsetup()

    {

    Serial.begin(9600);

    irrecv.enableIRIn(); // Start the receiver

    // setup channels A

    pinMode(motorPinA, OUTPUT); //Initiates Motor Channel A pin

    pinMode(brakePinA, OUTPUT); //Initiates Brake Channel A pin

    // setup Channel B

    pinMode(motorPinB, OUTPUT); //Initiates Motor Channel B pin

    pinMode(brakePinB, OUTPUT); //Initiates Brake Channel B pin

    digitalWrite(brakePinA, LOW); // disengage the Brake for channel A

    digitalWrite(brakePinB, LOW); // disengage the brake for channel B

    }

    voidloop()

    {

    if(irrecv.decode(&results)) { // results is an instance of a

    decode_results class, decode() returns an int

    Serial.println(results.value, HEX);

    if( results.value == repeat )

    {

    current_command = previous_command;

  • 8/11/2019 How to Build a Robot- Robot Assembly

    17/25

    }

    else

    {

    current_command = results.value;previous_command = current_command;

    }

    // channel A is the left motor as viewed from a "driver"

    viewpoint"

    // channel B is the right motor as viewed from a "driver"

    viewpoint"

    switch(current_command)

    {

    //Serial.println("in switch statement");

    caseforward_button:

    tracky.DriveForward();

    break;

    casestop_button:

    tracky.Stop();

    break;

    casereverse_button:tracky.DriveBackward();

    break;

    caseleft_button:

    tracky.TurnLeft();

    break;

    caseright_button:

    tracky.TurnRight();

    break;

    }

    irrecv.resume(); // Receive the next value

    }

    }

    Robot::Robot(intlts, intrts, intltd, intrtd)

  • 8/11/2019 How to Build a Robot- Robot Assembly

    18/25

    {

    _lts = lts;

    _rts = rts;

    _ltd = ltd;_rtd = rtd;

    }

    voidRobot::drive(intlts, intrts, intltd, intrtd)

    {

    //Serial.println(lts);

    digitalWrite(directionPinA, ltd); // Establishes direction of

    Channel A

    analogWrite(motorPinA, lts); // Spins the motor on Channel A

    //Serial.println(rts);

    digitalWrite(directionPinB, rtd); // Establishes direction of

    Channel B

    analogWrite(motorPinB, rts); // Spins the motor on Channel B at

    full speed

    }

    voidRobot::Stop()

    {

    //Serial.println("Stop");

    _lts = 0;

    _rts = 0;

    drive(_lts, _rts, _ltd, _rtd);

    }

    voidRobot::DriveForward()

    {

    //Serial.println("Forward");

    _lts = MAX_SPEED;

    _rts = MAX_SPEED;

    _ltd = AFORWARD;

  • 8/11/2019 How to Build a Robot- Robot Assembly

    19/25

    _rtd = BFORWARD;

    drive(_lts, _rts, _ltd, _rtd);

    }

    voidRobot::DriveBackward()

    {

    //Serial.println("Backwards");

    _lts = MAX_SPEED;

    _rts = MAX_SPEED;

    _ltd = !AFORWARD;

    _rtd = !BFORWARD;

    drive(_lts, _rts, _ltd, _rtd);

    }

    voidRobot::TurnLeft()

    {

    //Serial.println("Turn Left");

    Serial.println(_lts);

    Serial.println(_rts);

    // the robot does not have to be moving to turn, we will spin in

    placedrive(MAX_SPEED, MAX_SPEED, !AFORWARD, BFORWARD); // drive the left

    track in the opposite direction

    delay(500);

    drive(_lts, _rts, _ltd, _rtd); // resume whatever we were doing when

    this function was called

    }

    voidRobot::TurnRight()

    {

    //Serial.println("Turn Right");

    // the robot does not have to be moving to turn, we will spin in

    place

    drive(MAX_SPEED, MAX_SPEED, AFORWARD, !BFORWARD); // drive the left

    track in the opposite direction

  • 8/11/2019 How to Build a Robot- Robot Assembly

    20/25

    delay(500);

    drive(_lts, _rts, _ltd, _rtd); // resume whatever we were doing when

    this function was called

    }

    Lets take a closer look at this program.

    The only library that we need to include is the remote control library like we did in the IR remote

    section.

    #include

    How to Build Robot if youre new to Programming

    If youre new to programming one thing youll hear about before long is being advised against

    using global variables, this is usually good advice and by using a class here were actually

    avoiding the use of a lot of global variables and the problems that can come with them, ( i.e.

    making variables like _lts global instead of a being a member of a class) that said there are still

    very good uses for global variables. Things like port assignments and hex codes that are

    associated with a remote control button should be declared as global variables as they wont

    change throughout execution of the program, note the hex codes that we use are not int, but

    rather long, storing these codes actually requires a data type that is larger than a normal integer

    type. We assign variables to all the needed Arduino ports, and then the hex codes that

    represent remote control button presses.

    // pin assignments

    constintRECV_PIN = 4;

    constintdirectionPinA = 12;

    constintdirectionPinB = 13;

    constintmotorPinA = 3;

    constintmotorPinB = 11;

  • 8/11/2019 How to Build a Robot- Robot Assembly

    21/25

    constintbrakePinA = 9;

    constintbrakePinB = 8;

    // hex code assigned to a button pressconstlongforward_button = 0xFF629D; // uses 'VOL+' button

    constlongstop_button = 0xFF02FD; // uses 'Play' button

    constlongreverse_button = 0xFFA857; // uses 'Vol-' button

    constlongleft_button = 0xFF22DD; // uses 'prev' button

    constlongright_button = 0xFFC23D; // uses 'next' button

    constlongrepeat = 0xFFFFFFFF;

    Youll notice most of the code that handles the infrared remote portion of the program is

    identical to what we did earlier, just pasted in here. Theres also a global variable to hold

    MAX_SPEED which is set to 255, Im using the highest gear ratio, and with this robots overall

    speed is not very fast. So I have MAX_SPEED set to be 255, and then provide the ability to turn

    simply by changing direction of the tracks.

    constintMAX_SPEED = 255; // use speed 255

    Remember back we were hooking up the wires to the motors and I said that how you hook up

    the wires was important, but we can tweak it in software? Thats being done here with the

    variables AFORWARD and BFORWARD. If one of your tracks is spinning in the wrong

    direction, you only need to change the value saved for AFORWARD or BFORWARD.

    constintAFORWARD = LOW;

    constintBFORWARD = HIGH;

    I decided not to use the brakes with this program, I think it keeps things simpler. In the the

    setup() function we will disengage the brakes as soon as the program starts, then well just use

    the speed to control when the robot stops by setting it to zero.

    digitalWrite(brakePinA, LOW); // disengage the Brake for channel A

  • 8/11/2019 How to Build a Robot- Robot Assembly

    22/25

    digitalWrite(brakePinB, LOW); // disengage the brake for channel B

    }

    Also in the setup() function, we need to set up all of our various pins as inputs or outputs,

    enable the infrared remote, and setup the Serial Monitor.

    Serial.begin(9600);

    irrecv.enableIRIn(); // Start the receiver

    // setup channels A

    pinMode(motorPinA, OUTPUT); //Initiates Motor Channel A pin

    pinMode(brakePinA, OUTPUT); //Initiates Brake Channel A pin

    // setup Channel B

    pinMode(motorPinB, OUTPUT); //Initiates Motor Channel B pin

    There are five public functionsstop(), driveForward(), driveBackward(), turnLeft(),

    and turnRight(). These are the primary functions that we will call from the main loop, to control

    the robot.

    public:

    Robot(intlts, intrts, intltd, intrtd);

    voidStop(); // engage both brakes

    voidDriveForward(); // both forward

    voidDriveBackward(); // both backward

    voidTurnLeft(); // opposite directions

    voidTurnRight(); // opposite directions

    The robot has four private variables which represent the left track speed, right track speed, left

    track direction, right track directionand one private function drive(). It is these four variables that

  • 8/11/2019 How to Build a Robot- Robot Assembly

    23/25

    maintain the Robots state. Since they are private, you can only use them from within functions

    that belong to the Robot class (e.g. Robot::DriveForward() ).

    private:

    int_lts; // left track speed

    int_rts; // right track speed

    int_ltd; // left track direction

    int_rtd; // right track direction

    voiddrive(intlts, intrts, intltd, intrtd);

    drive() is a low-level function that is called by the other public functions. It actually sets the track

    directions, and commands the motors to turn.

    The other public function is the constructor, which initializes the four private variables. We

    create a new robot object just before calling the setup() function, initializing both speeds to zero,

    this is done so tracky doesnt take off as soon as we power him up.

    // create a new robot

    Robot tracky(0, 0, AFORWARD, BFORWARD); // our robot is named tracky

    In the loop function we are reusing some of the code from the IR remote section. There are a

    couple new variables however, current command and previous command, these are used

    because our remote puts out a repeat code of FFFFFF instead of actually repeating the hex

    code for the button we pressed. What we need to do is save the current command to the

    previous command variable every time a button is pressed. Then we need to check if the repeat

    code is given, and if so, use the previous command.

    voidloop()

    {

    if(irrecv.decode(&results)) { // results is an instance of a

    decode_results class, decode() returns an int

    Serial.println(results.value, HEX);

  • 8/11/2019 How to Build a Robot- Robot Assembly

    24/25

    if( results.value == repeat )

    {

    current_command = previous_command;}

    else

    {

    current_command = results.value;

    previous_command = current_command;

    }

    After setting the value for current command correctly we have a switch statement that checks

    which button has been pressed, and then calls the function that corresponds to that button.

    switch(current_command)

    {

    //Serial.println("in switch statement");

    caseforward_button:

    tracky.DriveForward();

    break;

    casestop_button:

    tracky.Stop();

    break;

    casereverse_button:

    tracky.DriveBackward();

    break;

    caseleft_button:

    tracky.TurnLeft();

    break;caseright_button:

    tracky.TurnRight();

    break;

    }

  • 8/11/2019 How to Build a Robot- Robot Assembly

    25/25