Showing posts with label 4tronix. Show all posts
Showing posts with label 4tronix. Show all posts

Friday 23 December 2016

4Tronix Bit:Bot Neuron Controlled Edge follower

In the last post I was playing with 4Tronix's Bit:Bot. In this post I will show the initial experimentation with an artificial neuron controlling the Bit:Bot to follow the edge of a line (it follows the left-hand side of the line).



The neurons (well two separate ones, S1 and S2) are produced using weighted sums - summing the weights x inputs [ right-hand sensor (rs) and left-hand sensor (ls)] plus a bias for each neuron in this case w[0] and w[3].
    




              


    net=w[0]+w[1]*rs+w[2]*ls      
    net2=w[3]+w[4]*rs+w[5]*ls


  If weighted sum >=0 then its output 1 otherwise 0
    if net>=0: 
        s1=1
    else:
        s1=0

    if net2>=0:
        s2=1
    else:
        s2=0

What actual causes S1 to be either 1 or 0 is all defined by a set of weights w (three for the first neurone, S1,  three for S2).

w=[0,-1,1,-1,1,-1]



Converting the outputs of the two neurones S1 and S2 into actions is shown below.
    if s1==1 and s2==1:
        forward(40)   
    elif s1==0 and s2==1:
        forward(30)
        right_turn(10)
    elif s1==1 and s2==0:
        forward(30)
        left_turn(10)       
    elif s1==0 and s2==0:
        backward(40)

The functions for forward, right_turn, etc are defined elsewhere.


At the moment the movement is a bit rough and it is a little simpler to build a version that follows the centre of the line; this approach though, works with thinner lines. 

To change the function of the system, change the values in w; for example to produce one that follows the centre of the line just change w (I will leave that to someone to work on). The complete code is shown below.


Code
from microbit import *
import neopixel, random, array

w=[]  

def forward(n):
    pin0.write_analog(551)
    pin8.write_digital(0) 
    pin1.write_analog(551)
    pin12.write_digital(0)
    sleep(n)
    
def backward(n):
    pin0.write_analog(551)
    pin8.write_digital(1) 
    pin1.write_analog(551)
    pin12.write_digital(1)
    sleep(n)
    
def right_turn(n):
    pin0.write_analog(511)
    pin8.write_digital(0) 
    pin1.write_analog(511)
    pin12.write_digital(1)
    sleep(n)
    
def left_turn(n):
    pin0.write_analog(551)
    pin8.write_digital(1) 
    pin1.write_analog(551)
    pin12.write_digital(0)
    sleep(n)
       
w=[0,-1,1,-1,1,-1]

while True:
    ls= pin11.read_digital()
    rs= pin5.read_digital()
    
    net=w[0]+w[1]*rs+w[2]*ls
    net2=w[3]+w[4]*rs+w[5]*ls

    if net>=0:
        s1=1
    else:
        s1=0

    if net2>=0:
        s2=1
    else:
        s2=0
   
    if s1==1 and s2==1:
        forward(40)   
    elif s1==0 and s2==1:
        forward(30)
        right_turn(10)
    elif s1==1 and s2==0:
        forward(30)
        left_turn(10)       
    elif s1==0 and s2==0:
        backward(40)
       
           
    




All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

Sunday 18 December 2016

4Tronix Bit:Bot - now there is a good idea.

When I first heard of this robot, my first thought was what a great idea; a robot with neopixels (I know I should be saying 'smart RGB LEDs' but neopixels is so much more snappier) controlled via a micro:bit.





A good starting point for learning more about this robot, is the details on the 4Tronix site/blog, which includes build guidance and programming instructions for micropython and PXT. Though for the micropython code you might need to change pinX.digital_write() to pinX.write_digital() where X is the pin number.

My play code was to randomly select which neopixels to light up, I didn't include code to turn them off so multiple ones can be on. The robot is driven forwards, waits, backward, waits, turns to the right and then the left; and then repeats. 

Code:
from microbit import *
import neopixel, random

np = neopixel.NeoPixel(pin13, 12)

def forward(n):
    pin0.write_digital(1)
    pin8.write_digital(0) 
    pin1.write_digital(1)
    pin12.write_digital(0)
    sleep(n*1000)
    
def halt(n):
    pin0.write_digital(0)
    pin8.write_digital(0)
    pin1.write_digital(0)
    pin12.write_digital(0)
    sleep(n*1000)
    
def backward(n):
    pin0.write_digital(0)
    pin8.write_digital(1) 
    pin1.write_digital(0)
    pin12.write_digital(1)
    sleep(n*1000)

def right_turn(n):
    pin0.write_digital(1)
    pin8.write_digital(0) 
    pin1.write_digital(0)
    pin12.write_digital(1)
    sleep(n*1000)

def left_turn(n):
    pin0.write_digital(0)
    pin8.write_digital(1) 
    pin1.write_digital(1)
    pin12.write_digital(0)
    sleep(n*1000)

while True:
    pxl=random.randint(1,11)
    rd=random.randint(1,32)
    gr=random.randint(1,32)
    bl=random.randint(1,32)
    t1=random.randint(10,100)
    np[pxl] = (rd, gr, bl)
    np.show()
    forward(1)
    halt(1)
    backward(1)
    halt(1)
    right_turn(1)

    left_turn(1)

The video below shows it in action, the code is simple but this is a lovely robot to program especially if the mu editor is used.




All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

Monday 3 October 2016

Playing with 4Tronix's Micro:bit Playground

As much as I like the simplicity and flexibility of Crocodile Clips connecting components to a Micro:Bit, it can get a bit of a rat's nest of wires (especially if you are as messy as I am!). 4Tronix's have released their Micro:Bit PlayGround (http://4tronix.co.uk/store/index.php?rt=product/product&path=89&product_id=580)which is a board that has 3.5mm jack plugs to connect to a range of Gizmos (their phrase not mine) to which the Micro:Bit is screwed into. The battery pack is integrated onto the board on its back (see image below)



The Micro:Bit screws into the PlayGround via five screws which also for the connection between the PlayGround and the 'bit'. 

Below via the three cables connecting 3.5mm plugs, I have attached a 'Dial' (a potentiometer ) and two Flames (neopixels) to change the LED/neopixel's colours by rotating the Dial. Pin 0 has the 'Dial' attached and Pin 1 has the two Flames (neopixels) attached the out from the first goes in as input of the second via one the cables. 





Code
Essentially vary the 'Dial' varies the R,G,B values going to the two Flames/neopixels and so varying their colours. The code used is shown below.

from microbit import *
import neopixel

# Setup the Neopixel strip on pin0 with a length of 2 pixels

np = neopixel.NeoPixel(pin1, 2)

while True:

    s1=int(pin0.read_analog()/5)
    np[0] = (255, s1, 255-s1)
    np[1] = (s1,255, 255-s1)
    np.show()





Video of it in action





Thoughts
I like the idea that the cable doing both the power and control for the Gizmos, it does simplify building a little, more importantly it does produce less clutter (not so many wires). The whole unit with the batteries installed is a little weighty but that does give it at the same a sense of sturdiness which is a positive feature for just playing around  - you wouldn't use it for wearables. Not using croc clips also avoids issues with the clips slipping off with rough handling. 

Nice little arrangement which I am enjoying playing - is for everyone? Probably not, but does provide a sturdy system to experiment with some standardised units.


Related Links
Micro:Bit Playground - Starter Kit
Traffic lights - Microbit, GlowBugs and micropython


All opinions in this blog are the Author's and should not in any way be seen as reflecting the views of any organisation the Author has any association with. Twitter @scottturneruon

Top posts on this blog in March 2024

The Top 10 viewed post on this blog in March 2024. Covering areas such as small robots, augmented reality, Scratch programming, robots. Micr...