Hi everybody, this week we changed from preprogrammed automated robots to remote-controlled robots. We converted our simple Pygame script from last week which controlled a small rectangle on our screens so it could control our robots.
As Pygame needs a screen to operate we had to get VNC going before we could start, I think we had it working for most people by the time the session ended. VNC will be a hugh advantage to us as we start to write more code as we can use a mouse and copy and paste with a GUI text editor instead of Nano on Putty.
We also added a webcam to our robots which worked well, we would probably need to work on mounting them properly if we wanted to use them for real life applications.
Here is a video of them in action.
I will go over the code again next week, if you want to take a look at the code check out pygame_robot.py and robot_cam.py here.
We will leave robots for a while and go back to basic electronics for the next few weeks.
Sorry for the delay in posting, I’ve been very busy this week. Last Saturday we started the session by testing our robots on an obstacle course. Here is a video of them in action.
Then we had a quick look at Pygame which we will use to control our robots this week. If you missed last week don’t worry as I will do a recap tomorrow. Meanwhile here is a very good Pygame tutorial. Here are my slides from last week pygame-101.
This week we worked on the function calls for our robot, however as most of the robots were low on battery power we didn’t get a chance to test them properly. I will have new batteries when we return on November 5th and we’ll spend a while running through the obstacle course. You can find code with the finished function calls on Dropbox.
We also talked about The Future Maker Awards which is a competition being run by CoderDojo and Liberty Global. I mentioned that you can use video as part of your entry this is an example of a video from Google Science Fair.
Here are my notes from last week obstacle-course.
See you all after the break. Happy Halloween.
This week we created our own folders on our Raspberry Pi3s, this will allow us to each find our own code and keep working on it.
We finished off the code for our own robot functions which we will use to navigate an obstacle course next week. The finished code is available on Dropbox here. These are my slides from Saturday finished-functions.
After we are finished with the obstacle course we will look at controlling our robot from our keyboard. I will use Pygame to do this and as Pygame is used for Graphical applications such as games and GUIs we will have to use VNC to connect to our Pi3s. Pygame will also allow us to use a webcam to capture video so if you have an old USB webcam please bring it in.
CoderDojo and Liberty Global are due to launch the Future Maker Awards this week FAQ here and more information here scroll down to see how one of our Bodgers uses his super power. I’ll have more information next Saturday after the launch.
See you then.
Our WiFi set up worked much better this week with an issue with only one of our routers which decided to give up half way through the session.
We started to write our own functions today, functions are very important in programming as they allow us to run the same code multiple times without rewriting the code several times. Instead, you can put that code inside a function and call the function several times. This has the added benefit that if the function’s code has a mistake, you only have one place in the program to fix it.
If we use names that relate to our function’s operation it will make our code much easier to understand.
Functions make designing and testing bigger programs much easier as we can break the project down to manageable chunks and we can write and test these functions individually.
As we saw last week the GPIO Zero library contains robot functions which turn our robot’s wheels forwards or backwards or in opposite directions to go left or right until we call the robot.stop() function. We are going to take these functions and the sleep() function and put them in functions that will make our robot go forward or backwards by a specified amount of Centimetres or will turn it left or right by an amount of degrees. This week we wrote a function that makes our robot go forward. See code here. Here are this weeks slides writing-functions . Next week we will write the rest of our functions.
Some Bodgers and their parents have been asking about buying Raspberry Pis but I would advise holding off until we decide on our projects as some people may end up using Arduinos or Raspberry Pi Zeros depending on their project.
However if you want to get one to experiment at home with I would recommend the following sites. https://shop.pimoroni.com/ and https://thepihut.com/ for Raspberry Pi and accessories. If you are buying a case for your Pi I would recommend Pibow Coupé from Pimoroni as the GPIO pins are numbered and easy to get at. For electronic components such as sensors etc. http://www.bitsbox.co.uk/ are very good, they also do cheap Arduino clones. Avoid starter kits as you should be able to get your hands on stuff like keyboards if you ask friends and relations for them, do get SD cards as you can have different set ups on different cards e.g. Retropi, Kodi.
See you all next week.
We are still having network difficulties with WiFi on our Raspberry Pi3s but I will work on finding a solution or a work around during the week.
We managed to connect to two of our robots and we wrote a simple hello world script and a script to control our robots. When we use Python and the Gpio Zero module all it takes to control a wheeled robot like ours is 9 different lines of code these are:
- from time import sleep – this will allow us to call the sleep function later in the code.
- from gpiozero import Robot – this will allow us to call all the robot functions from Gpio Zero.
- robot = Robot(left=(22,23),right=(24,25)) – this is the most complicated line in our code, all it does is assign which gpio pins will be used to control our robot. This will become much clearer when we start to build simpler circuits later in the year.
- robot.forward() – this function drives all our motors forward.
- robot.backward() – this function drives all our motors backward.
- robot.left() – this function drives the motors on the right forward and the motors on the left backward.
- robot.right() – this function drives the motors on the left forward and the motors on the right backward.
- sleep() – this function pauses the program.
- robot.stop() – this function stops the motors.
If we want to drive our robot forward for five seconds we would use the following code
- from time import sleep
- from gpiozero import Robot
- robot = Robot(left=(22,23),right=(24,25))
We had a brainstorming session about what we might do for our projects and we came up with some great ideas which we talk more about on Saturday. My slides from Saturday are here robot-intro.
Last week we used GPIO Zero’s inbuilt functions to control our robots. These functions allowed us to move or turn our robot using time to control the amount of movement, this week we made our own functions which allow us to control our robot using distance and degrees. A version of the code we used is available here and check out my slides here Robot Functions.
We had another enjoyable session this week, we especially enjoyed the drone demo by Chris Tierney.
We began our ownsession by looking at using input from our keyboard with Pygame to control our robot. We then returned to last week’s challenge, programming our robot to follow a defined path. We continued to have issues with the robots going off in arcs rather than straight line so we started to build corrections into the code. We will continue with this next week and we will learn to use functions to tell our robots what distance we want them to travel or the angle we want them to turn instead of using times to control them.
Here are my notes from today pygame robot and our code is available here
For more on Pygame there’s a very good tutorial here
This week the PiDojo group started to experiment with building our own robots.
To build a basic robot all you need is motors, a driver chip, some batteries and of course a Raspberry Pi.
IMPORTANT: Do not connect a motor, no matter how small directly to the Raspberry Pi, it will damage your Raspberry Pi.
We use a L293B motor driver chip which is like is a little current amplifier that takes a low-current control signal and then turns it into a higher-current signal that can drive a motor.
I would also recommend using separate batteries to power the Raspberry Pi and the Motors as the motors can sometimes draw a lot of power from the batteries for example when starting up and this could cause the Raspberry Pi to restart. We used a “Power Bank” from Penneys to power the the Raspberry Pi and four AA batteries to power the motors. Here is a drawing of our circuit.
We wired up our circuit and placed it into a container that had the motors attached to it.
Once we had our robot assembled we loaded up this code and tested it out then we tried to program our robots to follow a preset path but the lack of a third wheel made this very difficult, something we will have to improve for next week.
This week was also our first time using Gpio Zero which is a new Python library for controlling the GPIO pins on the Raspberry Pi. It makes physical programming much more straight forward and really helped to speed up our work this session.