Hacking Roomba - Tod E.Kurt Part 8 ppt

30 256 0
Hacking Roomba - Tod E.Kurt Part 8 ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

192 Part II — Fun Things to Do The implementation is straightforward; the main hurdle is conceptual as you’re using Roomba driving data without commanding the robot to move. Instead of simply drawing lines as pixels onto the screen, an array of Line objects is created. Each Line holds an array of points that define the line. Each time draw() is called (deter- mined by framerate), the current line is added if the Spot button is being held down. Each press and release of the Spot button creates a new Line object and thus a new line to be drawn. Listing 10-1: RoombaSketch Line[] lines = new Line[numlines]; int l = 0; int strokeW = 5; void draw() { computeRoombaLocation(); // same as before parseRoombaSensors(); updateRoombaState(); background(180); stroke(0); for( int i=0; i<numlines; i++ ) lines[i].draw(); translate(rx,ry); rotate(rangle); image(rpic,-20,-20); } void parseRoombaSensors() { if( roombacomm.powerButton() ) { roombacomm.disconnect(); System.exit(0); } if( roombacomm.cleanButton() ) { rx = width/2; ry = height/2; rangle = 0; strokeW = 5; } if( roombacomm.bumpLeft() ) { strokeW ; if( strokeW<1 ) strokeW=1; } if( roombacomm.bumpRight() ) { strokeW++; if( strokeW>100 ) strokeW=100; } if( roombacomm.spotButton() ) { if( drawing ) { if( rx != rxo && ry != ryo ) lines[l].addPoint((int)rx,(int)ry,strokeW); } else { drawing = true; l++; l %= numlines; lines[l] = new Line(); 193 Chapter 10 — Using Roomba as an Input Device Listing 10-1 Continued } } else if( drawing ) drawing = false; } Figure 10-2 shows how you might hold Roomba and draw with it, while Figure 10-3 shows a drawing made with RoombaSketch. The ability to change the pen stroke width while drawing enables a much more fluid line than is possible with a normal mouse. You can create very organic drawings. Granted, as Figure 10-2 shows, using Roomba as a mouse requires a bit more physical movement than with a normal mouse, but with some people complaining that computer users don’t get enough exercise, you can now point to the Roomba and say, “That’s my mouse.” F IGURE 10-2: Using Roomba as a mouse 194 Part II — Fun Things to Do F IGURE 10-3: A drawing made with RoombaSketch Using Roomba as a Theremin The theremin is a strange and unique musical instrument. It’s played without even touching it; placing your hands in front of it in particular ways adjusts its pitch and loudness. The Beach Boys song “Good Vibrations,” old Star Trek episodes, and countless horror movies have used the theremin to good effect, so you have undoubtedly heard it. It produces a clear pure sine wave tone that glides between notes and sounds vaguely human-like. Invented in 1919 by Leon Theremin, the theremin was the result of research into electrostatic proximity sensors. Figure 10-4 shows the inventor playing his instrument. The theremin con- sists of two antennae, one for controlling pitch and one for controlling volume. The pitch antenna is usually vertical and on the right-hand side of the player. The circuitry inside the theremin measures the varying capacitance between the player’s body and the antennae and adjusts the loudness and pitch accordingly. If you’ve ever used your body to get better reception on your TV, you’ve experienced how the theremin works. Making a Theremin Out of Roomba The two Roomba dirt sensors are apparently capacitive-based and thus a likely candidate to use as a theremin input. Unfortunately, they do not seem to be tuned to picking up human-sized variations. But the other downward-facing sensors offer an alternative for the pitch control. By 195 Chapter 10 — Using Roomba as an Input Device tilting Roomba left and right you can get a few bits of resolution by combining the cliff sensors with the wheeldrop sensors. Figure 10-5 shows how Roomba can be used to detect variations in tilt. The scale of the tilt is a bit exaggerated in the diagram to demonstrate the effect. Since the theremin glides from one pitch to the next, having relative pitch adjustments via tilting actually works pretty well. F IGURE 10-4: Leon Theremin and his musical instrument F IGURE 10-5: Tilting Roomba for pitch control No sensors triggered Pitch change = 0 Left cliff sensor triggered Pitch change = +1 Left cliff & front left cliff sensors triggered Pitch change = +2 Left cliff & front left cliff & left wheeldrop triggered Pitch change = +3 196 Part II — Fun Things to Do Listing 10-2 shows the core of a Processing sketch called RoombaTheremin. As before, the draw() method calls parseRoombaSensors() and draws a growing vertical line showing pitch change. Figure 10-6 shows what the sketch looks like after running for a while and per- forming with it. Figure 10-7 shows the typical way you hold and use the robot when playing it with RoombaTheremin. Listing 10-2: RoombaTheremin int pitch = 70; int dur = 6; int pshift = 0; void draw() { parseRoombaSensors(); updateRoombaState(); stroke(0); strokeWeight(7); int x = pitch * 4; y = (y+1) % height; // keep going down, but loop at height if( y==0 ) { background(180); } // clear screen if back at top line( x,y, x,y+2); } void parseRoombaSensors() { if( roombacomm.cliffLeft() ) pshift++; if( roombacomm.cliffFrontLeft() ) pshift++; if( roombacomm.cliffRight() ) pshift ; if( roombacomm.cliffFrontRight() ) pshift ; if( roombacomm.wheelDropLeft() ) pshift++; if( roombacomm.wheelDropRight() ) pshift ; if( roombacomm.wheelDropLeft() && roombacomm.wheelDropRight() && roombacomm.wheelDropCenter() ) { ; // all wheels down, do nothing } else { // otherwise, play pitch += pshift; pshift=0; roombacomm.playNote(pitch, dur); } } 197 Chapter 10 — Using Roomba as an Input Device F IGURE 10-6: RoombaTheremin tracking tilt changes to modify pitch F IGURE 10-7: The three main positions when performing with RoombaTheremin Better Sound with the Ess Library The necessary time delays between each note sent to Roomba makes for a less-than-convincing theremin simulation. Instead of a smooth tone characteristic of a theremin, the best that can be done on Roomba is a repetitive beep-beep-beep that is limited by the time it takes the robot to execute the SONG and PLAY commands. An optional library for Processing called Ess makes it really easy to load, play, and manipulate sounds on your computer. It is built on JavaSound (the standard Java sound API), but is much 198 Part II — Fun Things to Do easier to use than JavaSound normally is. For example, the following snippet is a complete Processing sketch that loads an MP3 file, pitch shifts it up five semitones, and plays it. import krisker.Ess.*; Ess.start(this); AudioChannel myChannel = new AudioChannel(“somesong.mp3”); myChannel.filter(new PitchShift(Ess.calcShift(7))); myChannel.play(); You can use Ess instead of the playNote() command in RoombaComm. Granted then the sound is coming out of the computer instead of Roomba, but by using Ess’s pitch shifting abil- ity you can get a fairly smooth glissando from pitch to pitch. Ess is available at www.tree-axis.com/Ess/ and, like any Processing library, is installed by unzipping its downloaded zip file into the libraries folder of the main Processing applica- tion. After restarting Processing, Ess is visible when you use the Sketch ➪ Import Library menu command. Touchless Sensing Another way of triggering pitch changes that is perhaps more theremin-like is to flip Roomba over and use the cliff sensors as hand proximity sensors. Figure 10-8 shows how this works. By holding your hand over one or more of the cliff sensors, you un-detect a cliff. The cliff sensors are barrier sensors just like the wall sensor, but its logical sense is flipped and it’s called a cliff sensor instead of a floor sensor. It’s more logical (and marketable) to say cliff sensor instead of floor sensor. But flipped over and used as a theremin, these cliff sensors work well detecting hands, as long as your hand has its fingers together and is mostly parallel to the surface of Roomba. Listing 10-3 shows a variation of the previous code, called RoombaThereminToo. It includes the Ess library method of making sound and has a modified version of parseRoombaSensors() to deal with the fact that it should change pitch when not detecting a cliff. The other features of that method are that by pressing both wheels down, it mutes the audio. Pressing the bump sensor starts the audio back up again. You’ve probably noticed that parseRoombaSensors() doesn’t actually change the pitch. It sets the pshift variable but doesn’t act on it. That’s because changing the pitch of a sound while it’s playing causes an audible glitch. By waiting for the sound to finish playing through and changing its pitch before it loops again, the pitch transitions are smooth. In Ess, if the method channelLoop() is declared in your sketch, it will be called when a sound is finished playing. By adding that method and a check to see if pshift is actually set, the pitch (actu- ally the rate here since it’s a faster computation and yields the same result) can be changed seamlessly. 199 Chapter 10 — Using Roomba as an Input Device F IGURE 10-8: Alternate playing method, used in RoombaThereminToo Listing 10-3: RoombaThereminToo, Using Ess and Hand-Waving Channel mySoundA; void setup() { // other standard setup Ess.start(this); mySoundA=new Channel(); mySoundA.initBuffer(mySoundA.frames(250)); mySoundA.wave(Ess.SINE,960,.75); pshift=0; } void parseRoombaSensors() { if( !roombacomm.cliffLeft() ) pshift++; if( !roombacomm.cliffFrontLeft() ) pshift++; if( !roombacomm.cliffRight() ) pshift ; if( !roombacomm.cliffFrontRight() ) pshift ; if( !roombacomm.wheelDropLeft() ) pshift++; if( !roombacomm.wheelDropRight() ) pshift ; if( !roombacomm.wheelDropLeft() && !roombacomm.wheelDropRight() ) { pshift = 0; mySoundA.stop(); } else if( roombacomm.bump() ) // bump to start playing mySoundA.play(Ess.FOREVER); } void channelLoop(Channel ch) { if( pshift != 0 ) { mySoundA.rate(1.0 + pshift*0.01); pshift = 0; } } 200 Part II — Fun Things to Do Turning Roomba into an Alarm Clock Roomba is now so familiar that it’s almost like a pet. Why not have it sleep at your feet like a faithful dog? Except unlike a dog, with Roomba you can set the exact time when it will wake you up. And you can even make it turn on the radio for you. Listing 10-4 shows the basics of the Processing sketch RoombAlarmClock, an alarm clock implemented on Roomba. It has the following features: Ⅲ Alarm: Beep and make noise at a particular wakeup time. Ⅲ Snooze button: Must hit both the left and light bump sensors. Ⅲ Turn off alarm: Pick up Roomba and press the Power button. Ⅲ Turn on or off radio on iTunes: Press the Clean button to play or pause. The alarm and snooze times are determined by using Java Date and DateFormat objects. Alter the initial time for the alarm to go off (wakeupTime) and the length of the snooze (snoozeSecs) and run the sketch to start the alarm. When the alarm goes off, the method playAlarm() is called, which plays a tune and vibrates Roomba back-and-forth. Pressing both bumpers snoozes the alarm, while picking up Roomba and pressing the Power button turns off the alarm until the next day. The radio is turned on and off with the runRadioCmd() method. This method uses Runtime.exec() to execute a system command. The particular command shown uses osascript, the Mac OS X command-line tool for running Applescript statements. The Applescript statement tell app “iTunes” to playpause will tell iTunes to play if it’s paused or pause if it’s playing. So before using RoombAlarmClock, set iTunes to the playlist or Internet radio station you’d like to wake up with. Then, when it’s running, at any time press the Clean button to turn on the radio. For other operating systems, change the radioCmd variable to any command-line command that you fancy. You could even add additional commands that are triggered for the other but- tons or sensors. Perhaps the Max button runs a command that talks to an X-10 controller to turn on the lights. If you encapsulate these various commands into a shell script (or batch file on Windows), you can change it all you want without re-exporting the sketch in Processing. In Chapter 13 you’ll learn how to make Roomba fully autonomous. When fully stand-alone, having it be an alarm clock would be even more interesting. For example, it could run away from you as you try to turn off the alarm. If you use RoombAlarmClock to wake you up, don’t put Roomba on your nightstand table. It may very well fall off. Roomba is quite sturdy, but why take the chance? Place Roomba on the floor. 201 Chapter 10 — Using Roomba as an Input Device You probably want to use the simpler serial tether rather than the Bluetooth adapter for the alarm clock. Some computers may power down their Bluetooth interface if it’s idle. Regular serial ports don’t have that problem. Listing 10-4: RoombAlarmClock String wakeupTime = “6/20/06 07:48 am”; Date wakeupDate = null; boolean alarm = false; int snoozeSecs = 60 * 9; // nine minutes String radioCmd[] = {“osascript”, “-e”, “tell app \”iTunes\” to playpause”}; void setup() { framerate(1); roombacommSetup(); DateFormat df=DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT); try { wakeupDate = df.parse(wakeupTime); } catch( Exception e ) { println(“error:”+e); } println(“wakeupTime: “+wakeupDate); } void draw() { if( roombacomm == null ) return; parseRoombaSensors(); updateRoombaState(); Date now = new Date(); if( now.compareTo( wakeupDate ) > 0 ) playAlarm(); } void playAlarm() { alarm = true; println(“playAlarm”); int song[] = { 78,4, 77,4, 76,4, 75,4, 74,4, 73,4, 72,4, 71,4, 70,4, 69,4, 70,4, 71,4 }; roombacomm.createSong(5,song); roombacomm.playSong(5); // play rude song for( int i=0; i<5; i++ ) { // and shudder a little roombacomm.spinLeftAt(75); roombacomm.pause( 100 ); roombacomm.spinRightAt(75); roombacomm.pause( 100 ); roombacomm.stop(); } } void runRadioCmd() { try { Continued [...]... number 51262 Ⅲ 220-ohm resistor (red-red-brown color code), Jameco part number 107941 Ⅲ Two 1µF polarized electrolytic capacitors, Jameco part number 94160PS Ⅲ 8- pin header receptacle, Jameco part number 70754 Ⅲ Male snap-apart header, Jameco part number 16 088 1 Ⅲ General-purpose circuit board, Radio Shack part number 27 6-1 50 Ⅲ Four 0.01µF ceramic disc capacitors, Jameco part number 15229 Finally, if you... NetMedia part number SPTS And that’s it That isn’t a very elegant solution, so if you want to build your own Ethernet-toRoomba interface, the parts you need are: Ⅲ NetMedia SitePlayer Telnet Module, NetMedia part number SPT1 Ⅲ LF1S022 10base-T filter, NetMedia part number FIL0011F Ⅲ Mini-DIN 8- pin cable, Jameco part number 10604 Ⅲ 780 5 +5 VDC voltage regulator, Jameco part number 51262 Ⅲ 220-ohm resistor... router is at 192.1 68. 1.1, then pinging it looks like: % ping 192.1 68. 1.1 PING 192.1 68. 1.1 (192.1 68. 1.1): 56 data bytes 64 bytes from 192.1 68. 1.1: icmp_seq=0 ttl=64 time=2.125 ms 64 bytes from 192.1 68. 1.1: icmp_seq=1 ttl=64 time=2.012 ms 64 bytes from 192.1 68. 1.1: icmp_seq=2 ttl=64 time=1. 787 ms ^C - 192.1 68. 1.1 ping statistics 3 packets transmitted, 3 packets received, 0% packet loss round-trip min/avg/max/stddev... regulator and taps the Roomba battery in the standard way via the Mini-DIN 8- pin plug FIGURE 1 1-1 3: SitePlayer Telnet Roomba adapter hooked up to Roomba If you know how to write network programs, you can immediately start sending ROI commands to Roomba by connecting to the telnet port (port 23) on the SitePlayer Telnet As a quick experiment, try sending the byte sequence 0x80,0x82,0x86 (with 100msec pauses... useful results Even those that produce non-optimal results are fun and instructive More Complex Interfacing part in this part Chapter 11 Connecting Roomba to the Internet Chapter 12 Going Wireless with Wi-Fi Chapter 13 Giving Roomba a New Brain and Senses Chapter 14 Putting Linux on Roomba Chapter 15 RoombaCam: Giving Roomba Eyes Chapter 16 Other Projects Connecting Roomba to the Internet chapter T he objects... 4.03 ( http://www.insecure.org/nmap/ ) at i 200 6-0 6-2 8 11:42 PDT Continued 217 2 18 Part III — More Complex Interfacing Debugging Network Devices Continued Host gw.home (192.1 68. 0.1) appears to be up MAC Address: 00:06:29:15:F3:13 (IBM) Host openwrt.home (192.1 68. 0 .8) appears to be up MAC Address: 00:16:B6:DA:91:2F (Cisco-Linksys) Host nasty.home (192.1 68. 0.9) appears to be up MAC Address: 00:0D:A2:01:04:70... the Roomba) , it’s wasteful Figure 1 1-5 shows a schematic based on the box design, but simplified The circuit consists of really just the SitePlayer Telnet module, the RJ-45 jack, a voltage regulator, and a Mini-DIN 8- pin cable The RJ-45 jack needs a few resistors and capacitors to help filter out any noise picked up on the Ethernet cable, and that’s about it RJ-45 U1 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1... — Connecting Roomba to the Internet Bluetooth module For the cable, solder header pins in a way that makes the most sense to you Figure 1 1 -8 shows one possible wiring of such a cable For that cable, the power and ground pins are separated by one pin spacing from the data pins FIGURE 1 1-7 : RJ-45 jack with the bent pins for breadboard insertion FIGURE 1 1 -8 : Roomba prototyping plug 213 214 Part III — More... minimi.home (192.1 68. 0.25) appears to be up MAC Address: 00:11:24:77 :84 :FA (Apple Computer) Host 192.1 68. 0.134 appears to be up Host 192.1 68. 0.136 appears to be up MAC Address: 00:11:24:3F:3A:50 (Apple Computer) Host 192.1 68. 0.144 appears to be up MAC Address: 00:13:10:3A:16:17 (Cisco-Linksys) Host 192.1 68. 0.1 48 appears to be up MAC Address: 00:30:65:06:63:67 (Apple Computer) Host 192.1 68. 0.149 appears... which is a 10base-T filter and RJ-45 jack 209 210 Part III — More Complex Interfacing FIGURE 1 1-3 : SitePlayer Telnet System box FIGURE 1 1-4 : SitePlayer Telnet System internals Chapter 11 — Connecting Roomba to the Internet Creating Your Own SitePlayer Telnet Roomba Adapter The SitePlayer Telnet box is very nice, but somewhat expensive And if you don’t need true RS-232 but instead need 0-5 V TTL serial . ) runRadioCmd(); } Summary Even though the Roomba s sensors are primitive, they can be put to some interesting uses. These uses need not be vacuum-related or even robotics-related. The cliff sensors are one of the best. 1 1-1 shows a few of these embedded Ethernet devices designed to add TCP/IP and Ethernet capability to an existing device. The two devices focused on in this chapter are the SitePlayer Telnet. becomes more effi- cient or useful when a higher percentage of the group participates. We do not yet live in a world where a large percentage of the objects in our lives are smart and networked,

Ngày đăng: 10/08/2014, 04:21

Tài liệu cùng người dùng

Tài liệu liên quan