Raspberry pi pico making loop / OSError: core1 in use

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
berke
Posts: 17
Joined: Wed Sep 01, 2021 7:45 am

Raspberry pi pico making loop / OSError: core1 in use

Post by berke » Mon Nov 15, 2021 7:04 am

Hello everyone, ı am using raspberry pi pico. When I write this line, stepper's full rotation is 5 seconds.

Code: Select all

def runstepper():
        enaPin.low()
        pulPin.low()
        time.sleep_ms(1)
        pulPin.high()
        time.sleep_ms(1)
while True:
    runstepper()
When I wrote this code, stepper's full rotation is 2.24 minute. It is too slow. How can I make it faster like first code? Should I use second _thread? I tried with that , it is working but I can't do my async tasks. Any advice? I am using micropython since august, I used some sensors and servos before. Thank you.

Code: Select all

def calcWeight():
    val = hx711.get_value()
    print(val*5/scale)
    time.sleep_ms(1)
def runstepper():
        enaPin.low()
        pulPin.low()
        time.sleep_ms(1)
        pulPin.high()
        time.sleep_ms(1)
while True:
    runstepper()
    calcWeight()
If I use _thread, my code is working but only one time. After that it gives OSError:core1 in use . I know this error. This error appears because I am using _thread in

Code: Select all

while True:
Here is working loop but only one time then
core 1 in use error
appears. Is there a way that I can pass this error even I use while True ?

Code: Select all

def main():
    while True:
            response = http.GET("example.com")
            _thread.start_new_thread(measureweight_thread, ())
            utime.sleep(0.01)
            runstepper()
            time.sleep(1)
            relaylow()
            time.sleep(2)
            relayhigh()
            utime.sleep(0.01)
main()

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Raspberry pi pico making loop / OSError: core1 in use

Post by pythoncoder » Mon Nov 15, 2021 9:36 am

You can only start a new thread once because you only have one spare core. If you want the motor to run continuously you want to do something like:

Code: Select all

def runstepper():
    enaPin.low()
    while True:
        pulPin.low()
        time.sleep_ms(1)
        pulPin.high()
        time.sleep_ms(1)
Then, in your main code, run this once only:

Code: Select all

_thread.start_new_thread(runstepper, ())
If you want to be able to start and stop the motor, share a boolean variable between your main code and the runstepper thread.
Peter Hinch
Index to my micropython libraries.

berke
Posts: 17
Joined: Wed Sep 01, 2021 7:45 am

Re: Raspberry pi pico making loop / OSError: core1 in use

Post by berke » Mon Nov 15, 2021 10:11 am

pythoncoder wrote:
Mon Nov 15, 2021 9:36 am
You can only start a new thread once because you only have one spare core. If you want the motor to run continuously you want to do something like:

Code: Select all

def runstepper():
    enaPin.low()
    while True:
        pulPin.low()
        time.sleep_ms(1)
        pulPin.high()
        time.sleep_ms(1)
When I do something like that for hx711, it stops. It is not working continuously. It works for one line then hx711 stops.

Code: Select all

current_value=False
def measureweight_thread():
    global current_value
    while trigger_hx711:
        value = hx711.get_value()
        time.sleep_us(300000)  #  I tried without time.sleep or lower valus, nothing change. If I decrease 300000 , hx711 send 2 value or 5 value instead of 1.
        print(value*5/scale)
        if(value>=0.2):
            current_value=True
        utime.sleep(0.01)      
_thread.start_new_thread(measureweight_thread, ())

Code: Select all

def main():
    while True:
            response = http.GET("example.com") 
            runstepper()
            time.sleep(1)
            relaylow()
            time.sleep(2)
            relayhigh()

main()

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Raspberry pi pico making loop / OSError: core1 in use

Post by pythoncoder » Tue Nov 16, 2021 3:37 pm

It is possible you're running into this issue. I have encountered erratic behaviour accessing UARTs and SPI devices on the second core.

You might want to consider achieving concurrency with uasyncio rather than dual core.
Peter Hinch
Index to my micropython libraries.

berke
Posts: 17
Joined: Wed Sep 01, 2021 7:45 am

Re: Raspberry pi pico making loop / OSError: core1 in use

Post by berke » Wed Nov 17, 2021 5:02 am

pythoncoder wrote:
Tue Nov 16, 2021 3:37 pm
It is possible you're running into this issue. I have encountered erratic behaviour accessing UARTs and SPI devices on the second core.

You might want to consider achieving concurrency with uasyncio rather than dual core.
Yes I was using UART for http response. That's why I guess. Now I am not using second core, I write PWM for stepper thanks to @horuable. Now It is working as I want.

Post Reply