680 likes | 772 Views
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….
E N D
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…
“Computers never do what you want them to do, only what you tell them to do.” Brian Patton Vice President, Robodyssey Systems
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.
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
Create a Project Open the BasicX software:
Create a Project Open the BasicX Editor:
Create a Project Make a New Project:
Create a Project Name the project and module “MyFirstApp” Save the project in the Robots folder on Drive C.
Enter your code here! Ready, Set, Code!
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
“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?
Oops! Increase the string length. From the menu: Options >> Environment >> Max String Length = 60 Now, here’s my program’s output:
Run it again! Press the Reset Button on the Motherboard to run the program again. Did the same message show up again?
More than Meets the Eye Turn off the power to the motherboard. Now turn it back on. Despite removing power to the BX-24, the program still resides on the chip. Try that with a PC! 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?
Printing to an LCD With 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.
Printing Numbers When 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.
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.
A Mathematical Genius Computers are calculating machines, so let’s do a little math. How about: Debug.Print CStr(2 + 2) or Debug.Print "2 + 2 = " & CStr(2 + 2) but not Debug.Print 2 + 2
Chapter 4: Some More Math Don’t feel obligated to enter all this code. 3.0 * 4.0 Debug.Print CStr(3.0 * 4.0) 6.0 / 12.0 Debug.Print CStr(6.0 / 12.0) Sqr(25.0) Debug.Print CStr(Sqr(25.0)) 2.0^8.0 Debug.Print CStr(2.0^8.0)
Say 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: Compiler Assistance Debug.Print CStr(Sqr(25)) The compiler will catch your mistake and give you some hints on how to fix it:
Chapter 7: Even More Math Don’t feel obligated to enter all this code. Exp(6.0) Debug.Print CStr(Exp(6.0)) Log10(100.0) Debug.Print CStr(Log10(100.0)) Log(Exp(1.0)) Debug.Print CStr(Log(Exp(1.0))) Cos(3.14159) Debug.Print CStr(Cos(3.14159))
Caveat Emptor If 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!
Chapter 5: Using Variables Just like in math class, we can create and utilize variables: Dim x as Single Dim y as Single Dim Answer as Single x = 5.0 y = -2.5 Answer = 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.
The monthly payment of long-term loans, such as home mortgages or car loans, is calculated with the formula where • 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, . Making Math Relevant Here’s one of many Challenge Problems in my book that students find interesting. You’ll find it on page 64. • 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.)
Making Math Relevant Here’s one way to answer this Challenge Problem: Dim LoanAmt as Single Dim n as Single ' Payment period in mo. Dim APR as Single ' Annual rate Dim i as Single ' Monthly rate Dim Payment as Single LoanAmt = 25000.0 ' $25,000 loan n = 60.0 ' 5-year payment APR = 0.073 ' APR = 7.3% i = APR/12.0 ' Calculte monthly rate Payment = LoanAmt*(i/(1.0-(1.0+i)^(-n))) Debug.Print "Payment = $" & CStr(Payment)
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.
Onboard LEDs The 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
Onboard LEDs Call PutPin(25, 0) Pin 25 is the red LED. Pin 26 is the Green LED. 0 turns it ON 1 turns it OFF 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.
Too Fast For You? Call PutPin(25, 0) ' Turn On RED Call PutPin(25, 1) ' Turn Off RED Call PutPin(26, 0) ' Turn On Green Call PutPin(26, 1) ' Turn Off Green Run this and see what you get. Are you surprised?
Chapter 3: Delays Sometimes, 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.1s Call Delay(1.0) ' Pauses for 1.0s Call Delay(10.0) ' Pauses for 10.0s
Back to the LEDs… Call PutPin(25, 0) ' Turn On RED Call Delay(1.0) Call PutPin(25, 1) ' Turn Off RED Call Delay(0.5) Call PutPin(26, 0) ' Turn On Green Call Delay(1.75) Call PutPin(26, 1) ' Turn Off Green Now that’s better!
Chapter 11: BX-24 & RAMB Pins As 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.
S S Chapter 11: RAMB Signal Pins 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. 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.
P P Chapter 11: RAMB Power Pins The 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.
G G 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 nevershort the ground pins to the power pins! Doing so will produce a huge electric current that may damage the microcontroller, RAMB, and batteries.
Ground Wire Darkest Lightest 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.
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 signalpin and the negative black lead is connected to the outermost groundpin.
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 buzzer Call Delay(0.3) Call PutPin(12, 0) ' Turn Off buzzer Call Delay(0.5) Call PutPin(12, 1) ' Turn On buzzer Call Delay(0.3) Call PutPin(12, 0) ' Turn Off buzzer
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.
Loops Many computer applications use structures called “loops” to repeatedly perform a task or tasks. For-to-Nextloops run for a finite number of times. Do-Loops can run forever.
Chapter 8: For-To-Next Loops Dim j as Integer Debug.Print "I can count fast!" For j = 1 to 5 Debug.Print CStr(j) Next
Multiple Beeps For j = 1 to 10 Call PutPin(12, 1) ' Turn On buzzer Call Delay(0.1) Call PutPin(12, 0) ' Turn Off buzzer Call Delay(0.1) Next
Chapter 9: Do-Loops Do Debug.Print "I can't stop!!!" Loop
Create Another Project • Make a New Project • Save it in the Robots folder on Drive C. • Name the Project and the Module “Sensors”
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. • 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: Debug.Print CStr(GetADC(13))
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?
Caveat Emptor If 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!
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.)
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) Loop End Sub