Top Banner
1 AndyMark Arduino Tutorial Tank Drive June 2014 Required: Required Software List: - Kit Arduino IDE - Robot Power Cable Kit (am-0975) RobotOpen Arduino Shield Software - Battery Base Package (am-0477) RobotOpen Driver Station - D-Link DAP-1522 Radio (am-0839) - Logitech Gamepad F310 (am-2064) - MK ES17-12, 12 Volt, 17 Amp-Hour Sealed Lead Acid Battery (half of am-0844) - Battery Cable (am-0009) - Ethernet Arduino and Peasy Breakout Board Combo (am-2734) - Or an Arduino with a Breadboard and 13 Jumpers - Programmer Cable (am-2416) - 6 - 30 Amp Snap Action Breakers (am-0290) - Ethernet Cable (am-0499) - Assembled Robot - Windows XP OR Newer
17

AndyMark Arduino Tutorial

Jan 02, 2017

Download

Documents

trinhtu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: AndyMark Arduino Tutorial

1

AndyMark Arduino Tutorial

Tank Drive

June 2014 Required: Required Software List: - Kit Arduino IDE

- Robot Power Cable Kit (am-0975) RobotOpen Arduino Shield Software

- Battery Base Package (am-0477) RobotOpen Driver Station

- D-Link DAP-1522 Radio (am-0839)

- Logitech Gamepad F310 (am-2064)

- MK ES17-12, 12 Volt, 17 Amp-Hour Sealed Lead Acid Battery (half of am-0844)

- Battery Cable (am-0009)

- Ethernet Arduino and Peasy Breakout Board Combo (am-2734)

- Or an Arduino with a Breadboard and 13 Jumpers

- Programmer Cable (am-2416)

- 6 - 30 Amp Snap Action Breakers (am-0290)

- Ethernet Cable (am-0499)

- Assembled Robot

- Windows XP OR Newer

Page 2: AndyMark Arduino Tutorial

2

Table of Contents Section Page # Setting Up the Software 3 Wiring the Arduino 4 - 6 Programing the Robot 7 - 10 Configuring the Driver Station 11 Connecting the Robot and Driver Station 12 Troubleshooting 13 - 15 Appendix A: PWM 16 - 17 Appendix B: The Driver Station 18 Appendix C: Motor Bias 19 - 2

Page 3: AndyMark Arduino Tutorial

3

Setting Up the Software Arduino IDE : Once you have downloaded Arduino IDE

● Double Click on “Arduino” ○ You may need to right-click and select Run as Administrator

● Install to a known location. Setting up the RobotOpen Software

Once you have downloaded RobotOpen Arduino Shield Software:

● Open the RobotOpen-Shield-Library-master zip file

● Find and right-click on the RobotOpen file folder

● Select “copy”

● Open the Arduino folder

○ The default location for this file is in your Program Files

○ My filepath was:

C:\Program Files (x86)\Arduino

● Paste the RobotOpen folder into the Libraries folder

Wiring the Arduino

Page 4: AndyMark Arduino Tutorial

4

Place your Arduino on a desk so it doesn’t short out. FTDI Port (Programming Port)

● Plug in your USB-to-FTDI Cable (am-2416) into an open USB Port on your computer ● Rotate the Arduino so the FTDI Port is facing your computer ● Plug the other end of the cable into your Arduino’s FTDI Port

Ethernet Port

● Plug one end of your ethernet cable (am-0499) into the ethernet port on your computer ● Plug the other end of your ethernet cable into the Arduino’s Ethernet Port

Note: If your computer does not have a ethernet port you will need to read the tutorial on the Arduino Wireless. Note: You now will need to place your robot on blocks, so that it’s wheel are off the ground.

● Place your Arduino on the robot while keeping everything plugged into the computer

● The Peasy Board (am-2734) plugs directly into the Arduino like so:

Once plugged into the Peasy Board: ● Plug PWM wires from the speed controller into the peasy board using the following

configuration:

Page 5: AndyMark Arduino Tutorial

5

○ Front Left = Digital Pin 9

○ Rear Left = Digital Pin 3

○ Front Right = Digital Pin 6

○ Rear Left = Digital Pin 5

● The yellow wire should insert into the headers that are labeled “sig.”

Note: Make sure all of your speed controllers are off.

Alternatively : You can use a breadboard. This will be less convenient than the Peasy Board,

but it will still work. To match our example programs, you may want to wire the Arduino to the

breadboard this way:

Page 6: AndyMark Arduino Tutorial

6

Programming the Arduino The Program

● Open up the Arduino IDE ○ You may need to right-click and select “run as administrator”

● At the menu bar select Tools > Board > Arduino Ethernet ● At the menu bar select Tools > Serial Port > Select Communication Port

○ To find communication (com) port, unplug your usb cord, re-iterate the above steps, find which COM is missing. The missing COM is the port to select.

● Insert this code below into your sketch. CODE: #include  <SPI.h> #include  <Ethernet.h> #include  <Servo.h> #include  <EEPROM.h> #include  <RobotOpen.h> #define  NEUTRAL  127 #define  FORWARD  255 #define  REVERSE  0   #define  FORWARD_MS  2000 #define  REVERSE_MS  1000 #define  NEUTRAL_MS  1500 //Declaring  which  joystick  we  are  using ROJoystick  usb1(1); //Declaring  that  we  have  two  Servos Servo  leftRear; Servo  leftFront; Servo  rightRear; Servo  rightFront; //Delay  time  for  calibration int  d  =  250; //IP  Address  of  the  Arduino IPAddress  ip  (  ,  ,  ,  );          //<===  Make  your  own  IP  Address void  setup()  {        //Identify  which  digital  pins  the  servos  are  using    rightRear.attach(5);    rightFront.attach(6);    leftRear.attach(3);

Page 7: AndyMark Arduino Tutorial

7

   leftFront.attach(9);        //This  function  makes  sure  the  speed  controller  and  the  Arduino  understand  each  other.      calibrate();        //Communicates  with  the  RobotOpen  Driver  Station    RobotOpen.setIP(ip);        //Begins  the  RobotOpen  tasks    RobotOpen.begin(&enabled,  &disabled,  &timedtasks); }   void  enabled()  {    //Records  the  value  of  the  left  analog  stick  on  our  selected  joystick.  //The  motors  move  slightly  slower  when  moving  in  reverse  than  when  moving  forward.  The  bias()  function  takes  care  of  this  problem.  //Adjustmants  may  be  needed  depending  on  your  setup.      int  valL  =  bias(usb1.leftY());  int  valR  =  bias(usb1.rightY());  //Maps  valL  and  valR  on  a  scale  of  1000  -­‐  2000.    //The  first  two  numbers  are  your  high  and  low  for  the  former  scale  and  the  last  two  are  the  high  and  low  for  the  new  scale.    int  spdL  =  map  (valL,  0,  255,  1000,  2000);  int  spdR  =  map  (valR,  0,  255,  1000,  2000);    //The  value  becomes  our  wheel  speeds    leftFront.writeMicroseconds(spdL);  leftRear.writeMicroseconds(spdL);  rightFront.writeMicroseconds(spdR);  rightRear.writeMicroseconds(spdR); } void  disabled()  {        //  safety  code        leftRear.writeMicroseconds(NEUTRAL_MS);        leftFront.writeMicroseconds(NEUTRAL_MS);        rightRear.writeMicroseconds(NEUTRAL_MS);        rightFront.writeMicroseconds(NEUTRAL_MS); } void  timedtasks()  {    //Publishing  the  values  of  the  analog  sticks  to  the  RobotOpenDS    RODashboard.publish("usb1.leftY()",  usb1.leftY());    RODashboard.publish("usb1.leftX()",  usb1.leftX());

Page 8: AndyMark Arduino Tutorial

8

   RODashboard.publish("usb1.rightY()",  usb1.rightY());    RODashboard.publish("usb1.rightX()",  usb1.rightX());    RODashboard.publish("Uptime  Seconds",  ROStatus.uptimeSeconds()); } void  calibrate()  {    //Calibrating  the  Contorllers.  See  Appendix  A  in  the  Arduino  Tutorial  for  more  info    leftRear.writeMicroseconds(NEUTRAL_MS);    leftFront.writeMicroseconds(NEUTRAL_MS);    rightRear.writeMicroseconds(NEUTRAL_MS);    rightFront.writeMicroseconds(NEUTRAL_MS);    delay(d);    leftRear.writeMicroseconds(FOWARD_MS);    leftFront.writeMicroseconds(FOWARD_MS);    rightRear.writeMicroseconds(FOWARD_MS);      rightFront.writeMicroseconds(FOWARD_MS);      delay(d);    leftRear.writeMicroseconds(REVERSE_MS);    leftFront.writeMicroseconds(REVERSE_MS);    rightRear.writeMicroseconds(REVERSE_MS);    rightFront.writeMicroseconds(REVERSE_MS);    delay(d);    leftRear.writeMicroseconds(NEUTRAL_MS);    leftFront.writeMicroseconds(NEUTRAL_MS);    rightRear.writeMicroseconds(NEUTRAL_MS);    rightFront.writeMicroseconds(NEUTRAL_MS); } //We  declare  that  we  have  a  function  that  is  going  to  take  an  integer int  bias  (int  motor){        //The  motors  move  around  15%  slower  when  in  reverse.    float  reverseBias  =  .85;        //If  the  motor  is  moving  forward  we  want  to  slow  it  down  by  15%    if(motor>NEUTRAL){        //Maps  a  new  scale  that  will  match  the  speed  of  the  motors  running  in  reverse        //SEE  APPENDIX  C  IN  THE  TUTORIAL  FOR  MORE  INFO        return  map(motor,  REVERSE  ,  FORWARD,  (FORWARD*(1.0-­‐reverseBias)),  (FORWARD*reverseBias));        }else  {        //Maps  a  new  scale  that  will  match  the  speed  of  the  motors  running  in  reverse        return  motor;    } }

Page 9: AndyMark Arduino Tutorial

9

void  loop()  {  //Continually  communicates  with  the  Robot  Open  Driver  Station  RobotOpen.syncDS(); }

● Upload your code by pressing the arrow at the top of your sketch ○ If an error occurs, check the Arduino Forums for help ○ If no errors appear, proceed

● Unplug your Arduino from the computer Note: You will need to change the IP Address to fit your computer. For help with this see the Troubleshooting section. Configuring the Driver Station The Driver Station

● Open up the RobotOpenDS (https://chrome.google.com/webstore/detail/robotopen-

ds/bhpgaibglpbnkhlkmpjhfmlpfbolgkme?hl=en-US)

● Go to: Setup > Robot > Type in the IP Address you made for the Arduino. ● The number for Port should be “22211” ● Plug in your Logitech Gamepad F310 (am-2046) ● Press any button on the Controller. The Joy #1 tab at the top of your Driver Station

should be highlighted green. If not see the Troubleshooting section. Connecting the Robot and Driver Station Note: Check that your ethernet cable (am-0499) is plugged into your computer or router (am-0839) . Calibrating the Speed Controllers

● Hold the Arduino’s reset button ● While holding the button, switch all of the speed controllers on ● Let go of the reset button

Using the Controller

● Open up the RobotOpen Driver Station ● Make sure your controller (am-2064) is plugged into the computer ● Click the connect button in the top right corner of the Driver Station ● Click the enable robot button in the top right corner of the Driver Station ● Move your analog sticks. Your left analog corresponding to your left wheels & your right

analog corresponding to your right wheels

Page 10: AndyMark Arduino Tutorial

10

Troubleshooting My program won’t load onto my Arduino. |OR| I keep getting the error message: avrdude:

stk500_getsync(): not in sync: resp=0x00 ?

Check that you have the right com port. To do this:

● Unplug your FTDI cable from the computer

● Open the serial port menu

● Find which com port is missing

● Plug your FTDI cable back into the computer

● Re-open the serial port menu

● Select the new com port

Check that your FTDI cable is correctly plugged into your Arduino:

● Completely unplug the FTDI cable from your Arduino

● Make sure that the golden crimps are facing down

● Fit the FTDI cable into the Arduino’s FTDI Port

● Push the cable up until the silver pins are covered.

○ Note: It may feel like the pins will break, but push the cable all the way. This

ensures that you will achieve a good connection.

Joy #1 on the Driver Station does not highlight when the controller is plugged in?

Make sure the Driver Station window is open and Selected. If it still does not work, move to the

next step.

Re-connect the controller:

● Close the Driver Station App

● Unplug your controller from the computer

● Re-open the Driver Station App

● Plug your controller into the computer

● Press any button on your controller

● Repeat the process once more if it does not work the first time.

Page 11: AndyMark Arduino Tutorial

11

The Driver Station won’t connect to the Arduino? |OR| The Driver Station only connects

to the Arduino for a short time?

Check to see if your Arduino IP Address is correct:

● Make sure you the code from Programming the Arduino section is loaded on to the

Arduino, your Arduino is plugged in to your network and you have changed the IP

Address to fit the subnet your computer is on. (Same first three numbers)

● Open start menu > search for cmd > select cmd.exe > type in:

ping IPADDRESS -n 15

>Press Enter

○ If it says Destination host unreachable you need to either:

■ Change your IP Address

■ Check the way you setup your software

To find a correct IP Address:

● Open start menu > search for cmd > select cmd.exe > type in:

ipconfig

● In the line that says "IPv4 Address" there should be a number ● You want your IP Address for your Arduino to have the same first three numbers

of your IPv4 Address but a different last number ● For example:

Your IPv4 - 192.168.1.15 Arduino IP - 192.168.1.25 or 192.168.1.XX

Re-install the software:

● Close all Arduino sketches

● Open up task manager and make sure no Arduino programs are running

○ The application will be called Arduino 1.0.5-r2

● Open up the start menu > Control Panel > Programs and Features > Right-Click on

Arduino > Select uninstall

● Open up your Downloads folder and delete your all of the downloaded files for Arduino

and RobotOpen EXCEPT for your Driver Station

● Once everything is uninstalled and deleted, re-download the Arduino IDE & the

RobotOpen Arduino Shield Software

● Open up your Downloads folder > Open RobotOpen-Shield-Library-master zip file >

Open RobotOpen-Shield-Library-master file > Open libraries file > Right-Click on the

RobotOpen file > Select Copy

Page 12: AndyMark Arduino Tutorial

12

● Open up the start menu > Select Computer > Open Local Disk (C:) > Open up either

Program Files (x64) or Program Files (x86) > Open Arduino > Open libraries > Right-

Click in an open area > Select paste

○ If you selected to install it to a custom area, you will go there instead of Program

Files.

● Follow the steps from the Programming the Arduino section until the end

Page 13: AndyMark Arduino Tutorial

13

Appendix A: PWM A PWM is a message that allows your Arduino to talk to your speed controller which then controls your motor. This "message" is, in short, pulses of electricity every few milliseconds. Let's look at a theoretical analogy. Assume there is an army back before they had radio or any other type of speedy communication, and the army is split into two different sections. These two sections need a way to communicate but they are too far away for messengers, so they decide to use a flag. To make sure the two groups understand each other, they decide that every twenty minutes they will raise the flag with a message. The flag is raised for a minimum of one minute so the other section knows a message is coming. If the flag is lowered right after that first minute, that means retreat. If the flag is kept up for thirty seconds after that minute, that means don't move. If the flag is kept up for one minute after the first minute, that means to go forward. In this analogy, the period of the message is twenty minutes. After the message, the flag will go down until the next period starts. If we were to graph this, it would look something like this:

How does this relate to your Arduino and Motors? Well think of your Arduino as the side sending the message and your speed controller as the other group receiving the message. Once your speed controller reads the message it controls the motor to do whatever the message said. The only difference between a PWM and the flag raising method is that a PWM uses electricity instead of a flag, and instead of minutes it uses milliseconds.

Page 14: AndyMark Arduino Tutorial

14

This is a variation to some of the code from above: //Adds  Servo  library  to  the  compiler  list #include  <Servo.h> //Create  a  motor  object  to  talk  to Servo  myservo;         int  d  =  233;         void  setup()  {    //Tell  the  motor  object  to  connect  to    myservo.attach(9);          myservo.writeMicroseconds(1500);    delay(d);    myservo.writeMicroseconds(1000);      delay(d);    myservo.writeMicroseconds(2000);    delay(d);    myservo.writeMicroseconds(1500); }   void  loop()  {}   Most of this is straightforward except for the myservo.writeMicroseconds()  function. The myservo is the specific Servo Object we want to modify. The writeMicroseconds() is the specific task we want to to with this object. The first part “myservo” just says that we are doing something with the servo that we call “myservo.” The second part writeMicroseconds()  is doing all the real work. writeMicroseconds()  takes the number we put in and uses that number to send the PWM. The number is the amount of time the pulse should be sent. You may have noticed the number we put in is in microseconds and not milliseconds like a PWM, but it doesn’t make a difference. Now that you know all of this you should know:  myservo.writeMicroseconds(1500);  says the motor is neutral.  myservo.writeMicroseconds(1000);  says the motor is full reverse.  myservo.writeMicroseconds(2000); says the motor is full forward.  myservo.writeMicroseconds(1500); says the motor is back to neutral. The delay function just stops the motor from changing positions to quickly. Note : DO NOT change the value of "d" this number should be constant. The purpose of the above code is to perform a calibration, a type of "handshake." The speed controller does not really know the exact scale that your Arduino would send out pulses so it must find out the midpoint then the low and high point. It must also have enough time to register these values, hence the delay. The set delay time of 233 is the minimum amount of time the controller needs to register the values and that is why the number should not be changed.

Page 15: AndyMark Arduino Tutorial

15

The Driver Station

Page 16: AndyMark Arduino Tutorial

16

Appendix C: Motor Bias What is motor bias? Look at the picture below: The arrow is the way the wheel would move if in forward or reverse. Yet, what happens to a motor on the opposite side? Motors on opposite sides are the same kind of motor; one is just flipped 180 degrees. So, if we flip the above motor we get this:

If you want both sides of your robot to run forward, the motors will be moving in opposite directions. So, to move your robot forward, the left side runs forward(100%) and the right side runs in reverse (-100%). The only problem is that when a motor runs in reverse (-100%), it does not always move as fast as a motor running forward. This is called a motor bias. So, to counter this you must slow down the motors moving forward to match the motors that are moving in reverse. To help you with this, we have created the bias() function in the program to help you. EXAMPLE: //We  declare  that  we  have  a  function  that  is  going  to  take  an  integer int  bias_ms  (int  motor){    //The  motors  move  around  15%  slower  when  in  reverse.    float  reverseBias  =  .85;        //If  the  motor  is  moving  forward  we  want  to  slow  it  down  by  15%    if(motor>NEUTRAL){        //Maps  a  new  scale  that  will  match  the  speed  of  the  motors  running  in  reverse        return  map(motor,  REVERSE  ,  FORWARD,  (FORWARD_MS*(1.0-­‐reverseBias)+1000),                      (FORWARD_MS*reverseBias));    }else  {        //Maps  a  new  scale  that  will  match  the  speed  of  the  motors  running  in  reverse

Page 17: AndyMark Arduino Tutorial

17

       return  map(motor,  REVERSE,  FORWARD,  REVERSE_MS,  FORWARD_MS);    } }