Learning to code Part 4
Transcription
Learning to code Part 4
CCITE Creating fruitful and sustainable links between innovative organisations committed to the improvement of technological education for young people. The Centre for Innovation in Technological Education in Cambridge http://ccite.org Learning to code - with a purpose Part 4 Adrian Oldknow March 2015 [email protected] 15. K’Nex and control: In part 1 I showed some Technical LEGO being used as a vehicle for the Crumble controller. In part 3 I showed a picture of my TI graphical calculator used to control a Mars buggy made from Meccano. The popular K'Nex construction kits make a cheaper alternative from which to build the chassis for robot vehicles. A good source of these is the UK branch of the K’Nex User Group: http://www.knexusergroup.org.uk/acatalog/knex-shop.html?region=uk. Their starter kit for control costs £143 and comes with a cut down version of the Data Harvest `FlowGo’ system called `Learn&Go’ together with 2 motors, 2 LEDs and a buzzer. What you don’t get are any inputs, or any form of computer interaction. So all you can do is control the outputs, and program them with button pushes like the Mindsets IQ4 in section 12 of Part 2. Compared with LEGO WeDo this is rather a disappointing introduction. The fuller version costs £239 and includes inputs and computer control via FlowGo: http://www.knexusergroup.org.uk/acatalog/info_K79017PK.html. A budget alternative is to buy the very cheap `K’Nex Dancing Robot’ kit which comes with the 2 motors you need, but no controller. Then you can choose your own controller, such as Crumble or PICAXE and provide your own power source such as: http://uk.farnell.com/pro-power/bh242b/holder-battery-4xc-snap-term/dp/1183120. Or maybe try a sweet surprise! 16. gPiO board for the Raspberry Pi – very hot from the press! http://gpio.co.uk/. This is a very user-friendly and powerful computer sense-and-control designed specifically to support programming in Python and Scratch with the GPIO pins of a Raspberry Pi costing £74 inc P&P. It comes with a connector cable and plug which fits over the pins of a Raspberry Pi and a micro-SD card with the ScratchGPIO software and examples already installed. You can run the standard `Blinky’ test program to flash any on-board LED. But, as with Crumble, 1 once you plug in an external power source, such as from a 6V battery box, then you can power external outputs including the motors, buzzer and LED units from the K’Nex packs above. So all you need to do now is to choose your favourite inputs and outputs from whatever kits you have acquired (such as a pair of the K’Nex or LEGO WeDo or Crumble motors), get yourself an external power supply for the Raspberry Pi and build yourself a chassis in Meccano, LEGO, K’Nex or make one from scratch to create a Raspberry Pi controlled autonomous vehicle. To use the `Motor’ ports on the side of the unit you will need to use the `Scratch GPIO5+’ software supplied on the SD card. The instructions are at: http://resources.gpio.co.uk/q-a/. Look at the answer to the `How do I use the Output Circuits?’ question. A suitable power supply is: http://www.maplin.co.uk/p/maplin-portable-power-bank-10000mah-n95lh. Can you do better than this: http://www.botmag.com/themars-exploration-rover-kit/? There are plenty of sample programs to try at: http://resources.gpio.co.uk/ as well as videos such as: https://www.youtube.com/watch?v=E4gT2u6CBnY&feature=youtu.be . 17. Raspberry Pi toppings aka `HATS’ – watch this SPACE! As we have seen there are many low cost devices designed to plug into the GPIO pins to make gadgetting simpler. With the advent of the B+ version with 4 USB ports and more GPIO pins, the Raspberry Pi Foundation has come up with a specification of `Hardware Attached on Top’ aka HAT boards: http://www.raspberrypi.org/introducing-raspberry-pi-hats/. The first of these has been designed to support a new competition for schools (primary and secondary) to design experiments for the British astronaut, Tim Peake, to take with him later this year when he goes on board the International Space Station! The competition is called Astro Pi: http://www.astro-pi.org/. 18. LEGO revisited – graphical programming with WeDo In Section 4 of Part 1 I mentioned the LEGO WeDo system which can be used together with Scratch to control the output (motor) and respond to the inputs (motion and tilt sensors). I also mentioned that you can also program it (at a price) with LEGO’s `WeDo Software’ which is based on the internationally used `LabVIEW’ system designed by National Instruments: http://www.ni.com/labview/. I confess that I found the WeDo software quite hard to understand at first – but my 11 year old grandson took to it like a duck to water! It is very visual and offers no apparent help or instructions – which avoids the need to have to produce different language versions! Here is an example of a very simple program which just counts. 2 Icons are dragged from the menu at the bottom of the screen and placed in a horizontal line from left to right. The first icon in the main block of a Project is the yellow Start icon. The red second icon used is one to display a message or number on the little display screen – in this case an `empty’ message to clear the display. The red third icon adds a 0 to the display. The fourth yellow icon is a loop structure. With a number box dragged below the end part you can make this a counted loop – equivalent to `Repeat 10’. Without the number it will be a `Forever’ loop. The first icon inside the loop is a yellow command to wait 1 second (I think the units are 1/10 second). The next red icon adds 1 to the number on the display. You run the program by clicking on the green arrow on the first icon. You can interrupt the program by clicking on the red arrow on the right edge of the screen. What would the same program look like (a) in Scratch, (b) in a flowchart or (c) in Python? You can find out more in the `User Guide’: https://c10645061.ssl.cf2.rackcdn.com/resources/wedo%20user%27s%20guide.pdf. I am running the program with a LEGO WeDo USB block, motor and motion sensor attached – which is shown in the top left-hand corner of the display above. So let’s see how we can interact with these devices. First we’ll try controlling the motor. Output icons are in green. We control the speed of the motor with the first green block, and then switch it off with next. Then we have a forever loop which waits 2 seconds, then turns the motor on for 2.5 seconds, then waits for 2 seconds, then reverses the motor and runs it for 2.5 seconds before repeating the process. Can you imagine how a motor-driven buggy would be moving? Now we can exert (limited) control by using the motion, or tilt, sensor. The first green block in the loop tells the motor to run not for a given time but until the motion sensor detects something in its path. Then it will stop, give a little chirp. Then wait a second and if the path is now clear it will get moving again until it encounters another object. Of course we have quite a few constraints with this system. Physically it is not autonomous and so we can only move as far as the lead to the USB block allows. Secondly we cannot fine-tune the motion sensor as we can in Scratch. With this software it just acts like a bump-switch returning the values “not close” or “too close”. But now we have met the notion of graphical programming we can see how LEGO have built on it to control their more sophisticated Mindstorms devices. 19. LEGO Mindstorms NXT 2.0: The first model I bought was called Mindstorms NXT 2.0 – as it was the `must have’ toy at that time, 2009 – albeit with a price tag around £250. The name `Mindstorms’ was the title of a seminal book by Seymour Papert, the father of the LOGO programming language, published in 1980: http://www.amazon.co.uk/Mindstorms-Children-Computers-PowerfulIdeas/dp/0465046746. The hardware and software roots of the Mindstorms Robotics Invention System kit go back to the programmable `brick’ created at the MIT Media Lab. This brick was programmed in Brick Logo. The original Mindstorms Robotics 3 Invention System kit contained two motors, two touch sensors, and one light sensor. The first NXT version had three servo motors and one light, sound, and distance as well as 1 touch sensor. The NXT 2.0 came with 3 motors, 2 touch sensors, a light sensor and distance sensor. You can see an example of my own efforts with NXT 2.0 at: https://www.youtube.com/watch?v=-NMzLA_Ib7k&feature=youtu.be The NXT-G programming language uses a very flexible graphical interface with boxes along the same lines as WeDo. We will build up the program used for the simple `Mars crawler’ simulation in the video. When you start a new program a new tab appears above a new worksheet containing just a single small icon called the `starting point’. Everything connected to the starting point will be loaded onto the NXT `block’ when you use the `Controller’ pad to `Download’. The controller pad is at the bottom left of the worksheet. You build up a program by dragging an icon from one of the tool menus at the left-hand side. We will start by using the `Common’ blocks menu, and clicking on the `Wait’ (hour-glass) icon. This opens a choice bar for the various sensors: time (clock), touch, light, sound, distance and colour. Select the colour sensor and drag it next to the starting point. Click on the icon and it opens a dialog area at the bottom left of the screen. I have selected the Control as a Sensor, and the Sensor as a Color Sensor. I have specified that it will be attached to input port 4 of the brick. As we’ll be waiting for the simulated Sun to rise over Mars we don’t need to check for any particular colour – just for enough light. Use the slider to select the threshold value on a scale from 0 (dark) to 100 (full sunlight). Check the `Light’ button rather than the red, green or blue ones. When we have enough light we’ll be moving forward checking out what lies ahead! But first we’ll give a wake-up call by adding a Sound block. We can choose a built-in sound file or record our own. I have selected the `Goodmorning’ file and set the volume level at 75. Drag the Sound block onto the program line. Now we need a `Repeat move until object sensed’ block. Select the `Loop’ tool and click on the 4 loop block to open up its dialog box. Select `Sensor’ as the `Control’ and `Ultrasonic sensor’ as the `Sensor’. Select input port 4, the units of measurement as Centimeters and drag the slider (or type the number in) to set the threshold distance as 52cms. In order to put the filling into the sandwich we need to use the `Move’ tools and the `Motor’ block. Open its dialog box and click on the ports to which you will have the motors attached and which you want to run – in my case A, B and C. Select the Directions you want to apply – in my case all going forward. Select the power level as 36 and the duration as `Unlimited’. Add the block between the jaws of the loop block. Now we have the first phase complete. We won’t continue in the same detail, but just look at the completed program. The repeat loop drives all 3 motors forward until the distance sensor picks up something within 52 cms. Then the motors are stopped, another sampled sound is played and the blue light is flashed. After 5 seconds the motors are run in reverse for 3 seconds and finally the light is switched off and a final sound is played. I think you’ll agree that this is a pretty impressive environment for coding a quite complex set of instructions. Now we’ve developed the software it’s time to build and connect the hardware before putting it to the test! The image shows the stripped down block with the three motors attached to output ports A, B and C. The sensors shown connected to output ports 1, 2, 3 and 4 are touch, sound, light and distance. For the `Mars crawler’ we do not need to use ports 1 or 2. As well as the 3 outputs and 2 inputs we also have a USB cable connected to the computer. 5 Now is the time to use `Save as…’ to give your program a name and to store your program on the computer before using the Controller in the NXT-G software to Download the NXT file to the brick with the USB cable. A downloadable version of the software is available from: http://service.lego.com/enus/helptopics/products/themes/mindstorms/mindstorms-nxt/nxt-software-download . The LEGO Mindstorms NXT 2 kit made a significant step forward in user-friendly practical computing, but at quite a high price. A quick trawl round e-Bay, Amazon etc. shows that this version is now out of stock and that there are very few cheap bargains are to be had. So if you are starting from scratch and have the cash then the next step, the EV3, is the one for you. 20. LEGO Mindstorms EV3: I bought the retail kit 31313: http://www.lego.com/en-us/mindstorms/products/31313mindstorms-ev3 which usually retails at £300, but can be picked up for c£250. Both the hardware and the software have been redesigned. Here is the same program we just constructed for the NXT 2.0 in the new EV3 Home Edition software. There are now 6 colour-coded block menus along the bottom instead of 3 along the left-side. The green menu shown provides the output bricks e.g. for motor control. The two `repeat x until y’ loops have now been given names. The first loop has no filling, so it just stays waiting until the value read by the light sensor is greater than or equal to 50. Instead of opening a dialog box to enter choices e.g. for the termination condition of the first loop, the parameters are entered through drop down boxes. Here we set up the first loop to be controlled by the `Color sensor’ by reading its ambient light intensity and comparing it with a threshold value of 50. The `Good morning’ message for the `Sound’ block was recorded using the `Sound Editor’ from the `Tools’ menu. The only difference between the ways the NXR 2.0 and the EV3 versions work is the way the light is flashed when the distance sensor detects the 6 presence of something in its path. This time we have control over the coloured LEDs in the brick’s own console. Once the Sound block has given the `a martian’ message, the first Brick status block starts the green LEDs pulsing before the motors run in reverse for 5 seconds. The second Brick status block turns the green LED off before the Sound block gives the `Good night’ message. My tame technology tester, aka Alexander (age 12), took this a lot further forward in the new EV3 version where he has very quickly discovered how to link the boxes in parallel to carry out parallel processing! On the hardware front there are significant improvements including built-in Bluetooth communication, a mini-SD memory card slot and a USB port to which a Wi-Fi dongle could be connected. I have set my unit up on a static test-rig so I can easily test the various inputs and outputs. I am also using a special connecting brick to allow me to use probes from the Vernier data-logging collection – the one here is a temperature probe. If you have the Educational version of the EV3 software there is also extensive support for using the kit as a sensing device for data-capture. I haven’t done it yet, but we should be able to put an Ev3 brick in the fridge with a light and temperature probe and check what happens when you shut the fridge door! Does the light go off? Does the temperature fall? What happens when you open the door?... For both the NXT 2.0 and EV3 it is very helpful to have an ideas book to help you explore new ideas. I have `The Unofficial LEGO Mindstorms NXT Inventor’s Guide’ by David J. Perdue: http://www.amazon.co.uk/Unofficial-LEGOMINDSTORMS-Inventors-Guide/dp/1593272154 and `The LEGO Mindstorms EV3 Idea Book‘ by Yoshohito Isogawa: http://www.amazon.co.uk/dp/1593276001/ref=pe_385721_51767431_TE_dp_1 . 21. LEGO Mindstorms, Scratch and Bluetooth In the course of this mission to learn more about the state of the art in physical computer I have made contact with the designers of systems like Crumble and PICAXE Microbot. In section 10 of Part 2 we met the clever S2P system from Revolution Education which hooks up PICAXE projects to Scratch 2. So I was delighted to discover the support Revolution is giving to schools around Bath using hardware such as LEGO Mindstorms. S2Bot is the free tool for connecting other kit, equivalent to S2P (Scratch to PICAXE): http://www.picaxe.com/Teaching/Other-Software/ScratchHelper-Apps/. 7 First you need to ensure the EV3 is not connected to any other software, as its Bluetooth option turned on, and is set to be recognised by other devices. Then you need to pair your computer’s Bluetooth (internal or external) with the EV3 by sending and receiving the `1234’ code. When communication is established, use Device Manager, or equivalent, to find which COM Port your EV3 connection is made through. Mine is COM 41. Once you have established Bluetooth connection with NXT 2.0 or EV3 you can run S2Bot and open the connection ready to use with Scratch. Select EV3 as the Target and COM41, or whatever your computer uses, as the Bluetooth Port. Click on the Connect button and with luck the EV3 will be connected. The panel show which sensors are connected to the input ports 1, 2, 3 and 4 on the brick. The Test pop-up allows you test that commands can be sent to the brick as well. Now you can open Scratch2 and the open the corresponding template such as `EV3 template.sb2’ and look under `More Blocks’ – lo and behold there are now a complete set of tools to read data from the sensors attached to any of the 4 input ports, to send output to 3 motor ports and to the brick’s sound and light systems. So here is the Scratch equivalent of the `Mars Rover’ program for EV3. I have also taken the opportunity to show off the use of a couple of procedure (sub-programs) to make the Scratch program fit in a single edit window without needing to scroll! Also note the way we have built a timed loop using the `timer’ blocks from the `Sensing’ menu. I used Scratch’s Sound Editor to record and save the 3 sound files for the messages – which are played back on the computer. It is also possible to play `rsf’ sound files recorded in the LEGO EV3 software and uploaded to the brick – but I haven’t made that work yet! Can you adapt the `Distance Time Graph’ we wrote in Scratch for the LEGO WeDo distance sensor to use any of the EV3’s sensors for data-logging? If you have of any of the Vernier sensors you can plug them into the EV3 or NXT using an adaptor block costing £45: http://www.inds.co.uk/engineering/lego_nxt.htm. Adrian Oldknow [email protected] 30th March 2015 8