Page 1 of 1

Thonny Strech error

Posted: Mon Sep 09, 2019 1:38 am
by s-petersen
Hi,
I just updated from Jesse to the latest Stretch, and I am getting this error while trying to run a script(in Thiony) that ran fine before Any ideas?

It says internal error

Code: Select all

Traceback (most recent call last):
  File "/usr/lib/python3.5/tkinter/__init__.py", line 1562, in __call__
    return self.func(*args)
  File "/usr/lib/python3/dist-packages/thonny/running.py", line 317, in _cmd_run_current_script
    self.execute_current("Run")
  File "/usr/lib/python3/dist-packages/thonny/running.py", line 295, in execute_current
    self.execute_script(filename, args, working_directory, command_name)
  File "/usr/lib/python3/dist-packages/thonny/running.py", line 266, in execute_script
    get_shell().submit_magic_command(cd_cmd_line + exe_cmd_line)
  File "/usr/lib/python3/dist-packages/thonny/shell.py", line 98, in submit_magic_command
    self.text.submit_command(cmd_line, ("magic",))
  File "/usr/lib/python3/dist-packages/thonny/shell.py", line 201, in submit_command
    assert get_runner().is_waiting_toplevel_command()
AssertionError
It's worse than I thought, whatever is happening the gpio stays the same when I try to run the program again (even executed directly)
I need to hook up everything to see if it is even running, I'll get back when I try that.

Re: Thonny strech error

Posted: Mon Sep 09, 2019 2:56 am
by s-petersen
Ok, I found out some more info:
The program does still run, it just dosen't change the state of the LED when it is first started, it did before, now it does not.
The error displayed in the last post, happens if you press" run current script" twice, without stopping the running program.
Here is my program, on Jesse it would always start the led in blue no matter what the previous state was.
now, in Stretch you have to have to change state in the program to change it to get it working correctly.

Code: Select all

#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (C)2013 Steven Devlan, 2015 Paul van Veen, mod by Chris Schaab, Scott Petersen

import RPi.GPIO as GPIO
import os, sys, time

#contants and literals
SELECTION_LETTERS=("V","U","T","S","R","Q","P","N","M","L","K","J","H","G","F","E","D","C","B","A")
WALLBOX=20
dirsong=("Blue")
#>>>these constants can be changed to fit the characteristics of your wallbox
MAXMIMUM_GAP=2
MINIMUM_PULSE_GAP_WIDTH=0.014
LETTER_NUMBER_GAP=0.2
song="error"                                                               
#set up IO port for input
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(WALLBOX, GPIO.IN)
GPIO.setup(9, GPIO.OUT)
GPIO.setup(10, GPIO.OUT)


#this function tests if a pulse or gap is wide enough to be registered
#this is needed for two reasons. 1) Sometimes the wallbox will generate an errant pulse
#which will cause errors if interpretted as a proper contact pulse 2) because of the
#way that I have tapped the wallbox pulses, there will be short gaps inside each pulse
#that need to be ignored

def state_has_changed(starting_state):
    starting_time = time.time()
    elapsed_time = 0

    for i in range (200):
        if not GPIO.input(WALLBOX) != starting_state: 
            elapsed_time = time.time() - starting_time
#            print ("check time recorded: %.3f" %elapsed_time)
            return False
    return True
        
#this function is called as soon as the main loop determines that a train of pulses
#has started.  It begins by counting the number pulses, then when it encounters a larger
#gap, it starts incrementing the letters.  If your wallbox uses the opposite order
#you will need to change this.  Also the final calculation of the track may need to be changed
#as some boxes have additional pulses at either the start or the end of the train
#once it encounters a gap over a pre-determined maxmimum we know that the rotator arm
#has stopped and we calculate the track 

def calculate_track():
    global song                                                 # ***** added *******
    global dirsong
    state = True
    count_of_number_pulses = 9 #since we are in the first pulse
    count_of_letter_pulses = 0
    length_of_last_gap = 0
    first_train = True
    time_of_last_gap = time.time()
    
    while length_of_last_gap < MAXMIMUM_GAP:
        if not GPIO.input(WALLBOX) != state: 
            
            if state_has_changed(not state): # state has changed but check it is not anomaly
                state = not state # I use this rather than the GPIO value just in case GPIO has changed - unlikely but possible
                if state: #indicates we're in a new pulse
                    length_of_last_gap = time.time() - time_of_last_gap 
                    print ("Pulse.  Last gap: %.3f" %length_of_last_gap)

                    if length_of_last_gap > LETTER_NUMBER_GAP: # indicates we're into the second train of pulses
                        first_train = False

                    if first_train:
                        if length_of_last_gap > MINIMUM_PULSE_GAP_WIDTH:  count_of_number_pulses -= 1
                    else:
                         count_of_letter_pulses +=1
                         if count_of_number_pulses > 9: count_of_number_pulses = 9
                         if count_of_letter_pulses > 20: count_of_letter_pulses = 20
                else: #indicates we're in a new gap
                    time_of_last_gap = time.time()
        else:
            length_of_last_gap = time.time() - time_of_last_gap #update gap length and continue to poll
    song =  SELECTION_LETTERS[count_of_letter_pulses -1] + str((count_of_number_pulses))

    print (dirsong+","+song)
    
    with open('/media/pi/MUSIC/Queue.csv',"a") as f:
           f.write(dirsong+","+song+"\n")
#while True:  
    if  dirsong == "Blue" and song == "V8":
         dirsong = "Red"
                  
    elif dirsong == "Red" and song == "V8":
         dirsong = "Blue"
                  
  
    if   dirsong == "Blue":
         GPIO.output(9,0)
         GPIO.output(10,1)
    if   dirsong == "Red":
         GPIO.output(9,1)
         GPIO.output(10,0)
         
                    
#this is the main loop.  We poll the GPIO port until there is a pulse.
#sometimes there can be random pulses, or a spike when the rotor arm starts to move 
#so before trying to decode the pulse train I check that
#the pulse is long enough to indicate a contact on the selector arm

while True:
    if  GPIO.wait_for_edge(WALLBOX, GPIO.BOTH):
         if state_has_changed(True):
            track = calculate_track()
        
        
    else:
         if  dirsong == "Blue" and song == "V8":
             dirsong = "Red"
                  
         elif dirsong == "Red" and song == "V8":
              dirsong = "Blue"
                  
  
    if   dirsong == "Blue":
             GPIO.output(9,0)
             GPIO.output(10,1)
    if   dirsong == "Red":
            GPIO.output(9,1)
            GPIO.output(10,0)             
             
            time.sleep(.001)
            print ("--> Pulse ignored")

Re: Thonny strech error

Posted: Mon Sep 09, 2019 5:18 am
by B.Goode
A couple of related observations:

The script was originally written for use with the Python2 variant of the Python interpreter - the Thonny IDE on the Raspbian Operating System uses Python3.

Even 'the latest Stretch' is no longer the current supported Raspbian Operating System - it was superceded by Raspbian Buster earlier this summer (2019.)

Re: Thonny strech error

Posted: Mon Sep 09, 2019 5:52 am
by s-petersen
I've already heard of issues with Buster and Pi Zero, Why do people feel they have to rush through version after version, and never really finish them? With a life cycle of only a year or 2 how is anyone going to benefit?

Re: Thonny Strech error

Posted: Mon Sep 16, 2019 1:27 am
by s-petersen
More weird things From cron

Code: Select all

@reboot sudo python3 /home/pi/jukebox/jukebutns5.py &
does not run(i can see no error output )
but

Code: Select all

@reboot sudo /home/pi/jukebox/./jukebutns5.py &
does run, Why?

Re: Thonny Strech error

Posted: Mon Sep 16, 2019 1:50 am
by Andyroo
The first one will not know where Python is where I expect the second one has a full hash bang entry.

If you replace the python3 with /usr/bin/python3 then the first should run.

The hash bang tells the system what program should be used to execute the code. So for a Python 3 program the first line would be

Code: Select all

#!/usr/bin/python3
Try to get out of the habit of using Sudo - it’s best to set the correct security rights up rather than default to root. If you have a file name error or security breach you are giving the program access to most things.

Re: Thonny Strech error

Posted: Mon Sep 16, 2019 8:35 am
by s-petersen
Thank You!
This is going in a completely embedded stand alone jukebox,(no network) so there is no need for security, and sometimes sequrity would get in the way for easy repair.
I was having lots of trouble accessing the USB drive and wanted to make sure the program would have access no matter what.
It's running on Stretch Lite, and getting USB working was a pain in the command line.
If I work on something that needs to be secure I will certainly follow your advice.
The odd thing is , a companion program(the player) runs in either format.
Could they are running different versions of python?