Raspberry Pi Bluetooth controlled steerable robot

From wikiluntti

Introduction

We create a a simple keyboard and mouse controlled robot. The data is transferred using Bluetooth (EV3BT package). We use TkInter and Python3.

Robot

Almost any robot will do. We use Verne 2/ Asimov.

Theory

The three loops are ready for change.

The following Python script needs to be run and after that the EV3-G code.

import tkinter as tk
import time
import EV3BT
import serial

class App(object):
    def __init__(self, master, **kwargs):
        self.master = master
        self.master.bind('<Left>', self.leftKey)
        self.master.bind('<Right>', self.rightKey)
        self.master.bind('<Up>', self.upKey)
        self.master.bind('<Down>', self.downKey)

        self.canvas = tk.Canvas(self.master, width=200, height=20)
        self.canvas.pack()

        self.power = tk.Scale(self.master,from_=-100,to=100)
        self.power.pack()

        self.steering = tk.Scale(self.master,from_=-100,to=100,
        orient=tk.HORIZONTAL)
        self.steering.pack()

        self.steeringTime = 0

        #
        #
        self.EV3 = serial.Serial('/dev/rfcomm0')

        self.master.after(0, self.update)

    def leftKey(self, event ):
        self.steering.set( self.steering.get() - 5)
        self.steeringTime = time.time()
    def rightKey(self, event ):
        self.steering.set( self.steering.get() +  5)
        self.steeringTime = time.time()

    def upKey(self, event ):
        self.power.set( self.power.get() - 5)
    def downKey(self, event ):
        self.power.set( self.power.get() +  5)

    def update(self):
        s = EV3BT.encodeMessage(EV3BT.MessageType.Numeric, 'power',
                self.power.get() )
        self.EV3.write(s)

        s = EV3BT.encodeMessage(EV3BT.MessageType.Numeric, 'steering',
                self.steering.get() )
        self.EV3.write(s)

        if time.time() - self.steeringTime > 1:
            self.steering.set(0)

        self.master.after(100, self.update )

root = tk.Tk()
app = App(root)
root.mainloop()

Video

Exercises

  1. Robot steers into a wrong direction. Fix that.