Marcus Johnson
Published © GPL3+

Python BOE Bot

This project will show how you can control a BOE bot with a Bluetooth module and a little Python.

IntermediateShowcase (no instructions)4 hours238
Python BOE Bot

Things used in this project

Hardware components

Parallax Boe-Bot Robot Kit - USB
×1
Parallax RN-42 Bluetooth Module
×1

Software apps and online services

Crazyflie Python Client
Bitcraze Crazyflie Python Client
Parallax BASIC Stamp Software

Story

Read more

Schematics

BOE Bot simulation schematic

The CAD software I used didn't have a BOE microcontroller so I tried to simulate it with a breadboard, battery, ultrasonic sensor (representing the Bluetooth module) and an Arduino.

Code

BOE Bluetooth Robot Code

Plain text
This is the software that goes on the microcontroller BOE device. It receives the commands from the Python client code. This code is written in PBasic.
' =========================================================================
'
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------

' This program demonstrates how you can control your BOE-Bot from an
' app using the RN-42 Bluetooth Module


' -----[ Revision History ]------------------------------------------------



' -----[ I/O Definitions ]-------------------------------------------------

BT_RX           PIN     0               ' RN-42 TX Pin
Servo_R         PIN     12              ' Right Servo
Servo_L         PIN     13              ' Left Servo


' -----[ Constants ]-------------------------------------------------------

Baud            CON     84              ' 9600 bps
Refresh         CON     20              ' Servo Refresh


' -----[ Variables ]-------------------------------------------------------

direction       VAR     Byte            ' Direction Command
pulseLeft VAR Word
pulseRight VAR Word
pulseCount VAR Word
counter VAR Word



' -----[ Initialization ]--------------------------------------------------

  LOW Servo_R                           ' Initialize Servo Pins
  LOW Servo_L


' -----[ Program Code ]----------------------------------------------------

Main:

  DO

    SERIN BT_RX, Baud, [direction]

    SELECT direction
      CASE = "w"
        GOSUB Go_Forward
      CASE = "s"
        GOSUB Go_Backward
      CASE = "a"
        GOSUB Go_Left
      CASE = "d"
        GOSUB Go_Right
      CASE = "x"
        GOSUB Stop_Motors
    ENDSELECT

  LOOP


' -----[ Subroutines ]-----------------------------------------------------

Go_Forward:
  pulseLeft = 850: pulseRight = 650: pulseCount = 64: GOSUB Navigate
  RETURN

Go_Backward:
  pulseLeft = 650: pulseRight = 850: pulseCount = 64: GOSUB Navigate
  RETURN

Go_Left:
  pulseLeft = 725: pulseRight = 725: pulseCount = 24: GOSUB Navigate
  RETURN

Go_Right:
  pulseLeft = 775: pulseRight = 775: pulseCount = 24: GOSUB Navigate
  RETURN

Stop_Motors:
  pulseLeft = 0: pulseRight = 0: pulseCount = 0: GOSUB Navigate
  RETURN

Navigate:
  FOR counter = 1 TO pulseCount
    PULSOUT 13, pulseLeft
    PULSOUT 12, pulseRight
    PAUSE Refresh
  NEXT
  PAUSE 200
  RETURN

Python BOE Client

Python
This is the code that will send commands via Bluetooth to the robot.
import time
import random
import bluetooth


def activateBotMotion():
    

    #Set the target MAC address which is typically found on the Bluetooth Module
    target_address = '00:06:66:83:E6:C0' 

    #This represents the serial port and set to a default of 1 based on the documentation I've read
    port = 1

    #Establish a connection with the RN-42 Bluetooth module
    bluetoothBotConnection = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
    bluetoothBotConnection.connect((target_address, port))


    # Set the starting number of seconds and the total seconds the bot will continue to move
    seconds = 0
    total_seconds = 60

    #Set the dictionary that will contain key values that will be sent to the bot
    motionDict = {1: 'w', 2: 's', 3: 'a', 4: 'd', 5: 'x'}
 
    # Loop that will continue until seconds is no longer less than total seconds
    while seconds < total_seconds:
 

        #Obtain a random value between 1 and 4 to correspond to the keys in the dictionary. Five won't be used as that key stops the robot motors
        motionKey = random.randint(1, 4)

        #Send the value of the motionKey to the robot
        motionValue = motionDict[motionKey]
        bluetoothBotConnection.send(motionValue)
 

        # Delays the program 3 seconds
        time.sleep(3)
 
        # Increases total time by three seconds
        seconds += 3
 
    #Send the command to stop the bot after the total seconds are reached
    bluetoothBotConnection.send(motionDict[5])
    bluetoothBotConnection.close()
    print("Stopping motors!")
 
if __name__ == "__main__":
    activateBotMotion()

Credits

Marcus Johnson
9 projects • 28 followers
Software engineer with professional experience creating, maintaining, integrating, and testing software in Windows and Unix environments.
Contact

Comments

Please log in or sign up to comment.