Friday 24 March 2017

Variable speed motors and radio control


This is a quick project I came up with to use the first DAVE robot I made, which was gathering dust and feeling a bit unloved. I wanted to demonstrate two things: variable speed motor control and radio communication between two micro:bits.



Remember, my first DAVE does not use the micro:bit motor controller board by Kitronik. This was not available back then, so I had to make my own much simpler one with transistors and stripboard. Pin 1 of the micro:bit controls the left motor, pin 2 controls the right motor. There is no option to reverse the motors. 

I used the Mu MicroPython editor to write some code that takes control inputs from a handheld micro:bit and transmits data to a receiver micro:bit on the robot.

This is the MicroPython code for the transmitter:

from microbit import *

import radio
radio.on()

speed_both = 0
speed_left = 0
speed_right= 0
wait_until_time=0

while True:
    
    #stop the robot if A+B are pressed, decrease speed with A and increase speed with B 
    #there is a slight delay in the increase/decrease to improve controller usability
    if button_a.is_pressed() and button_b.is_pressed():
        speed_both = 0
    elif button_a.is_pressed() and running_time() > wait_until_time:
        if speed_both > 0:
            speed_both = speed_both - 1
            wait_until_time=running_time() + 200           
    elif button_b.is_pressed() and running_time() > wait_until_time:
        if speed_both < 4:
            speed_both = speed_both + 1
            wait_until_time=running_time() + 200  
    
    #split speed into left and right motor values
    speed_left = speed_both
    speed_right = speed_left
    
    #tilting will steer the robot
    steering = accelerometer.get_x()
    
    #reduce left motor speed according to amount of left tilt 
    if steering < -200:
        speed_left = speed_left - 1
        if steering < -400:
            speed_left = speed_left - 1
            if steering < -600:
                speed_left = speed_left - 1
                if steering < -800:
                    speed_left = speed_left - 1 
                    
    #reduce right motor speed according to amount of right tilt
    if steering > 200:
        speed_right = speed_right - 1
        if steering > 400:
            speed_right = speed_right - 1        
            if steering > 600:
                speed_right = speed_right - 1
                if steering > 800:
                    speed_right = speed_right - 1
 
    #motor speeds are not allowed to be negative
    if speed_left < 0:
        speed_left = 0 
    if speed_right < 0:
        speed_right = 0

    #show motor speeds on LED display    
    display.clear()    
    display.set_pixel(0,4 - speed_left,9)
    display.set_pixel(4,4 - speed_right,9)
    
    #transmit a three char string where the second and third chars are left and right motor speeds  
    #the +100 fixes the string length at 3 and allows string indexing in the receiver to get the speed values
    speed_left = speed_left * 10
    radio.send(str(100 + speed_left + speed_right))
    
    #this pause helps reliability when using the radio
    sleep(50)

And this is the MicroPython code for the reciever:

from microbit import *

import radio
radio.on()

pin1.write_analog(0)
pin2.write_analog(0)
speeds = "100"
display.show(Image.NO)

while True:   
    
    #get a string from the radio message queue
    #if nothing is recieved, the robot continues as before
    #the sleep helps reliability when using the radio
    sleep(50)
    speeds=radio.receive()
    if speeds != None:   
        #speeds '100' is stop
        #speeds '144' is forward
        #speeds '104' is hard left turn
        #speeds '142' is wide right turn, etc
        speed_left = int(speeds[1])
        speed_right = int(speeds[2])   
    
        #analog output values can be 0 up to 1023  
        pin1.write_analog(speed_left * 255)
        pin2.write_analog(speed_right * 255)        
        
        #show motor speeds on led display
        display.clear()    
        display.set_pixel(0,4 - int(speed_left),9)
        display.set_pixel(4,4 - int(speed_right),9)

Monday 22 August 2016

The clap detector circuit

My DAVE robots have a breadboard attached to the top deck by velcro tape. Electronic circuits for sensors are built on this breadboard and these give the robot its sense of the world around it.

The circuit below is a sound sensor that reacts to loud noises like a handclap by producing an electronic pulse. An electret microphone picks up the sound and an LM386 amplifier boosts the signal. This signal triggers a 555 timer which turns the messy analogue signal into a single clean digital pulse.

The circuit runs off a 9V PP3 battery so it gets a clean voltage without the electrical noise created by the motors, which would ruin the amplification. 

The micro:bit buttons are pull-down switches, which makes interfacing to circuits of higher voltages very simple. Our BC337 transistor does the same job as the switch, pulling down to the 0V rail. You do need to be careful that your breadboard to BTN_A wire does not connect to the 9V supply of the breadboard! This could damage your micro:bit. 

The pulses from this circuit are counted by MicroPython code which carries out different actions depending on the number of command claps it hears. You can find the code here:

https://www.microbit.co.uk/pvlxqg 



Wednesday 13 April 2016

DAVE 2 - wiring it up


It is now time to switch on the soldering iron and get serious. I used single core insulated wire because this works best in the screw terminals of the Kitronik board.

My first move was to stick the 4 x AA battery holder into position with velcro sticky tape. I used hot glue on my first DAVE but this did not stick well to the plywood and it broke away when the robot was dropped. Velcro tape holds things very firmly but has a bit of shock-absorbing give which avoids this problem. 

In electronics, red is for positive and black is for zero volts. The red 6 volt wire goes through one side of the switch. It is a double-pole double-throw switch (DPDT) which means that two separate circuits can be controlled at the same time. The other half, or pole, of the switch will be used for the 9 volt circuit for external sensors. You can't see it well but I left a loop of wire available where the black wire connects to the negative battery terminal because the black wire of the 9 volt circuit will also connect here, creating a common zero volt rail.


With the 6 volt supply in place, I led the wires up through the underside of the top deck so they could go into the Kitronik board. I arranged all the wiring so that when the top deck is removed it will open like a book and lie upside down alongside the bottom deck. This gives easy access to the inside of the robot when you need to change the batteries or fix something.

Now everything has been flipped over and you can see the red and black 6 volt wires coming up through the top deck and into the power terminals of the Kitronik board. A pair of green wires has been connected to the MOTOR1 terminals and threaded down though the top deck and then the bottom deck to reach the motors. The motors are unclipped from the gearboxes for soldering.


Flipped back, here are the motor wires running across the bottom deck. Maybe the hole would have been better on the other side of the battery holder? This setup works okay and avoids a big bunch of wires all on one side of the chassis. I'm happy enough with it. I did find a 3mm hole is rather tight for the motor wires so a 6mm hole would be better.

Here the green wires have been soldered to their motor and this has been installed in the gearbox. Yellow wires have been threaded through the decks for the other motor.


The yellow wires have been soldered to the other motor and connected to MOTOR2 on the board.

Here the 9 volt circuit has been added. The PP3 battery is stuck to the underside of the top deck using velcro tape. The PP3 connector red wire runs to the switch and the black wire runs to the 0 volt terminal of the 4 x AA battery holder, creating the common 0 volt rail I mentioned earlier. A red wire from the switch and a black wire from the 0 volt terminal run up through the top deck and finish with bare ends, ready to plug in to a breadboard mounted on the top deck. The switch now controls the 6 volt supply to the Kitronik board and the 9 volt supply to the breadboard.


And here it is finished! The top deck has been screwed onto the spacers and a breadboard has been attached using velcro tape. The project on the breadboard has a microphone and other components that react to loud sounds like a handclap by producing a short pulse. That pulse goes back through a yellow wire to the BTN_A input of the Kitronik board where MicroPython code on the micro:bit counts these pulses and drives the motors in response. I will go into more detail on this project in a future post but for now, enjoy this video of DAVE 2 in action! 

Tuesday 12 April 2016

DAVE 2 - Putting it all together


The next step was to drill the mounting holes in the decks.

This step needs to be fairly precise. If the front caster is not on the centreline of the motor, the robot will always veer off course. I marked a centreline onto the underside of the bottom deck and laid the motor onto it. When the motor was properly aligned I marked through the mounting holes. I double checked the motor alignment with the centerline and the mounting holes, then laid the caster on the centerline at the front and marked its mounting holes. There needs to be enough room for the battery holder in between the motor and caster screws, a picture further down the page shows what I mean.

 I marked four more holes for the spacers that hold the decks together. 

A 3mm drill was used to make all the mounting holes. 

The four holes for the spacers need to line up exactly on the two decks. The easy way to do this is to lay the decks together and use the lower deck as a guide to drilling the top deck. To stop the decks from shifting around, 3mm screws were pushed though the holes as they were drilled.

 Construction has really moved along in this picture. Holes for the Kitronik board were marked and drilled in the top deck.  The motor, caster and spacers were attached to the bottom deck. You can see here how the battery holder sits in between the mounting screws for the motor and caster.

A 6 mm hole was drilled in the upper deck for wires to come up through. There is a hole in the lower deck for the wires that will go down to the motors.  The switch was mounted upside down in another 6mm hole on the centreline.


This shows the switch from the underside, in the off position. This means the robot is unlikely to get switched on accidentally as it is shielded by the motor.

The next stage will be wiring all the components together...

  

DAVE 2 - Beginning the build


DAVE 2 is the second Digital Autonomous Vehicle for Education. It is built around the Kitronic Motor Driver board for the micro:bit. This has an edge connector that the micro:bit slots into. It provides the 3 volt power supply that the micro:bit requires from a 4.5 volt or 6 volt battery. It has power transistors that boost the power from some of the micro:bit's pins so that you can drive a DC motor in either direction with your code. It has terminals for connecting sensors and switches that your code can read and respond to. All the terminals have screw connectors so no soldering is needed here.

The motor, gearbox, wheels and caster come from the model maker Tamiya. These are the 70097 Twin-Motor Gearbox Kit, the 70101 Truck Tire Set, and the 70144 Ball Caster Kit. These come as kits of parts that need to be assembled. This can be fiddly and many of the parts are small but the result is a robust unit that is perfect for robotics. The ball caster is the same height as the motor and wheels so the chassis sits level.

The chassis is made from 3mm plywood. The two decks are held together with M3 x 25mm Female/Female Threaded Brass Hex Spacers, M3 x 12mm screws and M3 washers.

A 4 x AA battery holder provides 6 volts to the Kitronic board and a separate 9 volt PP3 battery powers the optional external sensor circuits. A separate power supply is needed because the motors running on the 6 volt supply create a lot of interference and electrical noise. The micro:bit is isolated from this by the 3 volt regulator on the Kitronic board but running sensitive circuits on the 6 volt supply is impossible. The simplest solution is to use a different power supply for sensor circuits. The two power sources must be connected together at the 0 volt rail. The 6 volt and 9 volt rails are both controlled by a double pole switch. 

Lets have some pictures....

These are some of the parts I started with.

After assembling the gearbox and fitting the wheels I measured the space I would need to cut out to make room for the wheels. I folded a piece of paper in half and marked out one half of the body with one wheel cutout. When unfolded, the template was symmetrical. I checked it for fit against the wheels and then marked out the two decks on the plywood. Only the lower deck needs wheel cutouts.I clamped the plywood to a workbench and cut out the decks with a hacksaw.

I found that sawing the long straight sides was problematic. It was impossible to keep the cut straight, the saw would wander off course and leave a wobbly edge. I rough cut the decks from the large piece of plywood leaving about a centimetre of excess around the outside. I also cut an extra bit of plywood to protect the decks from being damaged by the clamp. I laid the edge of the hacksaw blade along the line I wanted to cut  and made several slow careful sawing movements across the surface. This created a groove all the way along the surface of the plywood which guided the saw and kept it perfectly straight as I cut all the way through. Unfortunately I was concentrating so much on making nice straight cuts that I forgot to take a picture of this. Sorry!








Sawing the wheel cutouts is the trickiest bit. I took a series of diagonal cuts which quickly got it square enough to be filed in to the final shape. 

After the decks were cut out I smoothed and beveled off the edges with a file.

The completed decks!  

The next posts will be about drilling the mounting holes and wiring everything up...

Monday 11 April 2016

DAVE


I am a software engineer. I started playing with technology a long time ago with a Commodore VIC-20 home computer and later a C-64. Programming in BASIC and 6502 assembler was my world. At the same time I was also making electronic circuits; plugging components like LEDs, transistors, op-amps, and logic gate ICs into a breadboard or soldering them onto stripboard and mounting the boards into plastic cases.

This youthful learning was helped by the BBC Micros we had at school where I learned the concepts of networked computing; logging in with a username and password and seeing all your files whichever machine I had logged in on. Eye-opening stuff for the 1980's !

This turned into a career in technology which led me to the BBC at MediaCityUK, Salford. 

About six months ago I became involved in the micro:bit project. This is a partnership of many organizations coming together to deliver an incredible educational leap; a small cheap connectable programmable device to be given free through schools to every Year 7 child of 2016, backed with copious learning resources for teachers and students.

I was very fortunate to be working on this project and I began to think back to the things that I built and the programs I wrote as a youngster. I was especially keen on using the edge connector of the micro:bit to give the tiny board more senses and more ways to interact with the outside world. I wanted to build a robot body for a micro:bit brain!

It was a very long time since I had built anything like this so I started with a kit purchased from the internet. This had nothing to do with micro:bits, just a couple of motors and a circuit board all bolted to a piece of plywood. I just wanted to have a look at what I was getting into. I learned a lot by building that kit and it all went into shaping my ideas for a micro:bit robot.

I wanted two decks on the chassis not just one, so that the components were not crowded together and fiddly to assemble.

I wanted to use a matched set of motors gearboxes and wheels to make the drivetrain robust and make the robot stand level. 

I wanted the finished robot to be easy to open up to allow for modifications, fixes, battery changes and the like.

I wanted it to be a platform for experimentation able to do many different things, not specialized to one particular task.

I wanted it to look simple and friendly, not intimidatingly technical or complicated. I wanted people to look at it and think "I could build that!"

It also needed a name so it became DAVE, the Digital Autonomous Vehicle for Education. So I had a great name and I had my guiding principles. Now I needed to build it.

I bought an edge connector and breakout board from Kitronik who are a micro:bit partner. I bought a twin motor/gearbox with wheels and ball caster made by the model company Tamiya. I bought electronic components from Maplin. I got some plywood, screws, bolts and threaded pillars that would become a roomy double-decker chassis. 

Building the first DAVE was a story of silly mistakes, lessons learned, problems solved and setbacks overcome. DAVE first ran during the Christmas holidays of 2015 and was revealed to the world on February 12th in this Twitter post.  




I had always intended to make two robots, one as a prototype where I would learn the hard way and a second as a clean build that I could document and share. By the time DAVE was completed and running well, the technology had moved on. Kitronic had released a new micro:bit board which took care of many of the problems I had fought with. MicroPython was now live, making programming the micro:bit easier for teachers and students already using Python in school. The next DAVE would not be a copy of the first. It would be better.

My next post will be the story of DAVE 2....