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!