import error
Forums:
Hi,
I'm tried to write a code simple program to make the monsterBorg robot move continuously and stop if it detects any obstacle using the Sharp distance sensor.
The program is very simple, tbh I just used a few lines from monsterSequence code and combined it with the distance sensor code.
I have attached the screenshot of the error that I'm getting.
Please help me, I'm not sure what to do for this type of error.
Program:
-----------------------------------------------------------------------
import ThunderBorg
import time
import math
import sys
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
#Setup ThunderBorg
TB = ThunderBorg.ThunderBorg()
TB.Init()
if not TB.foundChip:
boards = ThunderBorg.ScanForThunderBorg()
if len(boards) == 0:
print 'No ThunderBorg found, check you are attached :)'
else:
print 'No ThunderBorg at address %02X, but we did find boards:' % (TB.i2cAddress)
for board in boards:
print ' %02X (%d)' % (board, board)
print 'If you need to change the I²C address change the setup line so it is correct, e.g.'
print 'TB.i2cAddress = 0x%02X' % (boards[0])
sys.exit()
TB.SetCommsFailsafe(False)
maxPower = 1.0
####################
# Distance measure #
CLK = 18
MISO = 23
MOSI = 24
CS = 25
mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)
#Test = Continous drive and stop when distance <=50cm
while True:
v0 = (mcp.read_adc(0) / 1023.0) * 3.3
dist0 = 16.2537 * v0**4 - 129.893 * v0**3 + 382.268 * v0**2 - 512.611 * v0 + 301.439
print("******************************")
print("Distance0 {:.2f}".format(dist0))
print("******************************")
if dist0 <= 50.00:
TB.SetMotor1(0.0 * maxPower)
TB.SetMotor2(0.0 * maxPower)
print("Stop")
else:
TB.SetMotor1(1.0 * maxPower)
TB.SetMotor2(1.0 * maxPower)
print("forward")
-----------------------------------------------------------------------
Rowan
Sat, 08/08/2020 - 12:55
Permalink
Solved it!
I guess now it's alright after adding these two lines in the previous code!
Program:
----------------------------
#!/usr/bin/env python3
# coding: latin-1
----------------------------
But since there are other imports for using Distance sensor, like...
-------------------------------
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
-------------------------------
it doesn't work when I use "sudo ./file.py" :(
Is there any solution for that?
Many Thanks
Rowan
piborg
Sat, 08/08/2020 - 15:14
Permalink
Missing imports
Glad to hear you figured out how to get the script to load with Python :)
I think the problem is that the Adafruit libraries have only been setup for Python 2. I am guessing that you followed the install instructions on their GitHub:
If so you should be able to install the libraries for Python 3 as well by going back to the same directory and running the command with Python 3 instead:
You will probably need to do the same thing with the
Adafruit_MCP3008
library as well.Rowan
Mon, 08/10/2020 - 14:21
Permalink
Brilliant! works smooth :)
Hi PiBorg,
Thank you very much.
At first, I tried "pip3 install" to install those Libraries.
But for some reason even after install it wasn't working!
Thanks for your above installation method, it works perfectly and My robot stop detecting objects :)
Many Thanks
Rowan
Rowan
Mon, 08/10/2020 - 16:27
Permalink
GPS import error
Hi,
I still finding another similar error.
I'm also using Adafruit Ultimate GPS for the MonsterBorg
When I try to combine the existing code (monsteborg movement with obstacle avoidance), with Adafruit GPS code (which has few other library imports) causes another similar error like previously :(
- I think its because the Adafruit libraries code works only in python3, the monsterborg code doesn't work in python3...
I tried just running the GPS code using python3 file.py and python file.py. While using python3, the program was running without any issues because I already installed libraries using pip3 (pip3 install adafruit-circuitpython-gps). But while using python2, it displayed the import error! which is...
"
Traceback (most recent call last):
File "gps_simpletest.py", line 5, in <module>
import board
ImportError: No module named board
"
Therefore I tried to change the monsterborg script from
"
#!/usr/bin/env python
# coding: Latin-1
"
TO
"
#!/usr/bin/env python3
# coding: Latin-1
"
I thought this would help run the GPS imports... but I got a different error.
which is...
"
message
message
message
message
Scanning I²C bus #1
No ThunderBorg boards found, is bus #1 correct (should be 0 for Rev 1, 1 for Rev 2)
No ThunderBorg found, check you are attached :)
"
I also tried running the existing norma code like "monsterSequence.py" using python and python3. Using python it worked perfect and while using python3... it poped the same error 😭
Please help me run the monsterborg code using python3. I guess it will solve the issues
Many thanks
Rowan
piborg
Mon, 08/10/2020 - 21:07
Permalink
MonsterBorg and Python 3
To get the MonsterBorg scripts to work with Python 3 you will need the Python 3 version of the ThunderBorg library.
You can download it here: http://forum.piborg.org/downloads/thunderborg/ThunderBorg3.py.txt
Alternatively you can download directly to the Pi using:
You can then change your scripts from:
to
The other major change is any lines with
print
in the script. These need to be changed from:to