Matchpole
Posts: 13
Joined: Wed May 22, 2013 10:49 am

GPIO errors

Thu Mar 04, 2021 7:17 pm

HELP...... I am trying to merge some py programs to improve my servo controling. The first uses 'pygame' to get keyboard inputs, as in 'event.key == pygame.K_r', this responds when the user presses 'r' on the keyboard and is followed by whatever code to action what you want. Apart from importing pygame this also requires 'GPIO.setmode(GPIO.BOARD)'.

The second program is using the Adafruit PCA9685 which can control up to 16 servos via i2c. This requires 'from board import SCL, SDA'.

This clashes with 'GPIO.setmode.....' above with the error 'ValueError: A different mode has already been set!'.

I have been trying to get into the 'board library' to see what is in there and find a work around, without any success.

Has anybody out there any ideas, PLEASE.

Cheers,

Dave.

gordon77
Posts: 6120
Joined: Sun Aug 05, 2012 3:12 pm

Re: GPIO errors

Thu Mar 04, 2021 7:19 pm

Can you upload your code so we can see it ?

Matchpole
Posts: 13
Joined: Wed May 22, 2013 10:49 am

Re: GPIO errors

Thu Mar 04, 2021 7:28 pm

It is rather large but I will try.

Matchpole
Posts: 13
Joined: Wed May 22, 2013 10:49 am

Re: GPIO errors

Thu Mar 04, 2021 7:44 pm

This code is still rather messy, not finished expanding it but this is as far as I have got. Can you tell me how to add an attachment. I have tried to 'add files' it says it is an 'invalid extension', I removed the '.py' it still will not accept it.

Matchpole
Posts: 13
Joined: Wed May 22, 2013 10:49 am

Re: GPIO errors

Thu Mar 04, 2021 7:54 pm

Lets try a copy and paste. OK


# import curses and GPIO
import pygame
import RPi.GPIO as GPIO
import os #added so we can shut down OK
import time #import time module

Code: Select all

################# From Camera_PandT_4.py #################
from board import SCL, SDA # WILL NOT WORK WITH LINE 61 "GPIO.setmode(GPIO.BOARD)"
import busio

# Line below from "PCA9685_simpletest.py"
import Adafruit_PCA9685
# Line below from "PCA9685_simpletest.py"
# Initialise the PCA9685 using the default address (0x40).
pwm = Adafruit_PCA9685.PCA9685()

# Import the PCA9685 module.
from adafruit_pca9685 import PCA9685
from adafruit_motor import servo

# Create the I2C bus interface.
i2c = busio.I2C(SCL, SDA)

# Create a simple PCA9685 class instance.
pca = PCA9685(i2c)
# See the calibration.py example in the PCA9685 driver
# pca = PCA9685(i2c, reference_clock_speed = 25630710)

# Set the PWM frequency to 60hz.
pca.frequency = 50

# Adjust min_pulse and max_pulse to get the full range of movement
# See 'Sub-micro servo - https://www.adafruit.com/product/2201
# Pulse range is 750 - 2250 by default, this gives 135 degrees of movement

servo1 = servo.Servo(pca.channels[1], min_pulse = 800, max_pulse = 3250)
servo0 = servo.Servo(pca.channels[0], min_pulse = 1100, max_pulse = 3200)

start = 90
servo0.angle = start
servo1.angle = start
pan = start
tilt = start
###########################################################

#import picamera and datetime (to use in unique file naming)
from picamera import PiCamera
from datetime import datetime

#Open a Pygame window to allow it to detect user events
screen = pygame.display.set_mode([340, 160])

#setup camera
camera = PiCamera()
camera.resolution = (1280,720)
camera.framerate = (25)

record = 0 #set up a variable to be set to 1 when recording

#set GPIO numbering mode and define output pins
GPIO.setmode(GPIO.BOARD)
GPIO.setup(35,GPIO.OUT)
GPIO.setup(36,GPIO.OUT)
GPIO.setup(37,GPIO.OUT)
GPIO.setup(38,GPIO.OUT)
GPIO.setup(40,GPIO.OUT)

#setup PWM control
GPIO.setup(33,GPIO.OUT)
GPIO.setup(32,GPIO.OUT)
speedleft = GPIO.PWM(33,100)
speedright = GPIO.PWM(32,100)
print(" Speed Left Start ")
speedleft.start(100)
print(" Speed Right Start ")
speedright.start(100)

#flashes LEDs when all running, and also lets camera settle
for x in range(1, 5):
    GPIO.output(40,False)
    time.sleep(0.5)
    GPIO.output(40,True)
    time.sleep(0.5)

try:
    while True:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    pygame.quit()
                elif event.key == pygame.K_s:
                    os.system ('sudo shutdown now') # shutdown right now!
                elif event.key == pygame.K_r:
                    if record == 0:
                        record = 1
                        moment = datetime.now()
                        GPIO.output(40,False)
                        camera.start_preview()
                        #camera.start_recording('/home/pi/Videos/vid_%02d_%02d_%02d.mjpg' % (moment.hour, moment.minute, moment.second))
                elif event.key == pygame.K_t:
                    if record == 1:
                        record = 0
                        GPIO.output(40,True)
                        camera.stop_preview()
                        #camera.stop_recording()
                elif event.key == pygame.K_RIGHT:
                    GPIO.output(35,True)
                    GPIO.output(36,False)
                    GPIO.output(37,False)
                    GPIO.output(38,True)
                elif event.key == pygame.K_LEFT:
                    GPIO.output(35,False)
                    GPIO.output(36,True)
                    GPIO.output(37,True)
                    GPIO.output(38,False)
                elif event.key == pygame.K_UP:
                    GPIO.output(35,False)
                    GPIO.output(36,True)
                    GPIO.output(37,False)
                    GPIO.output(38,True)
                elif event.key == pygame.K_DOWN:
                    GPIO.output(35,True)
                    GPIO.output(36,False)
                    GPIO.output(37,True)
                    GPIO.output(38,False)
                elif event.key == pygame.K_1:
                    speedleft.ChangeDutyCycle(10)
                    speedright.ChangeDutyCycle(10)
                elif event.key == pygame.K_2:
                    speedleft.ChangeDutyCycle(55)
                    speedright.ChangeDutyCycle(55)
                elif event.key == pygame.K_3:
                    speedleft.ChangeDutyCycle(100)
                    speedright.ChangeDutyCycle(100)
            elif event.type == pygame.KEYUP:
                GPIO.output(35,False)
                GPIO.output(36,False)
                GPIO.output(37,False)
                GPIO.output(38,False)
                         
finally:
    #GPIO cleanup
    GPIO.cleanup()

gordon77
Posts: 6120
Joined: Sun Aug 05, 2012 3:12 pm

Re: GPIO errors

Thu Mar 04, 2021 8:22 pm

Maybe it has already set BCM mode.

What if you get rid of the BOARD line and renumber the gpio entries to bcm numbers?

#GPIO.setmode(GPIO.BOARD)
GPIO.setup(19,GPIO.OUT)

Etc...
Last edited by gordon77 on Thu Mar 04, 2021 9:17 pm, edited 1 time in total.

Matchpole
Posts: 13
Joined: Wed May 22, 2013 10:49 am

Re: GPIO errors

Thu Mar 04, 2021 9:01 pm

That works fine. I should have done that myself but there was a lot of numbers to be changed. Next. At the moment I have just got this program flashing a load of LED's to signify which GPIO pins are high and the pwm for speed indication/control, these are to be changed from the output to a driver board for 2 DC motors. My next move is to drive either stepper motors or continuous servos, plus a pan and tilt camera with a robot arm, probably some working lights too.

Thank you very much for your time. I could have saved myself a few days of head scratching.

Cheers,

Dave.

Return to “Advanced users”