Error message when run sample code

I’m trying to do programming, and firstly I try to run this samples code,(arm_test.py) and then My motor wouldn’t rotate, and I got some error message.

That is;
When I push run,

run arm_test.py
Traceback (most recent call last):
File”/user/lib/python3/dist-
packages/serial/serialposix.py”,line 265,
in open
Self .fd=os.open( self.portstr, os0_RDWR |
os.0_NOCTTY | os.0_NONBLOCK)
PermissionError: [errno 13] permission denied:
‘/dev/ttyAMA0’

And I can’t figure out how to fix the problem….

I’ll paste sample code which I use to make sure below:

from BrickPi import * #import BrickPi.py file to use BrickPi operations
roller =PORT_A
arm= PORT_B
BrickPiSetup() #setup the serial port for communication
BrickPi.MotorEnable[roller] = 1 #Enable the Motor A
BrickPi.MotorEnable[arm] = 1 #Enable the Motor B
BrickPiSetupSensors() #Send the properties of sensors to BrickPi

speed_roller=100
speed_arm=100
t1=.3
t2=.9
while True:
#Move the roller to bring up the page
BrickPi.MotorSpeed[roller] = -speed_roller
ot = time.time()
while(time.time() - ot < t1):
BrickPiUpdateValues()
init_pos=BrickPi.Encoder[arm]
time.sleep(.1) # sleep for 100 ms

time.sleep(2)   
BrickPi.MotorSpeed[roller] = -50
BrickPi.MotorSpeed[arm] = speed_arm  

#Rotate the arm to flip the page and stop at the initial position
while True:
	BrickPiUpdateValues()     
	if(BrickPi.Encoder[arm]-init_pos>710):
		BrickPi.MotorSpeed[arm] = -85
		BrickPiUpdateValues()
		time.sleep(.1) 
		BrickPi.MotorSpeed[arm] = 0
		BrickPiUpdateValues()
		time.sleep(.01) 
		break
	time.sleep(.01)              # sleep for 100 ms

#Move the roller to bring pages down
time.sleep(2)   
BrickPiUpdateValues()
BrickPi.MotorSpeed[roller] = 60
BrickPi.MotorSpeed[arm] = 0  
ot = time.time()
while(time.time() - ot < 3):    #running while loop for 3 seconds
	BrickPiUpdateValues()       # Ask BrickPi to update values for sensors/motors
	time.sleep(.1)             # sleep for 100 ms
time.sleep(4)
1 Like

Sorry - I don’t understand “push run”.

Typically, when I see Permission Denied when running a Python program, I try running the the program by typing “sudo” in front of the command to run the program:

sudo python arm_test.py
1 Like

What operating system are you using?

Are you running as the “pi” user?

I can’t speak for the brick-Pi, but most, (if not all), of the Dexter Industries software/operating systems expect you to be the “pi” user.  Others have tried running this stuff as a different user and had nothing but problems.

1 Like

I’m afraid, do you meant to open terminal ( looks like command prompt) and type Sudo Python arm_test.py ?

I tried that and replied ;
Python: can’t open file ‘arm_test.py’ : [errno 2] no such file or directory

But I don’t understand because I have my Python file on the desktop for sure…. It says no such file…

1 Like

I’m using vnc .
And sorry for my foolishness,
I don’t know what pi user is. I’ll try to find out , but i never asked an password and username while I enter to VNC.
so, how can I running as a pi user??
Sorry to ask so basic question but I’m really in trouble I hope you to help me…

2 Likes

Yes, to use sudo the command must be issued from a terminal. When you open a terminal it will usually open in the user’s home directory.

To find out what user you are, type “whoami”. Most of the time on Raspberry Pi, the user is “pi”

$ whoami
pi

To see what “working directory” your terminal is currently, type “pwd” (stands for print working directory):

$ pwd
/home/pi

To see what subfolders exist, and to find your program you created “on the desktop”, list the files and subfolders of the current working directory:

$ ls
Bookshelf  Desktop  di_update  Downloads  ip_feedback.sh   Public   Scratch  Templates  Videos
Dexter   Documents  ipcount    ip.number    Music   Pictures   sdcard_expansion  usb_disk

So the current working directory is /home/pi and the Desktop is a subfolder (/home/pi/Desktop) and your arm_test.py should be in the Desktop subfolder. Look for it by listing the files of the Desktop:

(assuming pwd returned /home/pi)
$ ls Desktop
... arm_test.py ...

In order to run arm_test.py we need to either change the working directory to be the Desktop, or copy the file from the desktop to the current directory.

Changing to the Desktop will probably work best for you:

To change the working directory to the Desktop folder, we use “cd” (stands for change directory):

(again assuming the terminal opened in /home/pi)
$ cd Desktop
$ pwd
/home/pi/Desktop
$ ls
... arm_test.py ...

So now that the terminal working directory contains the file we want to run, we can execute our command with sudo:

$ sudo python arm_test.py

Hopefully this will get you a little further along.

1 Like

I’m so grateful for your quick reply…!
And I allowed all of you instruction, and it seems worked perfectly ( cause it really same as your instruction) until in the end, while I typed $ sudo python arm_test.py,
Then it reply with new error message!
It says;
Traceback (most recent call last):
File “arm_test.py”,line 29, in
If (BrickPi.Encoder[arm]-init_pos>710):
TypeError: unsupported operand type (s) for -: ‘NoneType’ and ‘NoneType’

…the directory has changed to desktop and I have no idea is this about any error on the programming code?

Sorry to ask you frequently…
I don’t really get this error message…

1 Like

It is trying to tell you that BrickPi.Encoder[arm] did not return a number here or before when saved in init_pos.

Start by printing out the value of init_pos and try to find out why the encoder value is not being returned or is not a number.

1 Like

Thank you for explaining.
I’m totally new to python and not to good in coding…
I take a look at my code and , find this line,

While true :
BrickPiUpdateValues()
If (BrickPi. Encoder[arm]-init_pos>710);
BrickPi.MotorSpeed[arm]= -85
BrickPiUpdateValue()
time.sleep(.1)
BrickPi.MotorSpeed[arm] = 0
BrickPiUpdateValues()
time.sleep(.01)
break
time.sleep(.01)

And I think it’s refer to the error message.

And I browse ( about None) and find the article say, The sole value of the type NoneType. None is frequently used to represent the absence of a value, as when default arguments are not passed to a function. Assignments to None are illegal and raise a SyntaxError.

But, this leave me wondering, where can I put value?
How to printing out the value of init_pos?

1 Like

Spend an hour learning the basics of Python. Invest in yourself.

1 Like

When Python throws an error, there is usually both an error message and a “traceback” containing a lot of useful data.

Can you capture and send the entire error and traceback?

@cyclicalobsessive is absolutely correct.

Any programming language is a tool you need to learn how to use.

What is your native language and where are you from?

Depending on where you live, it might be possible to get books on Python in your native language, which will make understanding Python much easier for you.

You also need to learn how to use the programming tools, (like Thonny or Geany), as they have features that will allow you to understand exactly what the problem might be.

I have a suggestion for you:

Is it possible for you to stop working on your robot for a while?

It would be very helpful if you can just hook up a Raspberry Pi by itself, (in a case is best), and just practice using and programming the Raspberry Pi?

There are a number of good “beginner” books for the Raspberry Pi.  Unfortunately all the ones that I know of are in English, but it is very possible that good books exist in your native language as well.

See what you find and ask any questions you want, even if it’s not exactly about your robot.
:+1: :wink:

1 Like