diff --git a/PCA9685GpioExample.java b/PCA9685GpioExample.java new file mode 100644 index 0000000..00765c7 --- /dev/null +++ b/PCA9685GpioExample.java @@ -0,0 +1,581 @@ + +/* + * #%L + * ********************************************************************** + * ORGANIZATION : Pi4J + * PROJECT : Pi4J :: Java Examples + * FILENAME : PCA9685GpioServoExample.java + * + * This file is part of the Pi4J project. More information about + * this project can be found here: http://www.pi4j.com/ + * ********************************************************************** + * %% + * Copyright (C) 2012 - 2013 Pi4J + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + import java.io.IOException; + import java.math.BigDecimal; + import java.util.Scanner; + + import com.pi4j.component.servo.Servo; + import com.pi4j.component.servo.impl.GenericServo; + import com.pi4j.component.servo.impl.GenericServo.Orientation; + import com.pi4j.component.servo.impl.PCA9685GpioServoProvider; + import com.pi4j.gpio.extension.pca.PCA9685GpioProvider; + import com.pi4j.gpio.extension.pca.PCA9685Pin; + import com.pi4j.io.gpio.GpioController; + import com.pi4j.io.gpio.GpioFactory; + import com.pi4j.io.gpio.GpioPinPwmOutput; + import com.pi4j.io.i2c.I2CBus; + import com.pi4j.io.i2c.I2CFactory; + + /** + * Simple servo tester application demonstrating Pi4J's Servo component. + * + * @author Christian Wehrli + * @see Servo + * @see com.pi4j.gpio.extension.pca.PCA9685GpioProvider + */ + public class PCA9685GpioServoExample { + + //------------------------------------------------------------------------------------------------------------------ + // main + //------------------------------------------------------------------------------------------------------------------ + public static void main(String args[]) throws Exception { + System.out.println("<--Pi4J--> PCA9685 Servo Tester Example ... started."); + PCA9685GpioServoExample example = new PCA9685GpioServoExample(); + Scanner scanner = new Scanner(System.in); + char command = ' '; + while (command != 'x') { + printUsage(); + command = readCommand(scanner); + switch (command) { + case 'c' : // Choose Channel + example.chooseChannel(scanner); + break; + case 'n' : // Neutral Position + example.approachNeutralPosition(); + break; + case 'm' : // Move + example.move(scanner); + break; + case 's' : // Sub Trim + example.subtrim(scanner); + break; + case 'r' : // Reverse + example.reverse(); + break; + case 't' : // Travel (adjust endpoints) + example.travel(scanner); + break; + case 'p' : // Sweep + example.sweep(scanner); + break; + case 'i' : // Info + example.info(); + break; + case 'x' : // Exit + System.out.println("Servo Example - END."); + break; + case ' ' : + System.err.println("Invalid input."); + break; + default : + System.err.println("Unknown command [" + command + "]."); + break; + } + } + } + + private static char readCommand(Scanner scanner) { + char result = ' '; + String input = scanner.nextLine(); + if (input.trim().isEmpty() == false) { + result = input.trim().toLowerCase().charAt(0); + } + return result; + } + + private static void printUsage() { + System.out.println(""); + System.out.println("|- Commands ---------------------------------------------------------------------"); + System.out.println("| c : choose active servo channel "); + System.out.println("| n : neutral - approach neutral position "); + System.out.println("| m : move servo position "); + System.out.println("| s : subtrim "); + System.out.println("| r : reverse servo direction "); + System.out.println("| t : travel - adjust endpoints "); + System.out.println("| p : sweep - continuously move between max left and max right position) "); + System.out.println("| i : info - provide info for all servo channels "); + System.out.println("| x : exit "); + System.out.println("|--------------------------------------------------------------------------------"); + } + + //------------------------------------------------------------------------------------------------------------------ + // PCA9685GpioProvider + //------------------------------------------------------------------------------------------------------------------ + private final PCA9685GpioProvider gpioProvider; + private final PCA9685GpioServoProvider gpioServoProvider; + + private final Servo[] servos; + private int activeServo; + + public PCA9685GpioServoExample() throws Exception { + gpioProvider = createProvider(); + + // Define outputs in use for this example + provisionPwmOutputs(gpioProvider); + + gpioServoProvider = new PCA9685GpioServoProvider(gpioProvider); + + servos = new Servo[16]; + + // Provide servo on channel 0 + servos[0] = new GenericServo(gpioServoProvider.getServoDriver(PCA9685Pin.PWM_00), "Servo_1 (default settings)"); + + // Provide servo on channel 1 + servos[1] = new GenericServo(gpioServoProvider.getServoDriver(PCA9685Pin.PWM_01), "Servo_2 (max. endpoints)"); + servos[1].setProperty(Servo.PROP_END_POINT_LEFT, Float.toString(Servo.END_POINT_MAX)); + servos[1].setProperty(Servo.PROP_END_POINT_RIGHT, Float.toString(Servo.END_POINT_MAX)); + + // Provide servo on channel 2 + servos[2] = new GenericServo(gpioServoProvider.getServoDriver(PCA9685Pin.PWM_02), "Servo_3 (subtrim)"); + servos[2].setProperty(Servo.PROP_SUBTRIM, Float.toString(Servo.SUBTRIM_MAX_LEFT)); + + // Provide servo on channel 3 + servos[3] = new GenericServo(gpioServoProvider.getServoDriver(PCA9685Pin.PWM_03), "Servo_4 (reverse)"); + servos[3].setProperty(Servo.PROP_IS_REVERSE, Boolean.toString(true)); + + // Set active servo + activeServo = 0; + } + + public void chooseChannel(Scanner scanner) { + System.out.println(""); + System.out.println("|- Choose channel ---------------------------------------------------------------"); + System.out.println("| Choose active servo channel [0..15] "); + System.out.println("| Example: 0 "); + System.out.println("|--------------------------------------------------------------------------------"); + + int channel = -1; + boolean isValidChannel = false; + while (isValidChannel == false) { + String input = null; + try { + input = scanner.nextLine(); + channel = Integer.parseInt(input); + if (channel >= 0 && channel <= 15) { + isValidChannel = true; + } else { + System.err.println("Unsupported servo channel [" + channel + "], provide number between 0 and 15."); + } + } catch (NumberFormatException e) { + System.err.println("Invalid input [" + input + "], provide number between 0 and 15."); + } + } + activeServo = channel; + System.out.println("Active servo channel: " + activeServo); + } + + public void approachNeutralPosition() { + System.out.println("Approach neutral position"); + servos[activeServo].setPosition(0); + } + + public void move(Scanner scanner) { + System.out.println(""); + System.out.println("|- Move Position ----------------------------------------------------------------"); + System.out.println("| Move servo position to the left or to the right. "); + System.out.println("| Example: l10 this would move the servo from its current position to the "); + System.out.println("| left by 10% "); + System.out.println("| Example: r this would move the servo from its current position to the "); + System.out.println("| right by 1% "); + System.out.println("| -> subsequent single will repeat the previous command "); + System.out.println("| -> max travel to either side is 100% "); + System.out.println("| Exit command: x "); + System.out.println("|--------------------------------------------------------------------------------"); + + String command = null; + while ("x".equals(command) == false) { + float currentPosition = servos[activeServo].getPosition(); + System.out.println("Current servo position: " + currentPosition); + String input = scanner.nextLine(); + if (input.trim().isEmpty() == false) { + command = input.trim().toLowerCase(); + } + if (command != null) { + int sign; + if (command.startsWith("l")) { + sign = -1; + } else if (command.startsWith("r")) { + sign = 1; + } else if (command.equals("x")) { + continue; + } else { + System.err.println("Unknown command [" + command + "]."); + command = null; + continue; + } + + int moveAmount = 1; + try { + moveAmount = Integer.parseInt(command.substring(1)); + if (moveAmount < 0 || moveAmount > 100) { + moveAmount = 1; + System.out.println("Move amount is out of range - defaulted to [1]."); + } + System.out.println("Move amount is [" + moveAmount + "]."); + } catch (Exception e) { + System.out.println("Move amount defaulted to [1]."); + } + float newPosition = currentPosition + moveAmount * sign; + if (newPosition < Servo.POS_MAX_LEFT) { + newPosition = Servo.POS_MAX_LEFT; + System.out.println("Max left position exceeded - set position to " + Servo.POS_MAX_LEFT + "%"); + } else if (newPosition > Servo.POS_MAX_RIGHT) { + newPosition = Servo.POS_MAX_RIGHT; + System.out.println("Max right position exceeded - set position to " + Servo.POS_MAX_RIGHT + "%"); + } + servos[activeServo].setPosition(newPosition); + command = (sign == 1 ? "r" : "l") + moveAmount; + } + } + } + + public void subtrim(Scanner scanner) { + System.out.println(""); + System.out.println("|- Subtrim, adjust servo neutral position ---------------------------------------"); + System.out.println("| Example: r this would move the servos neutral position by one step to "); + System.out.println("| the right "); + System.out.println("| Example: l this would move the servos neutral position by one step to "); + System.out.println("| the left "); + System.out.println("| -> subsequent single will repeat the previous command "); + System.out.println("| -> max adjustment to either side is 200 steps "); + System.out.println("| Exit command: x "); + System.out.println("|--------------------------------------------------------------------------------"); + System.out.println("| Current Servo position: " + servos[activeServo].getPosition() + "] "); + System.out.println("|--------------------------------------------------------------------------------"); + + String command = null; + while ("x".equals(command) == false) { + String propertySubtrim = servos[activeServo].getProperty(Servo.PROP_SUBTRIM, Servo.PROP_SUBTRIM_DEFAULT); + int currentSubtrim = Integer.parseInt(propertySubtrim); + System.out.println("Current subtrim: " + currentSubtrim); + String input = scanner.nextLine(); + if (input.trim().isEmpty() == false) { + command = input.trim().toLowerCase(); + } + if (command != null) { + int moveAmount; + if (command.startsWith("l")) { + moveAmount = -1; + } else if (command.startsWith("r")) { + moveAmount = 1; + } else if (command.equals("x")) { + continue; + } else { + System.err.println("Unknown command [" + command + "]."); + command = null; + continue; + } + + float newSubtrim = currentSubtrim + moveAmount; + if (newSubtrim < Servo.SUBTRIM_MAX_LEFT) { + newSubtrim = Servo.SUBTRIM_MAX_LEFT; + System.out.println("Max left subtrim exceeded - set value to " + Servo.SUBTRIM_MAX_LEFT); + } else if (newSubtrim > Servo.SUBTRIM_MAX_RIGHT) { + newSubtrim = Servo.SUBTRIM_MAX_RIGHT; + System.out.println("Max right subtrim exceeded - set value to " + Servo.SUBTRIM_MAX_RIGHT); + } + servos[activeServo].setProperty(Servo.PROP_SUBTRIM, Float.toString(newSubtrim)); + } + } + } + + public void reverse() { + boolean isReverse = Boolean.parseBoolean(servos[activeServo].getProperty(Servo.PROP_IS_REVERSE)); + Boolean newValue = isReverse ? Boolean.FALSE : Boolean.TRUE; + servos[activeServo].setProperty(Servo.PROP_IS_REVERSE, newValue.toString()); + System.out.println("is reverse: " + newValue); + } + + public void travel(Scanner scanner) { + System.out.println(""); + System.out.println("|- Travel -----------------------------------------------------------------------"); + System.out.println("| Adjust endpoints. "); + System.out.println("| Example: r125 adjust RIGHT endpoint to 125 "); + System.out.println("| -> min: 0, max: 150, default 100 "); + System.out.println("| Exit command: x "); + System.out.println("|--------------------------------------------------------------------------------"); + + String command = null; + while ("x".equals(command) == false) { + String propertyEndpointLeft = servos[activeServo].getProperty(Servo.PROP_END_POINT_LEFT, Servo.PROP_END_POINT_DEFAULT); + String propertyEndpointRight = servos[activeServo].getProperty(Servo.PROP_END_POINT_RIGHT, Servo.PROP_END_POINT_DEFAULT); + System.out.println("Current endpoints: LEFT [" + propertyEndpointLeft + "], RIGHT [" + propertyEndpointRight + "]"); + + String input = scanner.nextLine(); + if (input.trim().isEmpty() == false) { + command = input.trim().toLowerCase(); + } + if (command != null) { + String propertyToAdjust; + if (command.startsWith("l")) { + propertyToAdjust = Servo.PROP_END_POINT_LEFT; + } else if (command.startsWith("r")) { + propertyToAdjust = Servo.PROP_END_POINT_RIGHT; + } else if (command.equals("x")) { + continue; + } else { + System.err.println("Unknown command [" + command + "]."); + command = null; + continue; + } + + int newEndpointValue; + try { + newEndpointValue = Integer.parseInt(command.substring(1)); + if (newEndpointValue < Servo.END_POINT_MIN || newEndpointValue > Servo.END_POINT_MAX) { + System.out.println("Endpoint value is out of range - defaulted to [" + Servo.PROP_END_POINT_DEFAULT + "]."); + newEndpointValue = Integer.parseInt(Servo.PROP_END_POINT_DEFAULT); + } + System.out.println("New value for property [" + propertyToAdjust + "]: " + newEndpointValue + ""); + } catch (Exception e) { + System.out.println("Endpoint value for property [" + propertyToAdjust + "] defaulted to [" + Servo.PROP_END_POINT_DEFAULT + "]."); + newEndpointValue = Integer.parseInt(Servo.PROP_END_POINT_DEFAULT); + } + servos[activeServo].setProperty(propertyToAdjust, Integer.toString(newEndpointValue)); + } + } + } + + public void sweep(Scanner scanner) throws Exception { + System.out.println(""); + System.out.println("|- Sweep ------------------------------------------------------------------------"); + System.out.println("| Continuously moves the servo between POS_MAX_LEFT and POS_MAX_RIGHT. "); + System.out.println("| To change speed provide value between 1 and 10 (10 for max speed) "); + System.out.println("| Example: 7 "); + System.out.println("| Default speed: 5 "); + System.out.println("| Exit command: x "); + System.out.println("|--------------------------------------------------------------------------------"); + + // create and start sweeper thread + Sweeper sweeper = new Sweeper(); + sweeper.start(); + + // handle user commands + String command = null; + while ("x".equals(command) == false) { + String input = scanner.nextLine(); + if (input.trim().isEmpty() == false) { + command = input.trim().toLowerCase(); + } + if (command != null) { + if (command.equals("x")) { + continue; + } + try { + int speed = Integer.parseInt(command); + sweeper.setSpeed(speed); + } catch (NumberFormatException e) { + System.err.println("Invalid speed value [" + command + "]. Allowed values [1..10] "); + } + } + } + sweeper.interrupt(); + servos[activeServo].setPosition(Servo.POS_NEUTRAL); + } + + public void info() { + for (int i = 0; i < servos.length; i++) { + Servo servo = servos[i]; + System.out.println("Channel " + (i < 10 ? " " : "") + i + ": " + (servo != null ? servo.toString() : "N.A.")); + } + } + + //------------------------------------------------------------------------------------------------------------------ + // Helpers + //------------------------------------------------------------------------------------------------------------------ + private PCA9685GpioProvider createProvider() throws IOException { + BigDecimal frequency = PCA9685GpioProvider.ANALOG_SERVO_FREQUENCY; + BigDecimal frequencyCorrectionFactor = new BigDecimal("1.0578"); + + I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1); + return new PCA9685GpioProvider(bus, 0x40, frequency, frequencyCorrectionFactor); + } + + private GpioPinPwmOutput[] provisionPwmOutputs(final PCA9685GpioProvider gpioProvider) { + GpioController gpio = GpioFactory.getInstance(); + GpioPinPwmOutput myOutputs[] = { + gpio.provisionPwmOutputPin(gpioProvider, PCA9685Pin.PWM_00, "Servo 00"), + gpio.provisionPwmOutputPin(gpioProvider, PCA9685Pin.PWM_01, "not used"), + gpio.provisionPwmOutputPin(gpioProvider, PCA9685Pin.PWM_02, "not used"), + gpio.provisionPwmOutputPin(gpioProvider, PCA9685Pin.PWM_03, "not used"), + gpio.provisionPwmOutputPin(gpioProvider, PCA9685Pin.PWM_04, "not used"), + gpio.provisionPwmOutputPin(gpioProvider, PCA9685Pin.PWM_05, "not used"), + gpio.provisionPwmOutputPin(gpioProvider, PCA9685Pin.PWM_06, "not used"), + gpio.provisionPwmOutputPin(gpioProvider, PCA9685Pin.PWM_07, "not used"), + gpio.provisionPwmOutputPin(gpioProvider, PCA9685Pin.PWM_08, "not used"), + gpio.provisionPwmOutputPin(gpioProvider, PCA9685Pin.PWM_09, "not used"), + gpio.provisionPwmOutputPin(gpioProvider, PCA9685Pin.PWM_10, "not used"), + gpio.provisionPwmOutputPin(gpioProvider, PCA9685Pin.PWM_11, "not used"), + gpio.provisionPwmOutputPin(gpioProvider, PCA9685Pin.PWM_12, "not used"), + gpio.provisionPwmOutputPin(gpioProvider, PCA9685Pin.PWM_13, "not used"), + gpio.provisionPwmOutputPin(gpioProvider, PCA9685Pin.PWM_14, "not used"), + gpio.provisionPwmOutputPin(gpioProvider, PCA9685Pin.PWM_15, "not used")}; + return myOutputs; + } + + private class Sweeper extends Thread { + + private int speed = 5; + private final int step = 1; // make sure this is always true: 100 % step = 0 + private final int maxSleepBetweenSteps = 100; + + @Override + public void run() { + int position = 0; + Orientation orientation = Orientation.RIGHT; + while (!Thread.currentThread().isInterrupted()) { + try { + if (orientation == Orientation.RIGHT) { + if (position < Servo.POS_MAX_RIGHT) { + position += step; + } else { + orientation = Orientation.LEFT; + position -= step; + } + } else if (orientation == Orientation.LEFT) { + if (position > Servo.POS_MAX_LEFT) { + position -= step; + } else { + orientation = Orientation.RIGHT; + position += step; + } + } else { + System.err.println("Unsupported value for enum : [" + orientation + "]."); + } + + servos[activeServo].setPosition(position); + Thread.currentThread(); + if (position % 10 == 0) { + System.out.println("Position: " + position); + } + Thread.sleep(maxSleepBetweenSteps / speed); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } + } + + public void setSpeed(int speed) { + if (speed < 1) { + this.speed = 1; + } else if (speed > 10) { + this.speed = 10; + } else { + this.speed = speed; + } + } + } + } + + + + + + + + + + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } diff --git a/__pycache__/threading.cpython-39.pyc b/__pycache__/threading.cpython-39.pyc new file mode 100644 index 0000000..89b0d15 Binary files /dev/null and b/__pycache__/threading.cpython-39.pyc differ diff --git a/simpletest.py b/simpletest.py new file mode 100755 index 0000000..33ea71b --- /dev/null +++ b/simpletest.py @@ -0,0 +1,55 @@ +# Simple demo of of the PCA9685 PWM servo/LED controller library. +# This will move channel 0 from min to max position repeatedly. +# Author: Tony DiCola +# License: Public Domain +from __future__ import division +import time + +# Import the PCA9685 module. +import Adafruit_PCA9685 + + +# Uncomment to enable debug output. +#import logging +#logging.basicConfig(level=logging.DEBUG) + +# Initialise the PCA9685 using the default address (0x40). +pwm = Adafruit_PCA9685.PCA9685() + +# Alternatively specify a different address and/or bus: +#pwm = Adafruit_PCA9685.PCA9685(address=0x41, busnum=2) + +# Configure min and max servo pulse lengths +servo_min = 150 # Min pulse length out of 4096 +servo_max = 400 # Max pulse length out of 4096 + +# Helper function to make setting a servo pulse width simpler. +def set_servo_pulse(channel, pulse): + pulse_length = 1000000 # 1,000,000 us per second + pulse_length //= 60 # 60 Hz + print('{0}us per period'.format(pulse_length)) + pulse_length //= 4096 # 12 bits of resolution + print('{0}us per bit'.format(pulse_length)) + pulse *= 1000 + pulse //= pulse_length + pwm.set_pwm(channel, 0, pulse) + +# Set frequency to 60hz, good for servos. +pwm.set_pwm_freq(60) + +print('Moving servo on channel 0, press Ctrl-C to quit...') +while True: + # Move servo on channel O between extremes. + pwm.set_pwm(0, 0, servo_min) + pwm.set_pwm(1, 0, servo_min) + pwm.set_pwm(2, 0, servo_min) + pwm.set_pwm(3, 0, servo_min) + pwm.set_pwm(4, 0, servo_min) + time.sleep(1) + pwm.set_pwm(0, 0, servo_max) + pwm.set_pwm(1, 0, servo_max) + pwm.set_pwm(2, 0, servo_max) + pwm.set_pwm(3, 0, servo_max) + pwm.set_pwm(4, 0, servo_max) + time.sleep(1) + diff --git a/simpletest2.py b/simpletest2.py new file mode 100755 index 0000000..cb80d5e --- /dev/null +++ b/simpletest2.py @@ -0,0 +1,53 @@ +# Simple demo of of the PCA9685 PWM servo/LED controller library. +# This will move channel 0 from min to max position repeatedly. +# Author: Tony DiCola +# License: Public Domain +from __future__ import division +import time + +# Import the PCA9685 module. +import Adafruit_PCA9685 + + +# Uncomment to enable debug output. +#import logging +#logging.basicConfig(level=logging.DEBUG) + +# Initialise the PCA9685 using the default address (0x40). +pwm = Adafruit_PCA9685.PCA9685() + +# Alternatively specify a different address and/or bus: +#pwm = Adafruit_PCA9685.PCA9685(address=0x41, busnum=2) + +# Configure min and max servo pulse lengths +servo_min = 150 # Min pulse length out of 4096 +servo_max = 400 # Max pulse length out of 4096 + +# Helper function to make setting a servo pulse width simpler. +def set_servo_pulse(channel, pulse): + pulse_length = 1000000 # 1,000,000 us per second + pulse_length //= 60 # 60 Hz + print('{0}us per period'.format(pulse_length)) + pulse_length //= 4096 # 12 bits of resolution + print('{0}us per bit'.format(pulse_length)) + pulse *= 1000 + pulse //= pulse_length + pwm.set_pwm(channel, 0, pulse) + +# Set frequency to 60hz, good for servos. +pwm.set_pwm_freq(60) + +print('Moving servo on channel 4, press Ctrl-C to quit...') +while True: + + for x in range(6): + pwm.set_pwm(x, 0,150) + + print('200') + time.sleep(1) + for y in range(6): + pwm.set_pwm(y, 0, 400) + + print('350') + time.sleep(1) + diff --git a/simpletest3.py b/simpletest3.py new file mode 100755 index 0000000..4a3a074 --- /dev/null +++ b/simpletest3.py @@ -0,0 +1,46 @@ +# Simple demo of of the PCA9685 PWM servo/LED controller library. +# This will move channel 0 from min to max position repeatedly. +# Author: Tony DiCola +# License: Public Domain +from __future__ import division +import time + +# Import the PCA9685 module. +import Adafruit_PCA9685 + + +# Uncomment to enable debug output. +#import logging +#logging.basicConfig(level=logging.DEBUG) + +# Initialise the PCA9685 using the default address (0x40). +pwm = Adafruit_PCA9685.PCA9685() + +# Alternatively specify a different address and/or bus: +#pwm = Adafruit_PCA9685.PCA9685(address=0x41, busnum=2) + +# Configure min and max servo pulse lengths +servo_min = 150 # Min pulse length out of 4096 +servo_max = 600 # Max pulse length out of 4096 + +# Helper function to make setting a servo pulse width simpler. +def set_servo_pulse(channel, pulse): + pulse_length = 1000000 # 1,000,000 us per second + pulse_length //= 60 # 60 Hz + print('{0}us per period'.format(pulse_length)) + pulse_length //= 4096 # 12 bits of resolution + print('{0}us per bit'.format(pulse_length)) + pulse *= 1000 + pulse //= pulse_length + pwm.set_pwm(channel, 0, pulse) + +# Set frequency to 60hz, good for servos. +pwm.set_pwm_freq(60) + +print('Moving servo on channel 4, press Ctrl-C to quit...') +while True: + + channel = int(input("Channel:\n")) + pos = int(input("Position: \n")) + num = int(input("num \n")) + pwm.set_pwm(channel, 0,pos) diff --git a/testwalk.py b/testwalk.py new file mode 100755 index 0000000..4e42b0e --- /dev/null +++ b/testwalk.py @@ -0,0 +1,49 @@ +# Simple demo of of the PCA9685 PWM servo/LED controller library. +# This will move channel 0 from min to max position repeatedly. +# Author: Tony DiCola +# License: Public Domain +from __future__ import division +import time + +# Import the PCA9685 module. +import Adafruit_PCA9685 + + +# Uncomment to enable debug output. +#import logging +#logging.basicConfig(level=logging.DEBUG) + +# Initialise the PCA9685 using the default address (0x40). +pwm = Adafruit_PCA9685.PCA9685() + +# Alternatively specify a different address and/or bus: +#pwm = Adafruit_PCA9685.PCA9685(address=0x41, busnum=2) + +# Configure min and max servo pulse lengths +servo_min = 150 # Min pulse length out of 4096 +servo_max = 400 # Max pulse length out of 4096 + +# Helper function to make setting a servo pulse width simpler. +def set_servo_pulse(channel, pulse): + pulse_length = 1000000 # 1,000,000 us per second + pulse_length //= 60 # 60 Hz + print('{0}us per period'.format(pulse_length)) + pulse_length //= 4096 # 12 bits of resolution + print('{0}us per bit'.format(pulse_length)) + pulse *= 1000 + pulse //= pulse_length + pwm.set_pwm(channel, 0, pulse) + +# Set frequency to 60hz, good for servos. +pwm.set_pwm_freq(60) + +print('Moving servo on channel 0, press Ctrl-C to quit...') +while True: + pwm.set_pwm(6,0,150) + for x in range(150,400): + print (x) + pwm.set_pwm(6, 0, x) + math = round(0.026*pow((x-275),2)) + print (math) + pwm.set_pwm(0, 0, math) + diff --git a/testwalk2.py b/testwalk2.py new file mode 100644 index 0000000..8113a0f --- /dev/null +++ b/testwalk2.py @@ -0,0 +1,20 @@ + +# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +# SPDX-License-Identifier: MIT + +"""Simple test for a standard servo on channel 0 and a continuous rotation servo on channel 1.""" +import time +from adafruit_servokit import ServoKit + +# Set channels to the number of servo channels on your kit. +# 8 for FeatherWing, 16 for Shield/HAT/Bonnet. +kit = ServoKit(channels=16) +kit.servo[12].set_pulse_width_range(1000, 2000) +#kit.servo[12].angle = 180 +#time.sleep(1) +kit.continuous_servo[12].throttle = 1 +time.sleep(3) +kit.continuous_servo[12].throttle = -1 +time.sleep(3) +kit.servo[0].angle = 0 +kit.continuous_servo[12].throttle = 0 diff --git a/testwalk3.py b/testwalk3.py new file mode 100644 index 0000000..8d43f5c --- /dev/null +++ b/testwalk3.py @@ -0,0 +1,11 @@ +import time +from adafruit_servokit import ServoKit + +kit = ServoKit(channels=16) +kit.servo[12].set_pulse_width_range(1000, 2000) + +kit.servo[12].actuation_range = 200 +while True: + channel = int(input("Channel:\n")) + pos = int(input("Position: \n")) + kit.servo[channel].angle = pos diff --git a/testwalk4.py b/testwalk4.py new file mode 100644 index 0000000..1cafe00 --- /dev/null +++ b/testwalk4.py @@ -0,0 +1,14 @@ +import time +from adafruit_servokit import ServoKit + +kit = ServoKit(channels=16) + +leg = int(input("leg")) + +kit.servo[leg].angle = 0 +kit.servo[leg+6].angle = 0 +time.sleep(1) +kit.servo[leg].angle = 180 +kit.servo[leg+6].angle = 180 +time.sleep(1) +kit.servo[leg].angle = 0 diff --git a/testwalk4.py.save b/testwalk4.py.save new file mode 100644 index 0000000..85dc838 --- /dev/null +++ b/testwalk4.py.save @@ -0,0 +1,7 @@ +import time +from adafruit_servokit import ServoKit + +kit = ServoKit(channels=16) +# smallest 0 biggest 110 +while True: + kit.servo[].angle = diff --git a/thr.py b/thr.py new file mode 100644 index 0000000..5ab1f4d --- /dev/null +++ b/thr.py @@ -0,0 +1,77 @@ +import threading +import time +from adafruit_servokit import ServoKit + +kit = ServoKit(channels=16) + +#leg = int(input("leg")) +def left2(): + kit.servo[2].angle = 0 + kit.servo[8].angle = 0 + time.sleep(1) + kit.servo[2].angle = 110 + time.sleep(1) + kit.servo[8].angle = 110 + time.sleep(1) + kit.servo[2].angle = 0 +def left1(): + kit.servo[1].angle = 0 + kit.servo[7].angle = 0 + time.sleep(1) + kit.servo[1].angle = 110 + time.sleep(1) + kit.servo[7].angle = 110 + time.sleep(1) + kit.servo[1].angle = 0 +def left0(): + kit.servo[0].angle = 0 + kit.servo[6].angle = 0 + time.sleep(1) + kit.servo[0].angle = 110 + time.sleep(1) + kit.servo[6].angle = 110 + time.sleep(1) + kit.servo[0].angle = 0 +def right5(): + kit.servo[5].angle = 0 + kit.servo[11].angle = 110 + time.sleep(1) + kit.servo[5].angle = 110 + time.sleep(1) + kit.servo[11].angle = 0 + time.sleep(1) + kit.servo[5].angle = 0 +def right4(): + kit.servo[4].angle = 0 + kit.servo[10].angle = 110 + time.sleep(1) + kit.servo[4].angle = 110 + time.sleep(1) + kit.servo[10].angle = 0 + time.sleep(1) + kit.servo[4].angle = 0 +def right3(): + kit.servo[3].angle = 0 + kit.servo[9].angle = 110 + time.sleep(1) + kit.servo[3].angle = 110 + time.sleep(1) + kit.servo[9].angle = 0 + time.sleep(1) + kit.servo[leg].angle = 0 + + +leg0 = threading.Thread(target=left0, args=()) +leg1 = threading.Thread(target=left1, args=()) +leg2 = threading.Thread(target=left2, args=()) +leg3 = threading.Thread(target=right3, args=()) +leg4 = threading.Thread(target=right4, args=()) +leg5 = threading.Thread(target=right5, args=()) +leg0.start() +leg2.start() +leg4.start() +time.sleep(3) +leg1.start() +leg3.start() +leg5.start() +