Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1579

Python • Help with running script in both sudo and venv

$
0
0
Hi, I am pretty new to this RPi thing and I am trying to take on a gig that has the RPi as the main controller for a device, it seemed simple enough at first.

My RPi has a script that I want to run on startup. I tried using crontab for this but I cannot get it to work, which I assume has something to do with what I'm about to say next

To test if crontab was working properly, I tried to simply run my script. My script uses gpiozero, pins 4-9, and a python module called roboflow (it's for image detection)

From my research I've found that I need to have a virtual environment to use the roboflow module in my RPi, but also root access so the script can use the pins. Not only that but I also need RPi.GPIO installed for gpiozero to work, so I've been running my script on both sudo and venv which I think is working but has led me to the problem of "RuntimeError: Failed to add edge detection"

Upon trying a few more things and (shamefully) consulting GPT, it is now 1am in my timezone and I still have yet to make it work. If I could get help or at a least a nudge in the right direction that'd be great! Thank you in advance:)

Code:

from  gpiozero import Button, LED, AngularServo, DigitalOutputDevicefrom time import sleepfrom signal import pausefrom roboflow import Roboflow### Edit these numbers to tweak the rotation of the motors and the pins of the input/output and other things, I hope I've made the variable names intuitive enough# Pin outbuttonPin = 4entranceServoPin = 5separatorServoPin = 6ledGreenPin = 7ledRedPin = 8pumpPin = 9# Servo variablesservoMinAngle = 0servoMaxAngle = 180openAngle = 120closeAngle = 0bottleAngle = 80canAngle = 160# MiscellaneouspumpSleepTime = 5 # How long to keep the pump on, essentially how much water it dispensesflashSpeed = 2 # How fast the red led flashesacceptDelay = 5 # How long to keep the entrance servo open# Setting the pin outs of the devices, as well as passing arguments like angles for the servo motorsledGreen = LED(ledGreenPin)ledRed = LED(ledRedPin)button = Button(buttonPin)pump = DigitalOutputDevice(pumpPin)entranceServo = AngularServo(entranceServoPin, min_angle=servoMinAngle, max_angle=servoMaxAngle, initial_angle=closeAngle)separatorServo = AngularServo(separatorServoPin, min_angle=servoMinAngle, max_angle=servoMaxAngle, initial_angle=closeAngle) #Importing the object detection modelcanBottleAPI = NonemodelName = Nonerf = Roboflow(api_key=canBottleAPI)project = rf.workspace().project(modelName)model = project.version(1).model#InitializeentranceServo.angle = closeAngleledGreen.off()ledRed.off()pump.off()# camera.on() idk what camera you guys are using so i can't code this part yetdef deny():    entranceServo.angle = closeAngle    ledRed.on()    sleep(5)    ledRed.off()    returndef dispense():    pump.on()    sleep(pumpSleepTime)    pump.off()    returndef accept(materialType):    ledGreen.on()    entranceServo.angle = openAngle    sleep(acceptDelay)    entranceServo.angle = closeAngle    if materialType == "bottle":        separatorServo.angle = bottleAngle    elif materialType == "can":        separatorServo.angle = canAngle    dispense()    returndef checkObject():    insertedObject = None    material = model.predict(insertedObject, confidence=50, overlap=30).json()    if material == "bottle":        accept("bottle")        return    elif material == "can":        accept("can")        return    deny()    returnif __name__ == "__main__":        button.when_pressed = checkObject        pause()

Statistics: Posted by Cyroshadow — Mon Nov 17, 2025 2:17 pm



Viewing all articles
Browse latest Browse all 1579

Trending Articles