The Robotics Toolbox A year’s worth of learning in two hours.

68
The Robotics Toolbox A year’s worth of learning in two hours.

Transcript of The Robotics Toolbox A year’s worth of learning in two hours.

Page 1: The Robotics Toolbox A year’s worth of learning in two hours.

The Robotics ToolboxA year’s worth of learning in two hours.

Page 2: The Robotics Toolbox A year’s worth of learning in two hours.

A Quick Tutorial• Rather than just a Show-and-Tell, let

me explain some of the basics of computer and microcontroller programming

• Very abbreviated tour of the book• Let’s start by learning how the BX-24

outputs data…

Page 3: The Robotics Toolbox A year’s worth of learning in two hours.

“Computers never do what you want them to do, only what you tell them to do.”

Brian Patton

Vice President, Robodyssey Systems

Page 4: The Robotics Toolbox A year’s worth of learning in two hours.

The Motherboard

You will control the BX-24 by sending your computer code through the Robodyssey Advanced MotherBoard, or RAMB. A serial cable connects the motherboard to your PC.

Connect the wire to the Motherboard now.

Page 5: The Robotics Toolbox A year’s worth of learning in two hours.

Chapter 1: Creating a Program

• The program is written on the PC in the BasicX language

• The code is saved as a simple text file. (New programs start with a blank page.)

• When the program is ready to run, simply press one button to compile the program into a language the BX-24 can understand

• The compiled program is sent to the BX-24 via a serial cable

Page 6: The Robotics Toolbox A year’s worth of learning in two hours.

Create a ProjectOpen the BasicX software:

Page 7: The Robotics Toolbox A year’s worth of learning in two hours.

Create a ProjectOpen the BasicX Editor:

Page 8: The Robotics Toolbox A year’s worth of learning in two hours.

Create a ProjectMake a New Project:

Page 9: The Robotics Toolbox A year’s worth of learning in two hours.

Create a ProjectName the project and module “MyFirstApp”

Save the project in the Robots folder on Drive C.

Page 10: The Robotics Toolbox A year’s worth of learning in two hours.

Ready, Set, Code!

Enter your code here!

Page 11: The Robotics Toolbox A year’s worth of learning in two hours.

Chapter 2: Printing to the PC

Let’s write a short program that will display a simple message to the computer screen. The text message will be sent back to the PC via the serial cable. Here’s the code:

Public Sub Main()Debug.Print "Hello, my name is Chris."

End Sub

Page 12: The Robotics Toolbox A year’s worth of learning in two hours.

“Run” the Code

To make your program run on the BX-24, simply press the F5 key on your keyboard.

Watch the window on the left side of your computer screen. You should see your message.

Or do you?

Page 13: The Robotics Toolbox A year’s worth of learning in two hours.

Oops!Increase the string length. From the menu:Options >> Environment >> Max String Length = 60

Now, here’s my program’s output:

Page 14: The Robotics Toolbox A year’s worth of learning in two hours.

Run it again!Press the Reset Button on the Motherboard to run the program again.

Did the same message show up again?

Page 15: The Robotics Toolbox A year’s worth of learning in two hours.

More than Meets the Eye

Turn off the power to the motherboard.

It is important to realize that it’s the BX-24, not the PC, that is controlling the action!

To prove this, swap motherboards with the person next to you and run their program, displaying their program’s output on your PC.

Cool, huh?

Now turn it back on. Despite removing power to the BX-24, the program still resides on the chip. Try that with a PC!

Page 16: The Robotics Toolbox A year’s worth of learning in two hours.

Printing to an LCDWith a small LCD screen, we can print messages and data without the need of a computer monitor. This allows us to be mobile!

See www.basicxandrobotics.com/tutorials/LCD/ to learn how.

Page 17: The Robotics Toolbox A year’s worth of learning in two hours.

Printing NumbersWhen computers output data to the screen, that data must be text, not numbers. In computer-speak, we call text strings, because it is a string of characters.

Your PC’s processor is big and powerful enough to convert numbers to strings on the fly, but we have to tell the BX-24 to perform this conversion explicitly. We do it with the CStr procedure:

Public Sub Main()Debug.Print "Hello, my name is Chris."Debug.Print CStr(38)

End Sub

Run the program and see what happens.

Page 18: The Robotics Toolbox A year’s worth of learning in two hours.

Fancy Printing

We can put two or more strings together with the so-called concatenation operator, &.

Like this:

Public Sub Main()Debug.Print "Hello, my name is Chris."Debug.Print "I am " & CStr(38) & " yrs."

End Sub

Run the program and see what happens.

Page 19: The Robotics Toolbox A year’s worth of learning in two hours.

A Mathematical Genius

Computers are calculating machines, so let’s do a little math.

How about: 22

Debug.Print CStr(2 + 2)

or

Debug.Print "2 + 2 = " & CStr(2 + 2)

but not

Debug.Print 2 + 2

Page 20: The Robotics Toolbox A year’s worth of learning in two hours.

Chapter 4: Some More MathDon’t feel obligated to enter all this code.

43 3.0 * 4.0Debug.Print CStr(3.0 * 4.0)

12

66.0 / 12.0Debug.Print CStr(6.0 / 12.0)

25 Sqr(25.0)Debug.Print CStr(Sqr(25.0))

82 2.0^8.0Debug.Print CStr(2.0^8.0)

Page 21: The Robotics Toolbox A year’s worth of learning in two hours.

Compiler AssistanceSay you make a mistake. For example, say we wish to calculate and we enter the following, forgetting to put a decimal after 25 like this:

25

Debug.Print CStr(Sqr(25))

The compiler will catch your mistake and give you some hints on how to fix it:

Page 22: The Robotics Toolbox A year’s worth of learning in two hours.

Chapter 7: Even More MathDon’t feel obligated to enter all this code.

6e Exp(6.0)Debug.Print CStr(Exp(6.0))

)100log( Log10(100.0)Debug.Print CStr(Log10(100.0))

eln Log(Exp(1.0))Debug.Print CStr(Log(Exp(1.0)))

cos Cos(3.14159)Debug.Print CStr(Cos(3.14159))

Page 23: The Robotics Toolbox A year’s worth of learning in two hours.

Caveat EmptorIf you are in the market for a microcontroller or a robotics system, make sure that the processor can perform floating point math!

That is, if the microcontroller cannot manipulate decimal numbers, you probably don’t want it!

Page 24: The Robotics Toolbox A year’s worth of learning in two hours.

Chapter 5: Using VariablesJust like in math class, we can create and utilize variables:

Dim x as SingleDim y as SingleDim Answer as Single

x = 5.0y = -2.5Answer = x * y

Debug.Print "Answer = " & CStr(Answer)

There are many kinds of data types. Some common ones are Integer, Single, & Byte. See Chapter 5 for more details.

Page 25: The Robotics Toolbox A year’s worth of learning in two hours.

Making Math RelevantHere’s one of many Challenge Problems in my book that students find interesting. You’ll find it on page 64.

The monthly payment of long-term loans, such as home mortgages or car loans, is calculated with the formula

where

ni

iAmountLoanPaymentMonthly

11__

• Loan_Amount is the cost of the home or car minus the down payment. • i is the monthly interest rate. Since you are interested in calculating a monthly

payment, i is the annual percentage rate (APR) divided by 12. Say, for example the APR is 7.3%, then .

• n is the payment period (in months). If you wish to finance your loan over a period of 5 years, for instance, .

006083.012

0.073i

60125 n

65. Calculate the monthly payment amount on a $25,000 loan at 7.3% annual interest over a period of 5 years. (Check your answers with one of the many online loan calculators.)

Page 26: The Robotics Toolbox A year’s worth of learning in two hours.

Making Math Relevant

Dim LoanAmt as SingleDim n as Single ' Payment period in mo.Dim APR as Single ' Annual rateDim i as Single ' Monthly rateDim Payment as Single

LoanAmt = 25000.0 ' $25,000 loann = 60.0 ' 5-year paymentAPR = 0.073 ' APR = 7.3%i = APR/12.0 ' Calculte monthly ratePayment = LoanAmt*(i/(1.0-(1.0+i)^(-n)))Debug.Print "Payment = $" & CStr(Payment)

Here’s one way to answer this Challenge Problem:

Page 27: The Robotics Toolbox A year’s worth of learning in two hours.

Chapter 3: Output Using PutPin

BasicX has a command named PutPin that can be used to turn on and off external devices such as lights, LEDs, motors, buzzers, etc.

All you have to do is tell the BX-24 which pin the device is connected to and whether to turn it on or off.

You execute this command calling it with Call.

Page 28: The Robotics Toolbox A year’s worth of learning in two hours.

Onboard LEDsThe BX-24 has two built-in LEDs. One red (pin 25), one green (pin 26).

Think about how to make a light blink. What we take for granted must be painstakingly programmed into the computer line by line:

– Turn on the light– Leave it on for some time– Turn off the light– Leave it off for some time– Repeat

Page 29: The Robotics Toolbox A year’s worth of learning in two hours.

Onboard LEDs

Call PutPin(25, 0)

0 turns it ON

1 turns it OFF

Pin 25 is the red LED.

Pin 26 is the Green LED.

This is one thing I don’t understand about BasicX. For the internal LEDs, we turn them on with 0 and off with 1.

For all other devices such as external LEDs, buzzers, and motors, we use 1 to turn them on and 0 to turn them off as expected.

Page 30: The Robotics Toolbox A year’s worth of learning in two hours.

Too Fast For You?

Call PutPin(25, 0) ' Turn On REDCall PutPin(25, 1) ' Turn Off REDCall PutPin(26, 0) ' Turn On GreenCall PutPin(26, 1) ' Turn Off Green

Run this and see what you get.

Are you surprised?

Page 31: The Robotics Toolbox A year’s worth of learning in two hours.

Chapter 3: DelaysSometimes, the computer performs its tasks too quickly. We can use a Delay command to slow it down.

Again, use Call:

Call Delay(0.1) ' Pauses for 0.1sCall Delay(1.0) ' Pauses for 1.0sCall Delay(10.0) ' Pauses for 10.0s

Page 32: The Robotics Toolbox A year’s worth of learning in two hours.

Back to the LEDs…

Call PutPin(25, 0) ' Turn On REDCall Delay(1.0)Call PutPin(25, 1) ' Turn Off REDCall Delay(0.5)Call PutPin(26, 0) ' Turn On GreenCall Delay(1.75)Call PutPin(26, 1) ' Turn Off Green

Now that’s better!

Page 33: The Robotics Toolbox A year’s worth of learning in two hours.

Chapter 11: BX-24 & RAMB PinsAs shown here, the BX-24 has 24 pins coming out of its underside. Pins 1-4 are used to communicate with the PC and pins 21-24 are used to power to BX-24. These eight pins are therefore off-limits to us programmers.

Robodyssey numbered the pins of their motherboard according to industry standards. That is, pin 0 on the RAMB corresponds to pin 5 on the BX-24.

It seems confusing, but you get used to it. Just remember to add 5 to the RAMB pin number to get the corresponding BX-24 pin.

Page 34: The Robotics Toolbox A year’s worth of learning in two hours.

Chapter 11: RAMB Signal Pins

Each of the 16 signal pins are capable of outputting a 5V signal.

Eight of the 16 pins can be used for analog-to-digital (A-to-D) conversions. These are pins 13-20 on the BX-24 and pins 8-15 on the RAMB.

S S

Each pin of the BX-24 corresponds to three pins on the RAMB.

The pins closest to the BX-24 (i.e., the innermost pins) are the signal pins. These pins are directly connected to the pins of the BX-24 and are known as the I/O pins, since they can receive input voltages as well as send output voltages.

Page 35: The Robotics Toolbox A year’s worth of learning in two hours.

Chapter 11: RAMB Power PinsThe middle pins on the RAMB are the power pins. These are used to provide power to motors and sensors plugged into the RAMB.

The power pins on the right side of the board (RAMB pins 8-15) receive their voltage from a 5V regulator and therefore provide a very steady 5-volts.

The power pins on the left side of the board (RAMB pins 0-7) can either be powered from the 5V regulator or from the battery pack. (The new RAMB II motherboard comes with a selectable jumper.) If powered directly from the battery pack, the voltage will drop as the batteries lose power.

P P

Page 36: The Robotics Toolbox A year’s worth of learning in two hours.

Chapter 11: RAMB Ground Pins

The pins farthest from the BX-24 (i.e., the outermost pins) are the ground pins. These pins are connected to the electrical ground of the RAMB circuitry.

When a sensor or servo is plugged into the RAMB, the ground pins provide these peripheral devices with the necessary electrical ground.

One should never short the ground pins to the power pins! Doing so will produce a huge electric current that may damage the microcontroller, RAMB, and batteries.

G G

Page 37: The Robotics Toolbox A year’s worth of learning in two hours.

Chapter 11: Color Conventions

It is a common convention in electronics to wire electrical components with wires of distinguishable colors. Usually, sensors and servos are wired from light-to-dark – the wire of the lightest color carries the signal, the darkest wire is the ground wire, and the middle wire carries the power.

When connecting or disconnecting components, you should always turn off the power to the RAMB.

Lightest

Darkest

Ground Wire

Page 38: The Robotics Toolbox A year’s worth of learning in two hours.

Appendix D: Piezo Buzzer!

Attached to your motherboard is a piezo buzzer. It is connected to pin 12 on BX-24, which is labeled pin 7 on Robodyssey’s motherboard.

Note that the positive light-colored lead wire is connected to the innermost signal pin and the negative black lead is connected to the outermost ground pin.

Page 39: The Robotics Toolbox A year’s worth of learning in two hours.

Piezo Buzzer!

To turn on any external device, use a 1 to set the pin “high”. Use a 0 (“low”) to turn it off.

Call PutPin(12, 1) ' Turn On buzzerCall Delay(0.3)Call PutPin(12, 0) ' Turn Off buzzerCall Delay(0.5)Call PutPin(12, 1) ' Turn On buzzerCall Delay(0.3)Call PutPin(12, 0) ' Turn Off buzzer

Page 40: The Robotics Toolbox A year’s worth of learning in two hours.

External LEDs

External LED’s can also be controlled in the same way with the BX-24 using the PutPin command.

See www.basicxandrobotics.com to learn how to make these little guys.

Page 41: The Robotics Toolbox A year’s worth of learning in two hours.

Loops

Many computer applications use structures called “loops” to repeatedly perform a task or tasks.

For-to-Next loops run for a finite number of times.

Do-Loops can run forever.

Page 42: The Robotics Toolbox A year’s worth of learning in two hours.

Chapter 8: For-To-Next LoopsDim j as IntegerDebug.Print "I can count fast!"For j = 1 to 5

Debug.Print CStr(j)Next

Page 43: The Robotics Toolbox A year’s worth of learning in two hours.

Multiple Beeps

For j = 1 to 10Call PutPin(12, 1) ' Turn On buzzerCall Delay(0.1)Call PutPin(12, 0) ' Turn Off buzzerCall Delay(0.1)

Next

Page 44: The Robotics Toolbox A year’s worth of learning in two hours.

Chapter 9: Do-LoopsDo

Debug.Print "I can't stop!!!"Loop

Page 45: The Robotics Toolbox A year’s worth of learning in two hours.

Create Another Project

• Make a New Project• Save it in the Robots folder on Drive C.• Name the Project and the Module “Sensors”

Page 46: The Robotics Toolbox A year’s worth of learning in two hours.

Voltmeter• Not only can the BX-24 output voltages,

it can read them, too.• This is easy to do with the GetADC

command.• The analog voltage from a battery, for

example, must be converted into a digital signal before it can be read by the computer.

Debug.Print CStr(GetADC(13))

• The BX-24 has eight built-in A-to-D converters! These are pins 13-20 on the BX-24 (pins 8-15 on the RAMB). For example, if we plug voltage probes into pin 13 (RAMB pin 8), we can print the voltage readings like so:

Page 47: The Robotics Toolbox A year’s worth of learning in two hours.

Understanding the Signal

• Digital signal 0 = 0V• Digital Signal 1023 = 5V (10-bit value)

• Therefore, a signal of 512 is ~2.5V.

• Why 1023?– The BX-24 uses a 10-bit processor

so 210 = 1024, or 0 to 1023.

• If this 1.28V battery was measured by the BX-24, what digital signal would it read?

2629.261V5

1023V28.1

Page 48: The Robotics Toolbox A year’s worth of learning in two hours.

Caveat EmptorIf you are in the market for a microcontroller or a robotics system, make sure that the processor has a built-in analog-to-digital (A-to-D) converter!

That is, if the microcontroller cannot read analog voltages directly, you probably don’t want it!

Page 49: The Robotics Toolbox A year’s worth of learning in two hours.

Chapter 17: Light Sensor• Ambient light can also be read

using GetADC and an inexpensive photoresistor.

• The photoresistor must be plugged into voltage divider board (VDB).

• Turn off the power to the RAMB before connecting any cables to the RAMB!

• The VDB must be plugged into one of the A-to-D pins. The orientation of the cable is important: The darkest wire is always positioned nearest the edge of the board. (Dark means ground.)

Page 50: The Robotics Toolbox A year’s worth of learning in two hours.

A Little Light Reading

• As the light intensity hitting the photoresistor increases, its resistance drops and the voltage to the BX-24 increases.

• Verify that this is so by plugging your VDB into pin 14 (pin 9 on the RAMB) and entering the code below.

Public Sub Main()Do

Debug.Print CStr(GetADC(14))Call Delay(0.2)

LoopEnd Sub

Page 51: The Robotics Toolbox A year’s worth of learning in two hours.

Light Meter Data

From Section 17.4 in BasicX and Robotics

A night watchman was “observed” checking the classroom.

Page 52: The Robotics Toolbox A year’s worth of learning in two hours.

Inverse Square LawAfter calibrating our light sensor, we can verify the famous inverse-square law of light to within 1.095%!

Lux vs Distance

Lux = 73606x-1.9781

R2 = 0.9874

0

1000

2000

3000

4000

0 10 20 30 40 50 60 70 80

Distance(cm)

Inte

ns

ity

(Lu

x)

See www.basicxandrobotics.com for more details.

Page 53: The Robotics Toolbox A year’s worth of learning in two hours.

Chapter 17: Temperature Sensor

• The ambient temperature be determined using GetADC, an inexpensive thermistor, and a voltage divider board. Just remove the photoresistor from the VDB and pop in the thermistor!

• As the temperature increases, the thermistor’s resistance increases and the voltage to the BX-24 decreases.

• The temperature can be calibrated and displayed in Fahrenheit, Celsius, and Kelvin scales. (See Section 17.7 of my book.)

Page 54: The Robotics Toolbox A year’s worth of learning in two hours.

Chapter 16: Infrared Range Finder• The range (or distance) to any

object can be determined using GetADC and an IR sensor. No VDB is required here.

• A transmitter sends out an invisible (to the human eye) beam of infrared light.

• The amount of light picked up by the receiver is an indication of the distance to the object

• Section 16.9 in my book shows how to convert the digital signal to an actual range

• Have you seen these? Yes!

Page 55: The Robotics Toolbox A year’s worth of learning in two hours.

Two Readings!You can read multiple sensors at the same time. With your thermistor in pin 14, plug your IR sensor into pin 15. (Remember to turn off the power to the RAMB!) Here’s one way to display both readings:

Public Sub Main()Do

Debug.Print CStr(GetADC(14))Debug.Print "IR = " & CStr(GetADC(15))Debug.PrintCall Delay(0.2)

LoopEnd Sub

Can your IR sensor detect movement?

Page 56: The Robotics Toolbox A year’s worth of learning in two hours.

Flame Sensor

• Raw infrared light (heat) can be measured and displayed using an infrared transistor and the GetADC command. No VDB is required.

• This sensor is also used in line- following robots and soccer-playing robots.

• Plug your flame sensor into pin 16 (RAMB pin 11) and see what light/heat sources you can detect.

See www.basicxandrobotics.com to learn how to make these little guys.

Page 57: The Robotics Toolbox A year’s worth of learning in two hours.

Three Readings!You may want to display all your sensor readings using variables to help you:

Public Sub Main()Dim IRValue as IntegerDim FlameValue as IntegerDo

IRValue = GetADC(14)FlameValue = GetADC(15)Debug.Print CStr(GetADC(14))Debug.Print "IR = " & CStr(IRValue)Debug.Print "Flame = " & CStr(FlameValue)Debug.PrintCall Delay(0.2)

LoopEnd Sub

Page 58: The Robotics Toolbox A year’s worth of learning in two hours.

Create Another Project

• Make a New Project• Save it in the Robots folder on Drive C.• Name the Project and the Module “Servos”

Page 59: The Robotics Toolbox A year’s worth of learning in two hours.

Chapter 11 & 13: Servomotors• Electric motors spin when a voltage is applied to it.• Servomotors (or servos) are special motors commonly used

in R/C planes, cars, and boats. Roboticists also use them.

Page 60: The Robotics Toolbox A year’s worth of learning in two hours.

• Servos are controlled with the PulseOut command.• A 1-ms pulse turns the servo clockwise• A 2-ms rotates it counterclockwise• A 20-ms delay is required after each pair of pulses.• The speed can be controlled with pulse width modulation

(PWM) but we’ll save that for another workshop.

Servomotors

Page 61: The Robotics Toolbox A year’s worth of learning in two hours.

Servos and PulseOut

Dim i as IntegerFor i = 1 to 20

Call PulseOut(5, 0.002, 1) Call PulseOut(6, 0.001, 1)

Call Delay(0.02) Next

The right servo is connected to pin 6

Pulsewidth = 0.001s(Clockwise)

The left servo is connected to pin 5

Pulsewidth = 0.002s(Counterclockwise)

Which direction will your Mouse robot move? Think about it before running the program!

Always put a 20ms delay between pulses!

Page 62: The Robotics Toolbox A year’s worth of learning in two hours.

Playtime!How far did your robot travel? In what direction did it travel? Can you do the following:

• Make your robot travel twice as far.

• Turn about its center to the left. To the right.

• Turn 90°.

• Turn 360°.

• Navigate a predetermined path on your table.

• Program your robot to pick up my kids from school.

Page 63: The Robotics Toolbox A year’s worth of learning in two hours.

Appendix G: Grippers• Grippers use servo motors to open

and close their jaws.• Loops and the PulseOut command

are used to control its movement.• To hold an object, the jaws must

continually be pulsed.

Page 64: The Robotics Toolbox A year’s worth of learning in two hours.

Gripper CodePlug a gripper into BX-24 pin 7 (RAMB pin 7) and enter the following code to make the jaws open and then close:

See www.basicxandrobotics.com/tutorials/multitasking/index.html to learn how to multitask with the BX-24!

' Open the jaws:For i = 1 to 20

Call PulseOut(7, 0.0011, 1)Call Delay(0.02)

Next

' Close the jaws:For i = 1 to 20

Call PulseOut(7, 0.00175, 1)Call Delay(0.02)

Next

Page 65: The Robotics Toolbox A year’s worth of learning in two hours.

Chapter 15: Computer Logic

1. First, check to see if the door is open.

2. If the door is open, then walk right through.

3. Else (computerese for “otherwise”), if the door is unlocked, then turn the handle and walk through.

4. Else, unlock the door with a key, turn the handle, and walk through.

The computer is only as smart as the person

who programmed it!

The computer can be programmed to think using the “If-Then” logic statement. Here’s one way that we humans employ logic statements to leave a room:

Page 66: The Robotics Toolbox A year’s worth of learning in two hours.

An If-Then Logic StatementFor i = 1 to 5

If (i <= 3) ThenDebug.Print "I am happy to be here!"

ElseIf (i = 4) ThenDebug.Print "BasicX rocks!"

ElseDebug.Print "Let's get started!"

End IfNext

Page 67: The Robotics Toolbox A year’s worth of learning in two hours.

Serious Playtime!We’ll that’s it! The whirlwind tour is over. In a few minutes you’ll have the opportunity to explore on your own and put your newfound tools to use. Stay as long as you like and have fun. We’ll be here to help.

If you want a challenge, program your Mouse to follow the black line race course. You’ll need two IR proximity sensors.

Or program it to follow you around the room. It’s not as difficult as it sounds.

Or use a gripper to grab objects.

Or make your robot move forward when a bright light hits the photocell.

Or do some science. The sky’s the limit!

Page 68: The Robotics Toolbox A year’s worth of learning in two hours.

Thank you for coming!

www.basicxandrobotics.com

[email protected]

If you are interested in having a robot loaned to you so you can put the book to good use, please ask.