Thursday, 24 August 2017

Robot control by a neuron.

This year the Computing team has been fortunate enough to host three Nuffield Research Placement students (https://www.nuffieldresearchplacements.org/) all working with Dr Scott Turner.


Michael Welsh
Michael has been working on using a micro:bit based bitbot from 4tronix to produce a potential teaching tool; an example of artificial neurons used control a robot. The aim is for this tool to be used with 3rd-year Undergraduates, as part of a module on Artificial Intelligence.

Michael's solution was to use the computer to run and train a single neuron; then for the robot to send values from the line sensors back to the program running on a Computer and receive control signals. 

Sounds easy? No really, in the end, the software running on the computer had to also send and receive the data through a microbit (via USB) and then use radio to communicate with the bit:bot robot. All the various developed parts of the solution were implemented in Python by Michael.




Example of the code.

import serial
from math import fabs
import random
import sys
import glob


#TO DO: GUI, implement manual mode. Make manual mode a button?? When in automatic, weight boxes are greyed out. Otherwise, they are able to be typed into.

def forward(n):
    microbit.write(('f'+str(n)).encode('utf-8'))
def backward(n):
    microbit.write(('b'+str(n)).encode('utf-8'))
def turnR(n):
    microbit.write(('r'+str(n)).encode('utf-8'))
def turnL(n):
    microbit.write(('l'+str(n)).encode('utf-8'))
def end():
    microbit.write('e'.encode('utf-8'))


def serial_ports():
    """ Lists serial port names

        :raises EnvironmentError:
            On unsupported or unknown platforms
        :returns:
            A list of the serial ports available on the system
    """
    if sys.platform.startswith('win'):
        ports = ['COM%s' % (i + 1) for i in range(256)]
    elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
        # this excludes your current terminal "/dev/tty"
        ports = glob.glob('/dev/tty[A-Za-z]*')
    elif sys.platform.startswith('darwin'):
        ports = glob.glob('/dev/cu.*')
    else:
        raise EnvironmentError('Unsupported platform')

    result = []
    for port in ports:
        try:
            s = serial.Serial(port)
            s.close()
            if '/dev/cu' in port:
                if '/dev/cu.usbmodem' in port:
                    result.append(port)
            else:
                result.append(port)
        except (OSError, serial.SerialException):
            pass
    return result


class neuron:

    def __init__(self,ins):

        self.inputs = [1]

        self.weights = []

        for i in range(ins+1):
            self.weights.append(random.uniform(-1,1))
        #print(self.weights)

        self.LC = 0.005
        
    def get_output(self,inputs):
        self.inputs = [1]+inputs

        WSum = 0

        for i in range(len(self.inputs)):
            WSum += int(self.inputs[i]) * self.weights[i]

        sums = sorted([fabs(WSum), fabs(WSum - (1/3)), fabs(WSum-(2/3)), fabs(WSum-1)])
        #print(sums)
        if sums[0] == fabs(WSum): #return 0 - move backwards
            backward(5)
            return 'B',WSum
        elif sums[0] == fabs(WSum - (1/3)):
            turnR(5)
            return 'R', WSum
        elif sums[0] == fabs(WSum - (2/3)):
            turnL(5)
            return 'L', WSum
        else:
            forward(20)
            return 'F', WSum

    def train(self,inputs,desired):

        result = self.get_output(inputs)[1]
        print(result)
        error = desired - result
        #print(error)

        for w in range(len(self.weights)):

            change = self.LC * int(self.inputs[w]) * error

            print('change in weight ' + str(w) + ': ' + str(change))

            self.weights[w] += change


for i in serial_ports():
    try:
        microbit = serial.Serial(port=i, baudrate=115200)
        break
    except:
        pass

microbit.setDTR(1)
microbit.close()
microbit.open()

control = neuron(2)

mode = input('Automatic or Manual? (A/M)\n')
while mode.lower() not in ['a','m']:
    mode = input('Automatic or Manual? (A/M)\n')
if mode == 'm':
    control.weights[0],control.weights[1],control.weights[2] = int(input('Enter weight 0/bias: ')),int(input('Enter weight 1: ')), int(input('Enter weight 2: '))
while True:
    #microbit.write(input('input: ').encode('utf-8'))
    out = microbit.read(6)
    microbit.flush()
    try:
        out = out.decode()
    except:
        pass
    for i in range(len(out)):
        if str(out[i]) in ['0','1']:
            if i == 0:
                if out[1] == ']':
                    num2 = out[i]
                elif out[1] == ',':
                    num1 = out[i]
            elif i == 5:
                if out[4] == '[':
                    num1 = out[i]
                elif out[4] == ' ':
                    num2 = out[i]
            else:
                if out[i-1] == '[':
                    num1 = out[i]
                elif out[i-1] == ' ':
                    num2 = out[i]
                elif out[i+1] == ']':
                    num2 = out[i]
                elif out[i+1] == ',':
                    num1 = out[i]
        
        #print(out)
    try:
        sleft=int(num1)
        sright=int(num2)
        print([sleft, sright])
        if mode == 'a':
            if [sleft, sright] == [0, 0]:
                control.train([sleft,sright],0)
            elif [sleft, sright] == [0, 1]:
                control.train([sleft,sright], 1/3)
            elif [sleft,sright] == [1, 0]:
                control.train([sleft,sright], 2/3)
            else:
                control.train([sleft,sright], 1)
        else:
            if [sleft, sright] == [0, 0]:
                control.get_output([sleft,sright])
            elif [sleft, sright] == [0, 1]:
                control.get_output([sleft,sright])
            elif [sleft,sright] == [1, 0]:
                control.get_output([sleft,sright])
            else:
                control.get_output([sleft,sright])
    except:
        pass


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

3 comments:

Remote Data Logging with V1 Microbit

In an earlier post  https://robotsandphysicalcomputing.blogspot.com/2024/08/microbit-v1-datalogging.html  a single microbit was used to log ...