Simplebot Code for BrickPi3

Hello,

I’m looking to modify the existing Simplebot code for use on BrickPi3. I’d like to start out with as simple of a robot as possible so I can add on and play with other sensors one at a time to hopefully learn how to make more complicated robots in the future. With that in mind, my code below doesn’t seem to work. I borrowed most of the script from simplebot_simple.py on Github and tried to amend the language/variables to BP3 coding from the balance bot robot:

#!/usr/bin/env python

########################################################################

Program Name: dumbbot.py

================================

Trying to modify simplebot_simple.py to BrickPi3…

########################################################################

#Commands:

w-Move forward

a-Move left

d-Move right

s-Move back

x-Stop

from future import print_function # use python 3 syntax but make it compatible with python 2
from future import division # ‘’

import time # import the time library for the sleep function
import brickpi3 # import the BrickPi3 drivers
import sys # import sys for sys.exit()

BP = brickpi3.Brickpi3() # Create an instance of the BrickPi3 class. BP will be the BrickPi3 object.

define which ports the sensors and motors are connected to.

PORT_SENSOR_IR = BP.PORT_1
PORT_MOTOR_RIGHT = BP.PORT_A
PORT_MOTOR_LEFT = BP.PORT_D

#Move Forward
def fwd():
BrickPi3.MotorSpeed[PORT_MOTOR_RIGHT] = speed
BrickPi3.MotorSpeed[PORT_MOTOR_LEFT] = speed
#Move Left
def left():
BrickPi3.MotorSpeed[PORT_MOTOR_RIGHT] = speed
BrickPi3.MotorSpeed[PORT_MOTOR_LEFT] = -speed
#Move Right
def right():
BrickPi3.MotorSpeed[PORT_MOTOR_RIGHT] = -speed
BrickPi3.MotorSpeed[PORT_MOTOR_LEFT] = speed
#Move backward
def back():
BrickPi3.MotorSpeed[PORT_MOTOR_RIGHT] = -speed
BrickPi3.MotorSpeed[PORT_MOTOR_LEFT] = -speed
#Stop
def stop():
BrickPi3.MotorSpeed[PORT_MOTOR_RIGHT] = 0
BrickPi3.MotorSpeed[PORT_MOTOR_LEFT] = 0

BrickPi3Setup() # setup the serial port for communication

PORT_MOTOR_RIGHT = BP.PORT_A
PORT_MOTOR_LEFT = BP.PORT_D
BrickPi3.MotorEnable[PORT_MOTOR_RIGHT] = 1 #Enable the Motor A
BrickPi3.MotorEnable[PORT_MOTOR_LEFT] = 1 #Enable the Motor B
BrickPi3SetupSensors() #Send the properties of sensors to BrickPi
BrickPi3.Timeout=10000 #Set timeout value for the time till which to run the motors after the last command is pressed
BrickPi3SetTimeout() #Set the timeout

speed=200 #Set the speed
while True:
inp=str(raw_input()) #Take input from the terminal
#Move the bot
if inp==‘w’:
fwd()
elif inp==‘a’ :
left()
elif inp==‘d’:
right()
elif inp==‘s’:
back()
elif inp==‘x’:
stop()
BrickPi3UpdateValues() #Update the motor values

time.sleep(.01)      	# sleep for 10 ms

I get the following error:

Traceback (most recent call last):
File “/home/pi/Desktop/Dumbbot/dumbbot.py”, line 25, in
BP = brickpi3.Brickpi3() # Create an instance of the BrickPi3 class. BP will be the BrickPi3 object.
AttributeError: ‘module’ object has no attribute ‘Brickpi3’

I’m definitely not a programmer so this is a bit over my head. Any help or suggestions would be greatly appreciated as this code could be used by others to create the simplebot on BP3.

Thanks!

Turns out tcoops already figured it out a back in January…

I’m not sure why he did a few things in his code though so I’m going to try to look at it for a bit, but if anyone wants to re-create simplebot his code works straightaway.

Hello again,

This code is really great and pretty easy to understand after a while. I augmented the simplebot with a grabber arm. The code is as followed, with only a new set of additions for the new medium motor.

‘’’

Program Name: simplebot_simple.py

================================

This code is for moving the simplebot

Author Date Comments

Karan 04/11/13 Initial Authoring

These files have been made available online through a Creative

Commons Attribution-ShareAlike 3.0 license.

(http://creativecommons.org/licenses/by-sa/3.0/)

Revised by T. Cooper 12/18

— program updated for Python 3

— curses interface added for consistent input mang.

#Commands:

w-Move forward

a-Move left

d-Move right

s-Move back

x-Stop

we add these libraries to give us the ability to use sleep func

use Brick Pi 3 stuff and the curses interface (it makes input easier and consistent)

‘’’

import time

import brickpi3 #import BrickPi.py file to use BrickPi operations

import curses # import curses for text processing

set up curses interface

stdscr = curses.initscr() # Uses curses ext. More info here https://docs.python.org/3/howto/curses.html

curses.noecho() #more info here https://linux.die.net/man/3/noecho

BP = brickpi3.BrickPi3() # Create an instance of the BrickPi3 class. BP will be the BrickPi3 object.
motorR = BP.PORT_D # right motor
motorL = BP.PORT_A # left motor
motorG = BP.PORT_B # gripper motor
speed = 225 #range is -255 to 255, make lower if bot it too fast
#Move backward

def back():
BP.set_motor_power(motorR,speed)
BP.set_motor_power(motorL,speed)

#Move Left
def left():
BP.set_motor_power(motorR, speed)
BP.set_motor_power(motorL, -speed)

#Move Right
def right():
BP.set_motor_power(motorR, -speed)
BP.set_motor_power(motorL, speed)

#Move forward
def fwd():
BP.set_motor_power(motorR, -speed)
BP.set_motor_power(motorL, -speed)

#Stop
def stop():
BP.set_motor_power(motorR, 0)
BP.set_motor_power(motorL, 0)
BP.set_motor_power(motorG, 0)

#gripper forward
def grip():
BP.set_motor_power(motorG, speed)

#gripper release
def rgrip():
BP.set_motor_power(motorG, -speed)

while True:
inp = stdscr.getkey() #Take input from the terminal
#Move the bot
if inp == ‘w’:
fwd()
print(“fwd”)

    elif inp=='a' :
            left()
            print("left")

    elif inp=='d':
            right()
            print("right")

    elif inp=='s':
            back()
            print("back")
elif inp=='g':
	grip()
	print("grip")
elif inp=='r':
	rgrip()
	print("release grip")

    elif inp=='x':
            stop()

    time.sleep(.01)         # sleep for 10 ms

I’ve attached a picture below as a reference. If anyone needs more detailed pictures just let me know. I’d honestly recommend starting with the simplebot for your first BrickPi build as you can start with as basic of a set-up as is probably possible.

IMG_4897