PeterStaev / lego-spikeprime-mindstorms-vscode

Helps you connect and work with Lego's SPIKE Prime/MINDSTORMS Robot Inventor hubs
Apache License 2.0
64 stars 22 forks source link

Can't exit program after uploading and starting #36

Closed ADScomics closed 2 years ago

ADScomics commented 2 years ago

On macOS Monterey 12.2.1. Upon running a program, if I terminate the program, it doesn't appear to exit the program on the hub. Pressing the center button does nothing either, so I need to hold the button down to restart the hub. I have my hub plugged in via USB. Don't know if this could have any bearing on the problem, but I did go back and forth between the Pybricks firmware, and the default firmware (as in, uninstalling and reinstalling). Unsure if that could have affected the brick in any way.

PeterStaev commented 2 years ago

Hey @ADScomics , when you say "terminate a program" do you mean the command that the plugin provides or you terminate it in the Python code?

ADScomics commented 2 years ago

Hi @PeterStaev, It's when I terminate it with the plugin command via the button next to 'play'. Even just pressing the home button to exit the program does not seem to work for me either. Here's my code if it helps at all. The program itself seems to run properly (Although I'm not sure where it's accessing the code libraries on the top).

# LEGO type:standard slot:1 autostart

from mindstorms import MSHub, Motor, MotorPair, ColorSensor, DistanceSensor, App
from mindstorms.control import wait_for_seconds, wait_until, Timer
from mindstorms.operator import greater_than, greater_than_or_equal_to, less_than, less_than_or_equal_to, equal_to, not_equal_to
import math

# Create your objects here.
hub = MSHub()
eyes = DistanceSensor('A')
foldingMechanism = MotorPair('C', 'D')
steeringMechanism = Motor('B')
rearWheels = Motor('E')
speed = 0
def BackUp():
    global speed
    speed = 0
    speed = 30
    if (dist > 60):
        speed = 0

# Write your program here.
#hub.light_matrix.write('Initialized!')
hub.light_matrix.write('Hi!')
hub.light_matrix.write('Hey!')
hub.speaker.start_sound('Hi',100)
while True:
    dist = eyes.get_distance_cm()
    rearWheels.start(speed)
    if hub.right_button.is_pressed():
        speed = -100
    if hub.left_button.is_pressed():
        speed = 0
    #if dist != None and dist < 30:
     #   BackUp()
PeterStaev commented 2 years ago

From what I see from your code you have an endless loop on the main thread. This is what stalls the hub and that's why the central button does not end the program. If you try the same code in the Mindstorms/SPIKE app probably the result will be the same 😃

ADScomics commented 2 years ago

Gah, that was it! Strangely enough, the Mindstorms app didn't have that same issue, although uploading and running the code was finicky a lot, so maybe that had something to do with it. Thanks for your help! I come from object oriented programming in C sharp, so Python is very new to me. 😅