How to fix nxt.motor.BlockedException? - python

I have the following script: 
#!/usr/bin/env python
import nxt.locator
from nxt.motor import *
def spin_around(b):
m_left = Motor(b, PORT_B)
m_left.turn(100, 360)
m_right = Motor(b, PORT_C)
m_right.turn(-100, 360)
print("Script Starting")
b = nxt.locator.find_one_brick()
spin_around(b)
I have installed pyUSB and libUSB. I run the script with python spin.py
However, when I run the script I get the following exception:
Script Starting
Traceback (most recent call last):
File "spin.py", line 14, in <module>
spin_around(b)
File "spin.py", line 8, in spin_around
m_left.turn(100, 360)
File "/Library/Python/2.7/site-packages/nxt/motor.py", line 211, in turn
raise BlockedException("Blocked!")
nxt.motor.BlockedException: Blocked!
Why is this exception occurring?

The script is making calls to motor ports "b" and "c".
If you do not have a motor plugged into port "b" or "c" then this exception will be caused.
To fix this error plug a motor into port "b" and another motor into port "c".

Related

Adafruit MAX 31856 communicating with raspberrypi

I have been trying to read a thermocouple temperature on my raspberrypi however cant get the raspi to communicate with the adafruit MAX 31856. I have downloaded necessary libraries and checked that all connections are secure and correct however keep running into this error when I try to execute this code.
Any help is greatly appreciated!!
CODE
import board
import digitalio
import adafruit_max31856
spi = board.SPI()
cs = digitalio.DigitalInOut(board.D5)
cs.direction = digitalio.Direction.OTUPUT
thermocouple = adafruit_max31856.MAX31856(spi,cs)
print(thermocouple.temperature)
ERROR:
Traceback (most recent call last):
File "/home/pi/test4.py", line 6, in <module>
spi = board.SPI()
File "/usr/local/lib/python3.7/dist-packages/board.py", line 299, in SPI
return busio.SPI(SCLK, MOSI, MISO)
File "/usr/local/lib/python3.7/dist-packages/busio.py", line 289, in __init__
self._spi = _SPI(portId)
File "/usr/local/lib/python3.7/dist-packages/adafruit_blinka/microcontroller/generic_linux/spi.py", line 25, in __init__
self._spi = spi.SPI(device=(portid, 0))
File "/usr/local/lib/python3.7/dist-packages/Adafruit_PureIO/spi.py", line 167, in __init__
raise IOError("{} does not exist".format(device))
OSError: /dev/spidev0.0 does not exist
Code
Errors
Most probably there is a device driver loaded for spi0-0 and that removes /dev/spidev0.0 device.
check your /boot/config.txt for overlays loading MAX 31856 driver module.
if you want to use adafruit_max31856 lib you shouldn't be loading this driver. hope that helps

pynput module won't run and it gives me an error that I don't understand

This is my first time using the pynput module in python, as I run my code it gives me an error that tells me to make sure that I have an "X server running". what does it mean?
Here's the code:
from pynput.mouse import Controller
#x, y are the pixels, top left is (0, 0)
def control_mouse():
mouse = Controller()
mouse.position = (10, 20)
control_mouse()
Here's the error output:
Traceback (most recent call last):
File "/home/user1/PycharmProjects/pythonProject/problemsolving.py", line 1, in <module>
from pynput.mouse import Controller
File "/usr/local/lib/python3.8/dist-packages/pynput/__init__.py", line 40, in <module>
from . import keyboard
File "/usr/local/lib/python3.8/dist-packages/pynput/keyboard/__init__.py", line 31, in <module>
backend = backend(__name__)
File "/usr/local/lib/python3.8/dist-packages/pynput/_util/__init__.py", line 76, in backend
raise ImportError('this platform is not supported: {}'.format(
ImportError: this platform is not supported: ('failed to acquire X connection: Can\'t connect to display ":0.0": b\'No protocol specified\\n\'', DisplayConnectionError(':0.0', b'No protocol specified\n'))
Try one of the following resolutions:
* Please make sure that you have an X server running, and that the DISPLAY environment variable is set correctly
Process finished with exit code 1

getting OSError -202 where running urequests.get from micropy

hi im having error with this code but it runs in python shell could any body help me
from machine import Pin
import time
import network
import urequests
p0 = Pin(0,Pin.OUT)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('ssid', 'pass')
response = urequests.get('http://jsonplaceholder.typicode.com/albums/1')
while True:
ans = response.json()['userId']
p0.value(1)
time.sleep(1)
p0.off()
time.sleep(1)
print('ok')
and this is the error:
Traceback (most recent call last):
File "<stdin>", line 9, in <module>
File "urequests.py", line 108, in get
File "urequests.py", line 53, in request
OSError: -202
Your issue (my guess) is that you begin to urequest.get() without connected to WiFi. Create function that do wifi connection and call it
def do_connect():
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect('essid', 'password')
while not wlan.isconnected():
pass
print('network config:', wlan.ifconfig())
Explain: wlan.connect() is asynchronous function and you have to wait, while it connects to wifi and only then continue with urequest.get()

scapy - Name Error "global name 'Padding' is not defined"

Im trying to write little http proxy server just for testing how scapy handles packet with StreamSocket.
from scapy.all import *
import socket
sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
print "Start"
sk.bind(("127.0.0.1",8090))
sk.listen(1)
conn , addr = sk.accept()
ss = StreamSocket(conn)
r = ss.recv(2048)
print r
ss.close()
When I got the line r = ss.recv(2048) it says (on scapy 2.2.0):
Traceback (most recent call last):
File "<string>", line 254, in run_nodebug
File "C:\Python26\ProxyServer\module1.py", line 21, in <module>
r = ss.recv()
File "C:\Python26\lib\site-packages\scapy\supersocket.py", line 126, in recv
pad = pkt.getlayer(Padding)
NameError: global name 'Padding' is not defined
I have tried to update my scapy (from 2.2.0 to 2.3.2) and it does the same error:
Traceback (most recent call last):
File "<string>", line 254, in run_nodebug
File "C:\Python26\ProxyServer\module1.py", line 21, in <module>
r = ss.recv()
File "C:\Python26\lib\site-packages\scapy\supersocket.py", line 126, in recv
pkt = self.basecls(pkt)
NameError: global name 'Padding' is not defined
Please Help?
sorry for my bad english
Note: I have windows 7 64 bit , python 2.6 and now scapy 2.3.2
I found that after uninstalling and reinstalling scapy I didnt reboot my machine and that made the error!
For people that might get my error about, just remove scapy from your machine and reboot it and install latest scapy version.

Calling method from module

I'm new to python and have issues getting my code working.
I got two different modules, in module a.py I put all my methods, in module b.py I put the logic.
The development environment I'm using is Sypder.
# module a
import serial
ser = serial.Serial()
def serWrite ( str ):
ser.write (str + "\x0D")
print "Write data :", str + "\x0D"
time.sleep(0.1)
return
def configuration():
flagAT = 0
while (flagAT == 0):
serWrite("at")
while True:
ok = ser.readline()
if (ok[0:2] == "OK"):
print ("AT OK, DEVICE CONNECTED" + "\x0D")
flagAT = 1
break
else:
print "DEVICE NOT CONNECTED OR NOT WORKING"
break
print("Starting with configuration")
Module b.py :
#module b
import serial
import a
ser = serial.Serial()
ser.port = "/dev/ttyS1"
ser.baudrate = 115200
### more serial configuration here###
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
configuration()
Now to the issue:
When I run module b.py AFTER running a.py INSIDE Spyder everything is working as intended.
BUT: Spyder code analysis tells me
'a' imported but unused
When I try to call module b.py OUTSIDE Spyder I get following error
pi#(none) ~/WorkingDirectory $ python b.py
Traceback (most recent call last):
File "b.py", line 83, in <module>
configuration()
NameError: name 'configuration' is not defined
Importing configuration() with:
import a
a.configuration()
Gives following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 540, in runfile
execfile(filename, namespace)
File "/home/workingDirectory/test.py", line 85, in <module>
a.configuration()
File "a.py", line 336, in configuration
serWrite("at")
File "a.py", line 16, in serWrite
ser.write (str + "\x0D")
File "build/bdist.linux-i686/egg/serial/serialposix.py", line 490, in write
serial.serialutil.SerialException: Attempting to use a port that is not open
I don't understand why my programm is running within spyder without problems but not outside.
Can someone help here?
You need to change module b to:
#module b
import serial
from a import ser, configuration
#ser = serial.Serial()
ser.port = "/dev/ttyS1"
ser.baudrate = 115200
### more serial configuration here###
try:
ser.open()
except Exception, e:
print "error open serial port: " + str(e)
exit()
configuration()

Categories

Resources