the sumobot handbook_final

25
The Engineer’s Guide to SumoBots Don’t Panic! The SumoBot Executive 2014 January 1, 2016 1

Upload: sandon-hamnett

Post on 21-Jan-2018

181 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The SumoBot Handbook_final

The Engineer’s Guide to SumoBots

Don’t Panic!

The SumoBot Executive 2014

January 1, 2016

1

Page 2: The SumoBot Handbook_final

Contents

1 Introduction 4

2 What is a Sumobot? 4

3 What is a Robot? 4

4 Designing Your Robot 5

5 Mechanical Design 55.1 Rough Sketches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55.2 CAD implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65.3 The CAD Checklist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

6 Electrical Design 66.1 Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66.2 Actuators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

6.2.1 DC Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86.2.2 Servomotors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86.2.3 A note about Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

6.3 Power Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96.3.1 The Motor Power System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96.3.2 H-Bridges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

6.4 The Logic Power System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

7 Software Design 107.1 Finite State Machines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107.2 Coding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

7.2.1 Declare variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117.2.2 Set up your ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117.2.3 The main program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117.2.4 Writing functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

7.3 Pulse Width Modulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137.3.1 How does it work? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

8 Conclusion 14

A Arduino Anatomy 15

B The CAD Checklist 17

C Hooking Up Sensors and H-Bridges 18

D Sample SumoBot Code 19

2

Page 3: The SumoBot Handbook_final

E PMWs in Arduinos 24E.1 Analog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24E.2 PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

3

Page 4: The SumoBot Handbook_final

1 Introduction

Welcome to the McMaster Sumobot competition! This handbook will give you a basic understand-ing of the components and systems needed to make your first Sumobot.

2 What is a Sumobot?

A Sumobot is exactly what it sounds like: a sumo-wrestling robot. More specifically, Sumobots areautonomous robots programmed to push other Sumobots out of a ring without getting pushed outthemselves. Our competition follows the international regulations for Sumobot size and shape, asoutlined in the McMaster Sumobot Rulebook.

3 What is a Robot?

Autonomous robots aren’t so different from animals if you think about it. Robots and animals bothobserve their surroundings before deciding what to do, and they use similar tools in the process:

• Animals use sensory organs like eyes and ears to collect information from the environment.A robot’s sensory systems do the same thing.

• The brain takes information from the sensory organs and uses it to come up with an appro-priate response to the environment. Robots have processing systems for this purpose.

• Once the brain has decided what the animal should do, it sends signals to the rest of the bodythrough the nervous system. A robot’s “nervous system” is made of electrical wires.

• The animal moves around using its limbs. Robots have many types of limbs, but they allneed motors to work.

So if you want to build a functional Sumobot, you need four things: a sensory system tocollect data, a processing system to convert data into behavior, a motor output system toperform those behaviors, and a wiring system that connects them all together. First, let’s talkabout a robot’s senses. We all know how sensory systems work in animals: the eyes, ears, nose,tongue, and skin work together to collect information, just like how a robot’s sensors gather datafrom the environment. But that’s all it is: raw data. Your eyes can’t tell you what you’re lookingat; they just absorb light. Your tongue can’t tell you what you’re eating; it just absorbs chemicals.If you want to know what you’re eating — if you want to understand and interpret the informationprovided to you by your tongue — you need a brain.

A robot’s brain is called a microcontroller or microprocessor. The robot’s sensors sendinformation to the microcontroller, which then interprets that information based on a man-madesystem of logic. The man (or woman), in this case, is you. You decide how your microcontrollerconverts sensory input to motor output. You wire the robot’s sensors so the data goes where itneeds to. You write the code, you upload it to the microcontroller, and you watch your robot dothe exact opposite of what ’it should, because no one gets it right on their first try. But when youdo get it right — when your little robot finally figures out how to push other robots out of the ring— you’ll feel as proud as any parent and deservedly so.

4

Page 5: The SumoBot Handbook_final

Most amateur robot enthusiasts use the Arduino brand of microcontroller. You will use theArduino Uno. There are many different kinds of Arduino, each with its own purpose, but theArduino Uno is best for our designs. It’s usually installed in simple robots like Sumobots, whichonly use a few sensors and motors. Your Sumobot will probably be limited to DC motors,which will help it move around the ring, but depending on your design you might use other, morecomplex devices, like servomotors. That covers the basics. If you have any other questions aboutdesigning robots, choosing parts, or following Sumobot guidelines, just keep reading, and you’ll findwhat you’re looking for. Good luck!

4 Designing Your Robot

A robot’s design can be broken down into three components

• Mechanical Design

• Electrical Design

• Software Design

These components are all equally important, and they depend on each-other to function properly.If one component is faulty, the whole design suffers, and your robot will refuse to work. But if everycomponent is well thought-out and properly coordinated, everything should work perfectly. Let’sstart with mechanical design.

5 Mechanical Design

You should already be familiar with the mechanical design process, but we’ll give you some pointersjust in case. Over the course of this project, you’ll probably come up with several design ideas foryour Sumobot. These designs may be very different from one-another; they might use differentparts, differently shaped chassis, etc. Chassis and robot parts are expensive, and they can bedifficult to swap out, so make sure your design is as complete as possible before you startbuilding. Don’t commit to anything unless you’re absolutely sure your current design is the oneyou want to use. For a complete description of Sumobot size regulations, refer to the McMasterSumobot rulebook. To reiterate, the design stage is the most critical part of your project. If youaren’t absolutely sure about your current design, ask your mentors for their opinion. That’s whythey’re there.

For a complete description of Sumobot size regulations, please refer to the McMaster Sumobotrulebook.

To reiterate, the design stage is the most critical part of your project. If you’re not absolutelysure about your current design, then ask! That’s why your mentors are there.

5.1 Rough Sketches

The first thing your group should do is sit down and sketch out some ideas. You don’t need anyspecial software to brainstorm; all you need is a pen, some paper, and a good imagination. Whenyou all agree on a design, you can safely move to the next step.

5

Page 6: The SumoBot Handbook_final

5.2 CAD implementation

With your design complete, you will now make a CAD model of the robot’s chassis. If you’re notfamiliar with CAD or 3D printing, just talk to your mentor, and they’ll tell you everything youneed to know. Think of the chassis as an exoskeleton: something that protects the robot’s innerbody while keeping everything secure. If the chassis is too small or too oddly-shaped, none of therobot’s parts will fit. But if it’s too bulky or roomy, the robot’s parts will fall out. To prevent this,you should constantly ask yourself these questions while designing the CAD:

5.3 The CAD Checklist

• Does our design meet the requirements and constraints of the competition?

• Where will each sensor go?

– Can every sensor be wired to the Arduino?

– Can every sensor see the environment?

• Are the Arduino’s supporting wires secure?

• Can we reach the battery easily?

– Can the batteries power the Arduino, sensors, motors, and other components?

– Are these components all receiving power from the battery?

• Are the motors in the right place?

– Do the motors touch the ground (via the wheels)?

– Can they actually move the robot?

If you can answer “yes” to all of these questions, you’re ready to move on.

6 Electrical Design

Now that you know how your robot looks, it’s time to really think about what goes inside: thesensors, the motors, the Arduino, the power source, and the wires connecting them all. For anexplanation of the Arduino’s various ports, pins, and connections, refer to Appendix A.

6.1 Sensors

When choosing sensors for your robot, keep these questions in mind:

1. How will your SumoBot detect the boundaries of the ring?

2. How will your SumoBot detect other robots?

Different sensors detect different things, and they detect these things in different ways. Somesensors can only tell you whether stimuli are “DETECTED” or “NOT DETECTED,” while othersprovide more detailed information, such as your robot’s distance from the stimulus. Every sensorhas its pros and cons. You may have to use two or more sensors, each one compensating foranother’s weakness. It’s a difficult decision, so to make things easier, we created this chart:

6

Page 7: The SumoBot Handbook_final

Table 1: Sensor Properties

SENSOR MECHANISM OFDETECTION

ADVANTAGES DISADVANTAGES

Infrared(IR)

Emits and Detects IRLight

Little interference fromnoise

Target must be in direct line ofsight

Low power requirement Short range only

Easy to use Signal can be absorbed by darksurfaces

Relatively inexpensive Relatively low sensitivity for dis-tance measurements

Unaffected by climate Large dead zone

Ultrasonic

Emits and detectssound waves greaterthan 18 kHz

Short and long range Must consider Doppler effect ifyour opponent’s robot is fast

Very sensitive distancemeasurements

Irregular or coarse surfaces de-flect the signal

Easy to use Relatively expensive

Little-to-no dead zone Lots of High Frequency Noise

Easy to use Affected by climate

Photo-resistorSemiconductor resis-tance decreases in lightand increases in thedark

Easy to use Does not provide distance

Relatively inexpensive Low sensitivity for low levels oflightl

Unaffected by climate Time delay between signal detec-tion and output

Optical sensorIR and optical light Similar to IR Similar to IRDetects light in theIR and optical electro-magnetic range, 300-1100 nm (varies be-tween manufacturers)*Dead zone refers to the sensor’s “blind spot,” where data becomes unreliable.

7

Page 8: The SumoBot Handbook_final

This table can be helpful, but only for a surface-level reference. If you want to know more abouta sensor’s capabilities, READ THE MANUFACTURER’S DATA SHEET. Only then willyou know for sure if a sensor does what you need it to. And remember: the Sumobot ring is blackwith a white border. Your robot will probably have to differentiate between and respond to thesecolors.

6.2 Actuators

6.2.1 DC Motors

DC motors power the robot’s wheels, making them a vital component of the motor system. For abasic understanding of how DC motors work, watch this two-minute video: video

Every motor is different, as is every wheel, so choose carefully. And while you’re choosing, keepthese things in mind:

1. One DC motor can power a single wheel. If your robot has two wheels, it needs two motors; ifit has four wheels, it needs four motors. A robot with two motors is lightweight, inexpensive,energy-conscious, and relatively easy to wire, but it doesn’t have as much power to fall backon. Robots with four motors are pricier, heavier, and more difficult to manage, but theirextra power could make a difference. Again, choose carefully.

2. A DC motor is connected to its wheel by two gears: the driven gear and the drive gear.The driven gear (or ring) is powered by the motor itself, while the drive gear (or pinion)turns the wheel. Gear ratio is the difference between the numbers of teeth on these twotypes of gears. The more teeth the driven gear has relative to the drive gear, the higher thegear ratio becomes, and the faster the drive gear will turn. If your robot has small wheels, itsDC motors also have small drive gears, which means the robot can move at higher speeds.Butspeed isn’t the only thing that matters. You also have to consider torque, the rotationalforce transferred from the motor to the wheels. A robot with lots of torque can move uphillmore easily and push weaker robots out of the ring, giving it a competitive advantage. Thebigger the drive gears, the bigger the torque, meaning a robot with large wheels and low gearratio has a lot of power backing it up. So, when choosing the size of your robot’s wheels, askyourself which is more important: speed or torque?

6.2.2 Servomotors

A servomotor is a device that makes small, precise changes in the angular position of the robot’sarmatures. A single servomotor contains a DC motor and a sensor. The sensor detects the arma-ture’s angle, compares it to the angle at which the armature should be, and sends feedback to theDC motor, which then corrects the armature’s position. They’re great for keeping your robot ontrack, but they usually can’t move farther than 180 degrees, meaning they can only correct smallerrors. Keep this in mind when designing your robot. You can use servomotors to mount sensors,rotate parts of your robot, or perform other functions. Like almost everything else in this project,it’s up to you.

6.2.3 A note about Motors

No matter how similar a pair of motors may seem, they will always move at slightly different speeds,even when given the same throttle. You may have to adjust each motor’s throttle individually to

8

Page 9: The SumoBot Handbook_final

keep your robot from swerving.

6.3 Power Systems

Your robot will have two power systems: a motor power system and a logic power system.Crisscrossing the power systems will burn your microcontroller — it’s not meant to handle largevoltages and amperages — so keep these systems separate.

6.3.1 The Motor Power System

It’s time to connect the Arduino to the motors. All you have to do is build an electrical circuit,and you’re done, right? Oh, if only it were that simple. . .

This process puts your Arduino in danger for two reasons. First, a motor is a power generator,so it will probably try to send a current back into the Arduino. Second, motors create somethingcalled back electromotive force (or back EMF for short). Back EMF comes from the differencein relative motion between the motor’s armature and magnetic field, and manifests itself as a voltagethat pushes against any incoming current. This includes the current coming from your Arduino, soyou’ll have to take some precautions.

These precautions come in the form of an H Bridge.

6.3.2 H-Bridges

H Bridges take signals from the microcontroller and increase their power with amplifier circuits.They also prevent current from going back the way it came. To give you a better idea of how an HBridge works, we stole this diagram from Wikipedia:

Figure 1: How H-Bridges work

The switches only allow current to move forwards; it cannot go back. S1 and S4 close at thesame time to go forwards, while S2 and S3 close simultaneously to go backwards. Hooking up an HBridge is straightforward enough. Just use the wiring diagram in Appendix B for reference. Andtry not to short anything.

9

Page 10: The SumoBot Handbook_final

6.4 The Logic Power System

The Arduino Uno has a recommended input voltage of 7-12 V. Anything below 7 V won’t haveenough voltage to power the Arduino. Anything above 12 V could damage the microcontroller. Werecommend using a 9 V battery, but other batteries are fine as long as they stay within the 7-12 Vlimit.

7 Software Design

So. . . everything’s hooked up. You’ve got your sensors working, your motors calibrated, and yourwires sorted out. Now it’s time for the real “fun”: programming the Arduino. I know what you’rethinking. You want to boot up your computer and start typing code. Well, don’t. You’ll have noclue where to begin. Instead, take some time to figure out how you want your robot to behave,then create a finite state machine (FSM).

7.1 Finite State Machines

A finite state machine is like a list of every action a system can take. At any time, an FSM canexist in one of many possible states. When certain conditions are met, the FSM will transitionfrom one state to another. One of the simplest FSMs out there is a blinking light:

Figure 2: The Blinking Finite State Diagram

As you can see, there are two bubbles; these represent the state of the light. These states areon and off. The arrows are where states can go if conditions are met.

For example, suppose the light is on. The system will remain in that state until one second ispassed. When that condition is met, the system will switch its state to where the arrow points.

10

Page 11: The SumoBot Handbook_final

Your system will not be more complicated; it will have more states to consider. What will yourrobot do when it sees a white line, what about when it sees your opponent?

It’s ok for this part to be difficult; it is one of the most challenging parts of the entire ordeal.But some time spent here will reap rewards in the future.

7.2 Coding

The Arduino has its own programming language, one similar to Java or C. If you have experiencewith these languages, Arduino programming shouldn’t be too hard. If not, don’t worry! Thelanguage is fairly straightforward, and there are tons of online resources to help you out. (Thishandbook is also pretty helpful, FYI.)

Let’s go through the blinking light program discussed in the FSM section. We’ll break it downbit by bit to show you how each function contributes to the program in its entirety.

7.2.1 Declare variables

The first thing you always do is declare your variables. If you want to use a pin, declare it herewith an easy-to-read variable name. Leave a comment with // in case you forget which variablesare which.

// On most Arduino boards , pin 13 i s connected to the LED.// Give i t a name :i n t l ed = 13 ;i n t s t a t e = 0 ;

7.2.2 Set up your ports

Once you’ve declared all your variables, it’s time to tell the pins what to do. Go to the setup areaand declare whether the pins are analog, digital, input, or output.

void setup ( ) {S e r i a l . begin ( 9 6 0 0 ) ;// I n i t i a l i z e the d i g i t a l pin as an output f o r the LED.pinMode ( led , OUTPUT) ;

}

7.2.3 The main program

A microcontroller system has lots of sensors and actuators to keep track of, and it needs to maintainthem all in real time. If it didn’t, your robot would drive itself off a cliff, because its actuatorscouldn’t react fast enough to incoming sensory data. The Arduino can’t keep an eye on everythingat once, so instead it runs in a constant loop, checking its conditions over and over until the machineturns off. This means the Arduino will repeat your code in an infinite cycle. There are two commontypes of checking behaviour:

1. Polling is when the microcontroller periodically checks on a sensor or actuator, usually aspart of an infinite loop. This would be like if you peeked out the window while waiting for afriend to come to your house.

11

Page 12: The SumoBot Handbook_final

2. When a sensor or actuator sends an alert to the microcontroller, it’s called an interrupt. Thiswould be like if your friend rang the doorbell when they arrived.

Sometimes a poorly-coded polling program gets stuck on a single statement, repeating endlessly.These statements are always “while” statements, so don’t use “while” statements unless you reallyknow what you’re doing.

// The loop rou t in e runs over and over again :void loop ( ) {

switch ( s t a t e ) {case 0 :

// Turn the LED on . HIGH i s the vo l tage l e v e l .d i g i t a l W r i t e ( led , HIGH) ;

// Wait f o r one second (1000 m i l l i s e c o n d s . )de lay ( 1 0 0 0 ) ;

s t a t e = 1 ; // Switch the s t a t ebreak ;

case 1 :// Turn the LED o f f . LOW i s the vo l tage l e v e l .d i g i t a l W r i t e ( led , LOW) ;de lay ( 1 0 0 0 ) ;s t a t e = 0 ; // Switch the s t a t ebreak ;

}}

7.2.4 Writing functions

You’ll probably have to duplicate certain pieces of code in the loop function. For example, a codethat tells the robot how to turn left or right is going to show up a lot. You may want to writeseparate modules for repeated functions to reduce the size and redundancy of your code. SeeAppendix C for examples. Put together, the code looks like this:

/∗Blink :Turns an LED on f o r one second , then o f f f o r one second .

Repeats i n f i n i t e l y .This example code i s in the pub l i c domain .∗/// Step 1 : Dec lare your v a r i a b l e s .// On most Arduino boards , pin 13 i s connected to the LED.// Give i t a name :i n t l ed = 13 ;s t a t e = 0 ;

12

Page 13: The SumoBot Handbook_final

// Step 2 : Set up your p ins// the setup rou t in e runs once when you pr e s s r e s e t :void setup ( ) {

// I n i t i a l i z e the d i g i t a l pin as an output .pinMode ( led , OUTPUT) ;}// Step 3 : The main program// The loop rou t in e runs over and over again f o r e v e r :void loop ( ) {

switch ( s t a t e ) {case 0 :

// Turn the LED on . HIGH i s the vo l tage l e v e l .d i g i t a l W r i t e ( led , HIGH) ;// Wait f o r one second (1000 m i l l i s e c o n d s ) .de lay ( 1 0 0 0 ) ;s t a t e = 1 ; // Switch the s t a t ebreak ;

case 1 :// Turn the LED o f f . LOW i s the vo l tage l e v e l .d i g i t a l W r i t e ( led , LOW) ;de lay ( 1 0 0 0 ) ; // Wait f o r one second .s t a t e = 0 ; // Switch the s t a t ebreak ;

}}

Once the FSM is complete, upload it to your Sumobot’s Arduino and see if it works. If you don’tknow how to upload the code, see Appendix D for a step-by-step run through. For a comprehensiveexplanation of the Arduino’s programming language, go to this website: http://arduino.cc/en/Reference/HomePage.If you run into any problems, try Googling a solution. Online forums are surprisingly helpful.

7.3 Pulse Width Modulation

Sensors and actuators can send and receive two different types of signals: analog and digital.Analog signals represent the world as a series of gradients, where the slightest change in soundfrequency or EM wavelength is detected, measured, and sent to the Arduino as an equally slightchange in voltage. By contrast, digital signals can only say whether something is “DETECTED”or “NOT DETECTED.”

Most motor drivers and actuators have two possible outputs (“OFF” and “ON,” where “ON”is the fastest speed possible), which means they can only respond to digital signals. Unfortunately,most sensors use analog signals, making it hard for the Arduino to relay information from thesensors to the motors.

Thankfully, we can represent analog information as a digital signal using a technique calledpulse width modulation (PWM). Most modern sensors, motor drivers, and actuators rely onPWM.

13

Page 14: The SumoBot Handbook_final

7.3.1 How does it work?

Imagine you have a light switch with two possible states: ON and OFF. The lights are too brightwhen turned ON, but there’s no way to make them dimmer without turning them completely OFF.

If you flick the light switch ON and OFF very quickly — faster than human hands can move —you can control the brightness of the room in gradients. The longer you leave the light switch ONbefore turning it OFF (and remember, you’re still flicking the switch faster than humanly possible)the brighter the lights appear to be. The longer you leave the lights OFF before turning them backon, the dimmer the room becomes. You are using digital inputs to simulate analog outputs.

The microcontroller does something similar. Every few milliseconds, the Arduino sends a 5V“pulse” of electricity to the motors. The greater the sensor’s signal, the longer the pulse lastsrelative to the milliseconds-long interval. The longer the pulse lasts, the longer the motors stayON, and the faster the robot moves.

For example, let’s say the interval is two milliseconds long. A 5V analog signal will producea pulse that lasts the entire two milliseconds; if the signal is sustained, your robot will eventuallyreach top speed. A 2.5V analog signal will produce a 1 millisecond long pulse, which puts the robotat half-speed. A 0V signal doesn’t produce any pulse at all, so the robot will decelerate to a halt.

Unlike a light switch, your robot’s motors cannot instantly change speeds. They need time toaccelerate or decelerate to the desired pace. Keep this in mind while programming your Arduino’sPWM.

Appendix E shows the difference between PWMs and analog implementation in Arduino code.

8 Conclusion

We hope this guide was a useful introduction to Sumobots. Below you’ll find the appendix, wherewe put most of our charts, diagrams, and extra pieces of information. If you have any questionsthis guide couldn’t answer, just talk to your mentors or send an email to Vinay Yuvashankar [email protected]. Good Luck!

14

Page 15: The SumoBot Handbook_final

A Arduino Anatomy

This picture, with all its parts and pins, might seem complicated to someone who’s never used amicrocontroller before. Don’t worry — it’s not as hard as it looks. And to prove it, we made achart for easy reference! Parts are listed clockwise from “External Power Supply” to “Reset Pin.”

Figure 3: The Arduino

15

Page 16: The SumoBot Handbook_final

Port FunctionExternal power supply Plug for the battery that powers the Arduino

and sensors. Use a separate power supply forthe motors.

Analog reference pin You probably won’t use this pin. Provides areference voltage for Analog inputs. Default is5 V, but can be changed.

Digital ground Ground wires for digital inputs and all outputsmust terminate here.

Digital I/O pins Digital inputs and all outputs are connectedhere. 12 pins available for digital inputs andoutputs, numbered 2-13.

Serial out (TX) Uses serial communication to send data to acomputer or other device

Serial in (RX) Uses serial communication to receive data fromsaid device

Reset button Erases all programming embedded on the Ar-duino. Don’t push it by accident!

In-circuit serial programmer You probably won’t use this pin. Allows you toburn sketches onto the Arduino board throughan external programmer instead of the boot-loader (which loads the OS).

ATMega328 microcontroller This is the processor. Contains 32 KB of mem-ory.

Analog in pins Connection point for sensors that take Analogreadings. 6 pins are available for Analog in-puts.

Voltage in You probably won’t use this pin. An alterna-tive to the Arduino’s external power supply.

Ground pins Connection point for ground wires from Analoginputs. There are two ports. It doesn’t matterwhich one you use.

5 Volt power pin This pin has 5 V (relative to the ground). Sen-sors and motors that require 5 V or Vcc shouldbe wired to this pin.

3.3 Volt power pin This pin has 3.3 V (relative to the ground).Sensors and motors that require 3.3 V or Vccshould be wired to this pin.

Reset pin Will reset the Arduino if it receives a positivevoltage

16

Page 17: The SumoBot Handbook_final

B The CAD Checklist

• Ensure design is within competition constraints

• Ensure that all sensors have been considered (IR and Distance)

– Ensure that all sensors can be wired

– Ensure that sensors are able to see the environment

• Ensure that Arduinos and supporting wires are in a secure place

• Ensure that Batteries are at an easily accessible place

– Ensure that all components (Sensors, Actuators and Arduinos) can be powered from thebattery

• Ensure Motors are at the correct place

– Ensure that motors can touch the ground and move the robot

17

Page 18: The SumoBot Handbook_final

C Hooking Up Sensors and H-Bridges

Figure 4: The Arduino

http://bildr.org/2011/03/various-proximity-sensors-arduino/ has a great tutorial onhooking up sensors to the arduino.

18

Page 19: The SumoBot Handbook_final

D Sample SumoBot Code

// Dec lare p ins f o r the r i g h t motor .const i n t motor pin1 = 5 ;const i n t motor pin2 = 6 ;

// Dec lare p ins f o r the l e f t motor .const i n t motor pin3 = 10 ;const i n t motor pin4 = 11 ;

// Dec lare p ins f o r p h o t o r e s i s t o r s and the IR senso r .const i n t IRSens = 0 ;const i n t PHOTO1 = 1 ;const i n t PHOTO2 = 2 ;const i n t PHOTO3 = 3 ;const i n t PHOTO4 = 4 ;

// Dec lare p h o t o r e s i s t o r s cut−o f f va lue s f o r black−white d e t e c t i o n .// This i s what you came up with a f t e r t e s t i n g :i n t In i tVa l1 = 120 ;i n t In i tVa l2 = 50 ;i n t In i tVa l3 = 270 ;i n t In i tVa l4 = 190 ;

// Step 2 : Set up your por t s

void setup ( ) {

// Data t r a v e l s through the s e r i a l data stream at the 9600 band .S e r i a l . begin ( 9 6 0 0 ) ;

// Set up motor p ins .pinMode ( motor pin1 , OUTPUT) ;

.// Set up senso r p ins .pinMode (PHOTO1, INPUT) ;. . .

}

// Step 3 : Writing f u n c t i o n s// He lp fu l as the se examples may be ,// y o u l l probably have to wr i t e other f u n c t i o n s on your own .

// This next func t i on i s a debugging t o o l that// p r i n t s s enso r r ead ings f o r your own sake .//The Arduino d o e s n t a c t u a l l y use checkSens ( ) to check the s e n s o r s .

19

Page 20: The SumoBot Handbook_final

void checkSens ( ) {S e r i a l . p r i n t (” Distance : ” ) ;S e r i a l . p r i n t ( analogRead ( 0 ) ) ;S e r i a l . p r i n t (”\ tPhoto 1 : ” ) ;S e r i a l . p r i n t ( analogRead (PHOTO1) ) ;S e r i a l . p r i n t (”\ tPhoto 2 : ” ) ;S e r i a l . p r i n t ( analogRead (PHOTO2) ) ;S e r i a l . p r i n t (”\ tPhoto 3 : ” ) ;S e r i a l . p r i n t ( analogRead (PHOTO3) ) ;S e r i a l . p r i n t (”\ tPhoto 4 : ” ) ;S e r i a l . p r i n t l n ( analogRead (PHOTO4) ) ; }

// This func t i on t e l l s the motors to move forward :void forward ( ) {

analogWrite ( motor pin1 , 0 ) ;analogWrite ( motor pin2 , 1 5 0 ) ;analogWrite ( motor pin3 , 1 5 0 ) ;analogWrite ( motor pin4 , 0 ) ;

}

// This func t i on s tops a l l motors :void ha l t ( ) {

AnalogWrite ( motor pin1 , 0 ) ;AnalogWrite ( motor pin2 , 0 ) ;AnalogWrite ( motor pin3 , 0 ) ;AnalogWrite ( motor pin4 , 0 ) ; }

// Step 4 : The main code//Your main code w i l l be much longe r than the provided example ,// but i t should g ive you an idea o f where to s t a r t :

void loop ( ) {

checkSens ( ) ;//Get r ead ings from the s e n s o r s that can be s to r ed as v a r i a b l e s .unsigned i n t read1 = analogRead (PHOTO1) ;unsigned i n t read2 = analogRead (PHOTO2) ;unsigned i n t read3 = analogRead (PHOTO3) ;unsigned i n t read4 = analogRead (PHOTO4) ;unsigned i n t th ing = analogRead ( IRSens ) ;

// Decis ion−making code :i f ( ( read1 > In i tVa l1 ) && ( read2 > In i tVa l2 ) ) {

// I f both f r o n t s e n s o r s de t e c t white :ha l t ( ) ;

}

20

Page 21: The SumoBot Handbook_final

e l s e { forward ;}

}

Put together , the code l ooks l i k e t h i s :

// Dec lare p ins f o r the r i g h t motor .const i n t motor pin1 = 5 ;const i n t motor pin2 = 6 ;

// Dec lare p ins f o r l e f t motorconst i n t motor pin3 = 10 ;const i n t motor pin4 = 11 ;

// Dec lare p ins f o r p h o t o r e s i s t o r s and the IR senso r .const i n t IRSens = 0 ;const i n t PHOTO1 = 1 ;const i n t PHOTO2 = 2 ;const i n t PHOTO3 = 3 ;const i n t PHOTO4 = 4 ;

// Dec lare p h o t o r e s i s t o r s cut−o f f va lue s f o r black−white d e t e c t i o n .// This i s what you came up with a f t e r t e s t i n g :i n t In i tVa l1 = 120 ;i n t In i tVa l2 = 50 ;i n t In i tVa l3 = 270 ;i n t In i tVa l4 = 190 ;

//Get r ead ings from the s e n s o r s that can be s to r ed as v a r i a b l e s :unsigned i n t read1 ;unsigned i n t read2 ;unsigned i n t read3 ;unsigned i n t read4 ;unsigned i n t th ing ;

void setup ( ) {

// Data t r a v e l s through the s e r i a l data stream at the 9600 band .S e r i a l . begin ( 9 6 0 0 ) ;

// Set up motor p ins .pinMode ( motor pin1 , OUTPUT) ;

.// Set up senso r p ins .pinMode (PHOTO1, INPUT) ;. . .

21

Page 22: The SumoBot Handbook_final

}

// T e l l s you what the s e n s o r s s ee .// D o e s n t make the Arduino check the s e n s o r s .void checkSens ( ) {

S e r i a l . p r i n t (” Distance : ” ) ;S e r i a l . p r i n t ( analogRead ( 0 ) ) ;S e r i a l . p r i n t (”\ tPhoto 1 : ” ) ;S e r i a l . p r i n t ( analogRead (PHOTO1) ) ;S e r i a l . p r i n t (”\ tPhoto 2 : ” ) ;S e r i a l . p r i n t ( analogRead (PHOTO2) ) ;S e r i a l . p r i n t (”\ tPhoto 3 : ” ) ;S e r i a l . p r i n t ( analogRead (PHOTO3) ) ;S e r i a l . p r i n t (”\ tPhoto 4 : ” ) ;S e r i a l . p r i n t l n ( analogRead (PHOTO4) ) ; }

// T e l l s the motors to move forward :void forward ( ) {

analogWrite ( motor pin1 , 0 ) ;analogWrite ( motor pin2 , 1 5 0 ) ;analogWrite ( motor pin3 , 1 5 0 ) ;analogWrite ( motor pin4 , 0 ) ;

}

// Stops a l l motors :void ha l t ( ) {

AnalogWrite ( motor pin1 , 0 ) ;AnalogWrite ( motor pin2 , 0 ) ;AnalogWrite ( motor pin3 , 0 ) ;AnalogWrite ( motor pin4 , 0 ) ; }

void loop ( ) {

checkSens ( ) ;//Get r ead ings from the s e n s o r s that can be s to r ed as v a r i a b l e s :read1 = analogRead (PHOTO1) ;read2 = analogRead (PHOTO2) ;read3 = analogRead (PHOTO3) ;read4 = analogRead (PHOTO4) ;th ing = analogRead ( IRSens ) ;

// Decis ion−making code :i f ( ( read1 > In i tVa l1 ) && ( read2 > In i tVa l2 ) ) {

// I f both f r o n t s e n s o r s de t e c t white :ha l t ( ) ;

}

22

Page 23: The SumoBot Handbook_final

e l s e { forward ;}

}

23

Page 24: The SumoBot Handbook_final

E PMWs in Arduinos

PWM signals make coding much easier. With PWM, you don’t have to worry about remappinginput values to match output values. PWM signals are represented in 8 bits, so values range between0 and 255, with 0 representing 0V and 255 representing 5V. The code needed to read and writedigital signals is similar to the code needed for analog signals; the only difference is the syntax,which is described below:

E.1 Analog

// Fi r s t , map the p ins .const i n t analogInPin = A0 ; // Analog inputconst i n t analogOutPin = 9 ; // Analog output

// Def ine other v a r i a b l e s :i n t InputValue = 0 ;i n t OutputValue = 0 ;

void setup ( ) {S e r i a l . begin ( 9 6 0 0 ) ;

}

void loop ( ) {// Te l l i t how to read the analog input value :ImputValue = analogRead ( analogInPin ) ;

// Do a l l nece s sa ry c a l c u l a t i o n s and r e s c a l e// the va lue s so they f a l l between 0 and 255 .OutputValue = map( InputValue , 0 , 1023 , 0 , 2 5 5 ) ;

// Change the analog output value .analogWrite ( analogOutPin , OutputValue ) ;

}

E.2 PWM

// Fi r s t , map the p ins .cons t i n t d i g i t a l I n p u t P i n = 10 ; // D i g i t a l Inputconst i n t d ig i ta lOutPin = 9 ; // D i g i t a l Output

// Def ine other v a r i a b l e s :i n t InputValue = 0 ;i n t OutputValue = 0 ;

24

Page 25: The SumoBot Handbook_final

void setup ( ) {S e r i a l . begin ( 9 6 0 0 ) ;pinMode ( d i g i t a l InputP in , OUTPUT) ;pinMode ( dig i ta lOutputPin , INPUT) ;

}

void loop ( ) {// Te l l i t how to read the d i g i t a l input value :

InputValue = d ig i ta lRead ( d i g i t a l I n p u t P i n ) ;

// I t i sn ’ t nece s sa ry to r e s c a l e d i g i t a l va lues ,// because they a l ready f a l l between 0 and// 255 .OutputValue = map( InputValue , 0 , 255 , 0 , 2 5 5 ) ;

// Output the nece s sa ry d i g i t a l va lue .d i g i t a l W r i t e ( d ig i ta lOutputPin , OutputValue )

}

25