def main(): """ Main program function """ # create an instance of the PWM class on i2c address 0x40 pwm = PWM(0x40) # Set PWM frequency to 1 Khz and enable the output pwm.set_pwm_freq(1000) pwm.output_enable() while True: for count in range(1, 4095, 5): pwm.set_pwm(1, 0, count) for count in range(4095, 1, -5): pwm.set_pwm(1, 0, count)
class servoDriver(object): servoFrequencyValue = 50 PWMFrequencyValue = 200 def __init__(self): #creating objects for the PWM and Motor self.pwm = pulseWidthModulation() self.ServoMotor = motor() def pulseWidthModulation(self): #Pulse Width Modulation(PWM) instatiation self.pwm = PWM(0x40) #Sets the PWM to a frequency of 200 Hz self.pwm.set_pwm_freq(self.PWMFrequencyValue) #Allow the output to be displayed self.pwm.output_enable() #Setting the channel for data stream to 1, time period to 1024 out of 4095. self.pwm.set_pwm(1, 0, 1024) def motor(self): #Instantiation of the servo object self.servo = Servo(0x40) #setting frequency for the PWM to interact with the PWM object self.servo.set_frequency(self.frequencyValuse) #setting the servo to move self.servo.move(1, 125, 250) def set_PWM_frequency(self, value): self.PWMFrequencyValue = value self.pwm.set_pwm_freq(self.PWMFrequencyValue) def get_PWM_frequency(self): return self.PWMFrequencyValue def set_servo_frequency(self, value): self.servoFrequencyValue = value self.servo.set_frequency(self.servoFrequencyValue) def get_servo_frequency(self): return self.servoFrequencyValue
#!/usr/bin/env python import roslib import rospy import geometry_msgs.msg import std_msgs.msg from sensor_msgs.msg import Joy from ServoPi import PWM import time import math pwm = PWM(0x40) pwm.set_pwm_freq(50) ts = 0.05 low = 205 mid = 307 high = 410 def angle_convert(angle): pulse_range = (2.0 - 1.0) pw_per_deg = (pulse_range / 181) pulse_width = int(((1.0) + (angle * (pw_per_deg))) * low) #print pulse_width return pulse_width def hip_move(tripod, user_angle_hiplist): angle_1 = angle_convert(user_angle_hiplist[0]) angle_2 = angle_convert(user_angle_hiplist[1])
from IOPi import IOPi from ADCDACPi import ADCDACPi from time import sleep from ServoPi import PWM pwm = PWM(0x41) pwm.set_pwm_freq(1000) ##output_enable() adcdac = ADCDACPi(1) adcdac.set_adc_refvoltage(3.3) bus = IOPi(0x20) bus.set_port_direction(0, 0x00) bus.set_port_direction(1, 0xF0) bus.write_port(0, 0) bus.write_port(1, 0) sleep(1) bus.write_pin(9, 1) sleep(1) bus.set_pin_pullup(16, 1) bus.set_pin_pullup(15, 1) bus.set_pin_pullup(14, 1) bus.invert_pin(16, 1) bus.invert_pin(15, 1) bus.invert_pin(14, 0) count = 0 dc = 0 while True: if bus.read_pin(14) == 1: