Showing posts with label robotics. Show all posts
Showing posts with label robotics. Show all posts

Thursday, June 3, 2010

Displaying messages

;here is a simple word
to sayHi

display-bits $00 $00 $76 $06
end

;this sub-procedure is required whenever you use the display-bits command
to display-bits :d1 :d2 :d3 :d4
bsend $110
bsend 64
bsend low-byte :d1
bsend low-byte :d2
bsend low-byte :d3
bsend low-byte :d4
end

Basic Sender and Receiver Procedures

For communicating between crickets:


to send-it

send 1
wait 10
send-it
end

to receive-it
waituntil [newir?]
if ir = 1 [beep]
receive-it
end

Using a Variable to Count

Until now you've had to write each new number you want the LED to display as it counts to 10. If you wanted it to count higher this would start to get tedious. To avoid the tedium you can use a variable. It is similar to a variable in algebra in that you use a name that stands for a value that can be changed.

Here is how:

global [number] ;this creates a variable and names it

to count

setnumber 1 ;this sets the value of the variable
repeat 10 [
display number ;this displays the current value of the variable
wait 5
setnumber number + 1 ;this sets the value again, adding one to the current value
]
end

What is tail recursion?

Tail recursion is another way to loop commands. With loop or repeat you give the command and then in brackets specify what is to be repeated. Tail recursion is simpler and a bit of a trick. Here's how:

to count

display 1
wait 5
display 2
wait 5
display 3
wait 5
count
end

See? If you put the name of the procedure at the end of the procedure itself, it will just keep looping itself!

Two Ways to Loop

Two Ways to Loop

There are two commands that can make other commands repeat.
Loop makes the commands repeat indefinitely:

to beepForever
loop [beep wait 2]
end

Repeat makes the commands repeat as many times as you tell it to:

to beepFiveTimes
repeat 5 [beep wait 2]
end

What if you want the LED display to not display anything?

Problem

The LED display will automatically turn off after a little while if it's idle to conserve battery power. But how can you make it go blank right away?

Solution

You have to use a procedure called "display-bits." Here it is:
to display-bits :d1 :d2 :d3 :d4
bsend $110
bsend 64
bsend low-byte :d1
bsend low-byte :d2
bsend low-byte :d3
bsend low-byte :d4
end

Bits are the individual bars that can light up in each of the four spaces. D1, d2, d3, and d4 stand for the four spaces. To make all of the bits turn off, you would send the command "display-bits $00 $00 $00 $00."

Applying the solution

If you only sent this command, the Cricket wouldn't know how to "display-bits," so here is an example of how you use this command. Notice that you have to include the "display-bits" procedure because it's acting as a sub-procedure to "count." The "count" procedure will make it count 1, 2, then go blank.
to count
display 1
wait 5
display 2
wait 5
display-bits $00 $00 $00 $00
end

to display-bits :d1 :d2 :d3 :d4

bsend $110
bsend 64
bsend low-byte :d1
bsend low-byte :d2
bsend low-byte :d3
bsend low-byte :d4
end

To Display Numbers

The command that makes the LED display numbers is "display".
Before the Cricket can access this command you will probably have to do the following:

  1. Make sure the procedures window ("Cricket Logo Programs") is empty.
  2. Click the download button to clear out the Cricket's memory.
  3. Now type "display 1234" in the command center and hit enter.
  4. If the LED display shows the number 1234 it is now working.

What is a conditional statement?

What is a conditional statement?
It's when you tell your robot to do something only if a certain condition is true. If the condition is not true, the robot won't do it.
We use conditional statements all the time in real life, like when we say, for example, "If they are serving fish for lunch, I'm going to the salad bar." 

In Logo, this would look something like this:

to eat.lunch
if fish
[eat saladbar]
end
(Note: I don't have anything against fish.)
Your robot can't eat, though, so let's see what it could do as a result of a conditional statement.

If you want your robot to start driving if you press a switch, this will do it:

to drive
if switcha
[ab, onfor 20]
end
Notice where the brackets go! They enclose the 'if true' command like bread on a sandwich.
If you want your robot to stop driving if you press a switch, this will do it:

to stop.driving
ab, on
if switcha
[ab, off]
end
Well, actually, I kind of lied. The two examples above won't really work because they check if the switch is pressed only once. We need it to check continuously so it will carry out the commands whenever we press the switch. So we have to "loop" the procedure, like this:

to drive

loop [
if switcha
[ab, onfor 20]
]
end

This example will keep checking whether the switch is pressed because loop makes the part inside the brackets repeat indefinitely.

Here's how you can use a switch to control your robot:
Use a switch to start your robot driving on its road course. First plug a switch into sensor port 'a'. 

You can keep your program the way it is and just wrap a conditional statement around it.
to start
loop [
if switcha
[drive]
]
end

to drive
ab, thisway
ab, on
wait 20
etc, etc,
end

Now be sure to put the name 'start' in the 'run this' window so when you press the run button on the robot, it will start the loop instead of just skipping the loop and going straight to the 'drive' procedure.

Using Sub-procedures

Sub-procedures are separate procedures in your program that are called upon in the main procedure. Here is what one looks like:


to drive
ab, thisway
ab, on
wait 20
turnright ;sub-procedure is being called upon here
wait 30
end

to turnright ;here is the sub-procedure
b, off
wait 10
b, on
end

So "turnright" is a sub-procedure of "drive." "Drive" is the main procedure. In other programming languages a sub-procedure is called a function but it basically works the same.

The benefit of using a sub-procedure is that it makes your program much more efficient and can save you a lot of work.

How will you use this in your program? 
In your procedures window, add a procedure called "turnright" and type in the commands that will make your car turn right. Do the same to add another procedure called "turnleft." In your main procedure every time there is a right turn, remove those commands and just type "turnright." Do the same for left turns. You will have to test your program to make sure the turnright and turnleft sub-procedures turn the correct amount, but all of the turns are right angles, so if it works once it should work for the rest.

Your Robot Follows Commands in a Sequence

How is this:

to drive
beep
ab, on
wait 30
ab, off
end
different from this?
to drive
ab, on
wait 30
ab, off
beep
end
The first procedure beeps, turns motors a and b on, waits 3 seconds, and turns motors a and b off. The second procedure beeps after the motors turn on and off.

What is the rule the Cricket is following?
The Cricket will follow the sequence of commands from the top of the procedure to the bottom.

Monday, December 21, 2009

Switch-Operated Elevator

Two of my 9th grade students created this elevator. It raises up at the press of a switch, then waits for another press before descending. The motors also gear down for increased torque.

Saturday, December 12, 2009

Ways to Program a Loop

If you need a switch to work repeatedly when you run its program, you need to loop it. Here are some ways depending on exactly how you want them to work:
;this strategy repeats indefinitely
to main
loop
[commands to loop]
end

;this strategy makes the program run itself indefinitely
to main
commands to loop
main
end

;this strategy repeats the commands many times
to main
repeat 100
[commands to loop]
end

Many Ways to Program Switches

;simplest way, one switch
to go
waituntil [switcha = 1]
motor commands
waituntil [switcha = 1]
more motor commands
end

;another way, one switch
to go
if switcha = 1
[motor commands]
if switcha = 0
[more motor commands]
end

;another way, one switch
to go
ifelse switcha = 1
[motor commands]
[more motor commands]
end

;two switches!
to go
if switcha = 1
[motor commands]
if switchb = 1
[more motor commands]
end

Tuesday, November 17, 2009

Programming a Switch

I've been working with RobotC for a bit now and I've come to realize something about the way switches can be programmed in Logo. Logo allows you to write this implied boolean equation with if switcha [a, on] or if not swticha [a, off], while boolean equations are true or false or 0 or 1 in other programming languages. So I tried programming a switch with if switcha = 1 [a, on] and it worked just as well. I much prefer that approach because of its similarity to most other programming languages.

Learning Through Discovery


One of my 9th grade students was making an important discovery, an instance of self-guided learning with the cricket logo console. She had 3 separate procedures that controlled different aspects of her dj robot--one spun the turntables, one set the dj's arms in motion, and one displayed a message on the LED display. She hadn't been taught how to crate a main procedure that calls all of the other three, so she was typing the name of each in the command center and beaming them to the cricket in turn to make them start up individually. I thought that was a very inventive way to get it to do all three of its procedures without knowing the 'right' way. Of course when I showed her the usefulness of a main procedure she got it right away because it was immediately useful.

Monday, July 27, 2009

Do It Yourself video

Here is my final (for now) version of my technology video. It focuses on encouraging my students to experiment with technology, not to be afraid to break it.

Thursday, July 16, 2009

Building Better Robots Podcast No 1


Welcome to the first episode of the Building Better Robots podcast. This podcast is designed to supplement my 9th grade robotics class in which we are building and programming Super Cricket Robots with Legos and the Logo programming language. But even if you're not taking the class, you might get some good tips out of it. In this episode I'll be talking about how to get the most out of this podcast and I'll introduce some of the specific topics coming up.

Thanks to Flickr user Brandy Shaun for the Lego photo, Flickr user Steve Kay for the Optimus Prime photo, and the music is brought to you by Elysis Raudenbausch, Breakdown Easy, available on Wikimedia Commons.

Sunday, January 11, 2009

Giving up on Jackal

My 9th grade robotics class has really liked the syntax coloring Jackal provides for our cricket microcomputers. But after a lot of frustration I've abandoned Jackal in favor of Cricket Logo. CL is much more stable, especially transmitting data to the cricket. We now get very few errors, and this was driving my students crazy with Jackal. As a result we're really working on properly indenting programs as a way to make them more readable.

Wednesday, August 13, 2008

Syntax coloring for Logo


As far as I know, this is only available in one place--the Java-based Jackal for Cricket microcomputers. Now that MicroWorlds EX Robotics supports the Cricket this is the only reason I'm sticking with Jackal for my 9th graders this year. I think colored syntax is a really important feature of any programmer's software toolkit and an essential type of feedback for someone beginning to learn to program in a text-based environment.

MicroWorlds ver 1.7 for Windows


I just installed the upgrade to MicroWorlds EX Robotics for Windows, version 1.7, and was thrilled to see not just support for the NXT but also the Cricket! I can't believe how much of an upgrade this is. And the help resources are robot-specific, so if you've checked the NXT, for example, and restarted the program, you'll see Robotics resources only for the NXT. One things I've learned from this is that different variations of Logo aren't dependent on the programs that you use it with so much as the robotic hardware you use it with, or maybe it's both.