def _playLoop(self, freq, music): divisor = (19.2*(10**3))/freq Music = import_module(music) wiringpi2.pwmSetClock(int(divisor)) for note, beat in Music.melody: if note == ' ': wiringpi2.pwmWrite(self.SPEAKER, 0) else: period, dutyCycle = self._calcParams(note) wiringpi2.pwmSetRange(period) wiringpi2.pwmWrite(self.SPEAKER, dutyCycle) wiringpi2.delay(Music.tempo*beat) if note[len(note)-1:] == ':': wiringpi2.pwmWrite(self.SPEAKER, 0) wiringpi2.delay(Music.tempo) elif note[len(note)-1:] == ';': wiringpi2.pwmWrite(self.SPEAKER, 0) wiringpi2.delay(int(Music.tempo/2)) # Make sure that music stop wiringpi2.pwmWrite(self.SPEAKER, 0)
def __init__(self, name, pwm_port, backward_port, forward_port): self.name = name self.pwm_port = pwm_port self.backward_port = backward_port self.forward_port = forward_port self.backward_value = 0 self.forward_value = 0 # initialize Raspberry Pi ports for output wiringpi2.pinMode(self.pwm_port, wiringpi2.GPIO.PWM_OUTPUT) wiringpi2.pinMode(self.backward_port, wiringpi2.GPIO.OUTPUT) wiringpi2.pinMode(self.forward_port, wiringpi2.GPIO.OUTPUT) # experimentally found values (use test_pwm.py): # frequency = 19200 (not hearable) # divisor = 2 # range = 500 # dead zone <= 53% self.mapping_u = [0, 1, 100] self.mapping_pwm = [0, 20, 100] wiringpi2.pwmSetMode(wiringpi2.GPIO.PWM_MODE_MS); # 480 Hz self.divisor = 40 self.pwm_range = 1000 wiringpi2.pwmSetRange(self.pwm_range) wiringpi2.pwmSetClock(self.divisor) # pwmFrequency in Hz = 19.2 MHz / pwmClock / pwmRange self.logger = logging.getLogger(__name__)
def setup_servo(): wiringpi.wiringPiSetupGpio() wiringpi.pinMode(18,2) # hardware pwm only works on GPIO port 18 wiringpi.pwmSetMode(0) wiringpi.pwmSetClock(375) wiringpi.pwmSetRange(1024) wiringpi.pwmWrite(18,0)
def set_pwm(self): wiringpi.wiringPiSetupGpio() wiringpi.pinMode(self.gpio_pin, 2) wiringpi.pwmSetMode(0) wiringpi.pwmSetClock(self.clock) wiringpi.pwmSetRange(self.range) wiringpi.pwmWrite(self.gpio_pin, 0)
def setup_servo(): wiringpi.wiringPiSetupGpio() wiringpi.pinMode(18, 2) # hardware pwm only works on GPIO port 18 wiringpi.pwmSetMode(0) wiringpi.pwmSetClock(375) wiringpi.pwmSetRange(1024) wiringpi.pwmWrite(18, 0)
def _playLoop(self, freq, music): divisor = (19.2 * (10**3)) / freq Music = import_module(music) wiringpi2.pwmSetClock(int(divisor)) for note, beat in Music.melody: if note == ' ': wiringpi2.pwmWrite(self.SPEAKER, 0) else: period, dutyCycle = self._calcParams(note) wiringpi2.pwmSetRange(period) wiringpi2.pwmWrite(self.SPEAKER, dutyCycle) wiringpi2.delay(Music.tempo * beat) if note[len(note) - 1:] == ':': wiringpi2.pwmWrite(self.SPEAKER, 0) wiringpi2.delay(Music.tempo) elif note[len(note) - 1:] == ';': wiringpi2.pwmWrite(self.SPEAKER, 0) wiringpi2.delay(int(Music.tempo / 2)) # Make sure that music stop wiringpi2.pwmWrite(self.SPEAKER, 0)
def __init__(self, PIN_IN1_L=5, PIN_IN2_L=6, PIN_IN1_R=7, PIN_IN2_R=8, PIN_VREF_L=12, PIN_VREF_R=13): self.PIN_IN1_L = PIN_IN1_L self.PIN_IN1_R = PIN_IN1_R self.PIN_IN2_L = PIN_IN2_L self.PIN_IN2_R = PIN_IN2_R self.PIN_VREF_L = PIN_VREF_L self.PIN_VREF_R = PIN_VREF_R GPIO.setmode(GPIO.BCM) GPIO.setup(self.PIN_IN1_L, GPIO.OUT) GPIO.setup(self.PIN_IN2_L, GPIO.OUT) GPIO.setup(self.PIN_IN1_R, GPIO.OUT) GPIO.setup(self.PIN_IN2_R, GPIO.OUT) wp.wiringPiSetupGpio() wp.pinMode(self.PIN_VREF_L, wp.GPIO.PWM_OUTPUT) wp.pinMode(self.PIN_VREF_R, wp.GPIO.PWM_OUTPUT) wp.pwmSetMode(wp.PWM_MODE_MS) wp.pwmSetRange(Drive.MaxdutyRange) wp.pwmSetClock(400) #適当
def main(): print "Starting PWM" wiringpi.wiringPiSetupGpio() wiringpi.pinMode(18,2) wiringpi.pwmSetMode(0) wiringpi.pwmSetClock(400) wiringpi.pwmSetRange(1024) wiringpi.pwmWrite(18,0) wiringpi.pinMode(13,2) wiringpi.pwmSetMode(0) wiringpi.pwmSetClock(400) wiringpi.pwmSetRange(1024) wiringpi.pwmWrite(13,0) dtMin, dtMax = 60, 120 dt = 72 while True: try: print dt wiringpi.pwmWrite(18,dt) wiringpi.pwmWrite(13,dt) dt += 10 if dt > dtMax: dt = dtMin time.sleep(1) except: wiringpi.pwmWrite(18,0) wiringpi.pwmWrite(13,0) print "Exiting." break
def __init__(self, freq = 100.0): self.freq = freq output_pins.append(12) wp.pinMode(12, PWM) wp.pwmSetRange(4000) #range can go up to 4096, 4000 is good because it's large and a multiple of 100 clock = 19.2e6 / (4000.0 * freq) #set the divisor so the frequency comes out right wp.pwmSetClock(int(clock)) #this function needs an int wp.pwmSetMode(0) # necessary or else it's in a weird mode where nothing works wp.pwmWrite(12, 0) # stop for safety
def __init__(self, base0 = 27, base90 = 73): wiringpi.wiringPiSetupGpio() wiringpi.pinMode(18,2) wiringpi.pwmSetMode(0) wiringpi.pwmSetClock(400) wiringpi.pwmSetRange(1024) wiringpi.pwmWrite(18, 0) self.base0 = base0 self.base90 = base90 self.setAngle(90)
def set_hz(self, hz, direction): wiringpi2.digitalWrite(self.pin_dir, direction) if hz == 0: wiringpi2.pwmWrite(self.pin_step, 0) return # if hz is 2000 (60 RPM) we want # x = 1200000/2000 = 600 r = (self.pwm_base / self.pwm_divider) / hz print("Pump Hz %s range %s" % (hz, r)) wiringpi2.pwmSetRange(int(r)) wiringpi2.pwmWrite(self.pin_step, int(r / 2)) # 50% duty cycle
def set_motor(hz, direction): wiringpi2.digitalWrite(pin_dir, direction) if hz == 0: wiringpi2.pwmWrite(pin_step, 0) return # if hz is 2000 (60 RPM) we want # x = 1200000/2000 = 600 r = int((pwm_base/pwm_divider)/hz) wiringpi2.pwmSetRange(r) wiringpi2.pwmWrite(pin_step, r/2) # 50% duty cycle
def __init__(self): self.thrd_sleep_duration = 0.2 # sec self.light_pin = 18 self.started = False self.exit_flag = threading.Event() self.brightness = 0.0 # 0.0 to 1.0 self.ctrl_events = [] if not TEST_MODE: IO.wiringPiSetupGpio() IO.pinMode(self.light_pin, IO.GPIO.PWM_OUTPUT) IO.pwmSetClock(1920) IO.pwmSetRange(100)
def init(): GPIO.setmode(GPIO.BCM) wiringpi.wiringPiSetupGpio() wiringpi.pinMode(LEFT_MOTOR_PWM, 2) wiringpi.pinMode(RIGHT_MOTOR_PWM, 2) wiringpi.pwmSetMode(0) wiringpi.pwmSetClock(CLK_DIVISOR) wiringpi.pwmSetRange(MAX_SPEED) wiringpi.pwmWrite(LEFT_MOTOR_PWM, 0) wiringpi.pwmWrite(RIGHT_MOTOR_PWM, 0) GPIO.setup(LEFT_MOTOR_A, GPIO.OUT) GPIO.setup(LEFT_MOTOR_B, GPIO.OUT) GPIO.setup(RIGHT_MOTOR_A, GPIO.OUT) GPIO.setup(RIGHT_MOTOR_B, GPIO.OUT)
def __init__(self, pin): self.dtMin, self.dtMax, self.dtMed = 35, 120, 65 self.dt = self.dtMed self.pin = pin self.direction = '' wiringpi.wiringPiSetupGpio() wiringpi.pinMode(self.pin, 2) wiringpi.pwmSetMode(0) wiringpi.pwmSetClock(400) wiringpi.pwmSetRange(1024) try: wiringpi.pwmWrite(self.pin, 40) except Exception as e: print str(e)
def setupIO(): # Initialisation des entrées sorties sur le GrovePi # grovepi.pinMode(relay_pin,"OUTPUT") grovepi.pinMode(RGB_pin,"OUTPUT") grovepi.pinMode(PIR_pin, "INPUT") grovepi.chainableRgbLed_init(RGB_pin,8) # On génère un PWM hardware de fréquence 50Hz # # La largeur de l'impulsion est entre 0,8 et 2ms # wiringPi.wiringPiSetupGpio() wiringPi.pinMode(PWM_GPIO, 2) wiringPi.pwmSetMode(0) wiringPi.pwmSetClock(384) wiringPi.pwmSetRange(1024) wiringPi.pwmWrite(PWM_GPIO,75)
def listener(): rospy.init_node('listener', anonymous=True) rospy.Subscriber("chatter", String, callback) wiringpi.wiringPiSetupGpio() wiringpi.pinMode(pin,2) wiringpi.pwmSetMode(0) wiringpi.pwmSetClock(400) wiringpi.pwmSetRange(1024) wiringpi.pwmWrite(pin,0) wiringpi.pinMode(pin_dir[0],1) wiringpi.pinMode(pin_dir[1],1) # spin() simply keeps python from exiting until this node is stopped rospy.spin()
def io_init(): global io_initialized if io_initialized: return wiringpi2.wiringPiSetupGpio() wiringpi2.pinMode(12, wiringpi2.GPIO.PWM_OUTPUT) wiringpi2.pinMode(13, wiringpi2.GPIO.PWM_OUTPUT) wiringpi2.pwmSetMode(wiringpi2.GPIO.PWM_MODE_MS) wiringpi2.pwmSetRange(MAX_SPEED) wiringpi2.pwmSetClock(2) wiringpi2.pinMode(5, wiringpi2.GPIO.OUTPUT) wiringpi2.pinMode(6, wiringpi2.GPIO.OUTPUT) io_initialized = True
def io_init(): global io_initialized if io_initialized: return wiringpi2.wiringPiSetupGpio() wiringpi2.pinMode(MODE, wiringpi2.GPIO.OUTPUT) wiringpi2.digitalWrite(MODE, LOW) wiringpi2.pwmSetMode(wiringpi2.GPIO.PWM_MODE_MS) wiringpi2.pwmSetRange(PWM_RANGE_HIGH) wiringpi2.pwmSetClock(2) wiringpi2.pinMode(AIN1, wiringpi2.GPIO.PWM_OUTPUT) wiringpi2.pinMode(BIN1, wiringpi2.GPIO.PWM_OUTPUT) wiringpi2.pinMode(AIN2, wiringpi2.GPIO.OUTPUT) wiringpi2.pinMode(BIN2, wiringpi2.GPIO.OUTPUT) wiringpi2.softPwmCreate(AIN2, PWM_RANGE_LOW, PWM_RANGE_HIGH) wiringpi2.softPwmCreate(BIN2, PWM_RANGE_LOW, PWM_RANGE_HIGH) io_initialized = True
def main(self): wiringpi2.pwmSetClock(int(self.divisor)) for i, note in enumerate(Music.melody): if len(note) == 0: wiringpi2.pwmWrite(self.SPEAKER, 0) else: period, dutyCycle = self.calcParams(note) wiringpi2.pwmSetRange(period) wiringpi2.pwmWrite(self.SPEAKER, dutyCycle) wiringpi2.delay(Music.delay*(Music.beats[i]+1)) if note[len(note)-1:] == ':': wiringpi2.pwmWrite(self.SPEAKER, 0) wiringpi2.delay(Music.delay) curses.endwin() wiringpi2.pinMode(self.SPEAKER, 0)
def main(self): wiringpi2.pwmSetClock(int(self.divisor)) for i, note in enumerate(Music.melody): if len(note) == 0: wiringpi2.pwmWrite(self.SPEAKER, 0) else: period, dutyCycle = self.calcParams(note) wiringpi2.pwmSetRange(period) wiringpi2.pwmWrite(self.SPEAKER, dutyCycle) wiringpi2.delay(Music.delay * (Music.beats[i] + 1)) if note[len(note) - 1:] == ':': wiringpi2.pwmWrite(self.SPEAKER, 0) wiringpi2.delay(Music.delay) curses.endwin() wiringpi2.pinMode(self.SPEAKER, 0)
def Init(): global on wiringpi.wiringPiSetupGpio() wiringpi.pinMode(13, 2) wiringpi.pwmSetMode(0) wiringpi.pwmSetClock(400) wiringpi.pwmSetRange(1024) wiringpi.pinMode(18, 2) wiringpi.pwmSetMode(0) wiringpi.pwmSetClock(400) wiringpi.pwmSetRange(1024) SetSP(1, 0.0) SetSP(2, 0.0) on = True procComms.PrintLog('Initialized servo control.')
def startup_event(self, db, cfg): # config pwm wiringpi2.pinMode(self.PWM_PIN, wiringpi2.GPIO.PWM_OUTPUT) wiringpi2.pwmSetMode(wiringpi2.GPIO.PWM_MODE_MS) wiringpi2.pwmSetRange(self.PWM_RANGE) # 19.2 / 32 = 600KHz - Also starts the PWM wiringpi2.pwmSetClock(self.PWM_CLK_DIV) self.logd("Finding minimum fan speed...") if not self._find_minspeed(): return False self.logd("Minimum fan speed: {}%".format(self._minspeed)) self.set_highest_profile() # self.set_profile(40) if not self.add_requests( [["cpu_temp_profile", dict(profile="int")], ["cpu_fan_user", dict(speed="int")], ["cpu_fan_auto", dict()]]): return False return True
check = 0 # speedPrev = 0 newTrack = 0 goalComponentPrev = 0 listPosition = [LatLon(0, 0)] # GPIO.setmode(GPIO.BCM) GPIO.setup(LED_RED, GPIO.OUT) GPIO.setup(LED_GREEN, GPIO.OUT) GPIO.setup(LED_BLUE, GPIO.OUT) pi.wiringPiSetupGpio() pi.pinMode(SAIL_SERVO_PIN, 2) pi.pinMode(RUDDER_SERVO_PIN, 2) pi.pwmSetMode(0) pi.pwmSetRange(1024) pi.pwmSetClock(375) # sailValue = (SAIL_FULLCLOSE + SAIL_FULLOPEN) / 2 if (SAIL_FULLCLOSE < 35 or SAIL_FULLOPEN > 55 or SAIL_FULLCLOSE > SAIL_FULLOPEN): exit() sailCntl = SailController(sailValue) # time.sleep(30) # rudderValue = RUDDER_CENTER if (rudderValue < 40 or rudderValue > 76 or RUDDER_FULLRIGHT < 40 or RUDDER_FULLLEFT > 76 or RUDDER_FULLRIGHT > RUDDER_FULLLEFT):
import wiringpi2 as wiringpi import time wiringpi.pwmSetMode(0) # PWM_MODE_MS = 0 wiringpi.wiringPiSetupGpio() wiringpi.pinMode(18, 2) # pwm only works on GPIO port 18 wiringpi.pwmSetClock(6) # this parameters correspond to 25kHz wiringpi.pwmSetRange(128) wiringpi.pwmWrite(18, 0) # minimum RPM time.sleep(1) wiringpi.pwmWrite(18, 128) # maximum RPM time.sleep(1) wiringpi.pwmWrite(18, 0)
# 4) Divisor do clock # divisor = clock base / frequencia de afinação # Tendo que o clock base do pwm é 19.2mhz: # divisor = (19.2x1000)/440 # divisor = 43.6363636364 divisor = (19.2*(10**3))/440 wiringpi2.pwmSetClock(int(divisor)) def calcParams(freq): period = (1/freq)*(10**6) dutyCycle = period/2 return (int(period), int(dutyCycle)) try: while True: for freq in range(500, 1001, 1): period, dutyCycle = calcParams(freq) wiringpi2.pwmSetRange(period) wiringpi2.pwmWrite(SPEAKER, dutyCycle) wiringpi2.delay(1) for freq in range(1000, 499, -1): period, dutyCycle = calcParams(freq) wiringpi2.pwmSetRange(period) wiringpi2.pwmWrite(SPEAKER, dutyCycle) wiringpi2.delay(1) except KeyboardInterrupt: wiringpi2.pwmWrite(SPEAKER, 0) wiringpi2.pinMode(SPEAKER, 0) exit(0)
from ports import port_motor_left_forward, port_motor_right_forward, \ port_motor_left_pwm, port_motor_right_pwm pwm_divisor = int(sys.argv[1]) pwm_range = int(sys.argv[2]) percentage = int(sys.argv[3]) wiringpi2.pinMode(port_motor_left_pwm, wiringpi2.GPIO.PWM_OUTPUT) wiringpi2.pinMode(port_motor_right_pwm, wiringpi2.GPIO.PWM_OUTPUT) wiringpi2.pinMode(port_motor_left_forward, wiringpi2.GPIO.OUTPUT) wiringpi2.pinMode(port_motor_right_forward, wiringpi2.GPIO.OUTPUT) wiringpi2.pwmSetMode(wiringpi2.GPIO.PWM_MODE_MS) wiringpi2.pwmSetRange(pwm_range) wiringpi2.pwmSetClock(pwm_divisor) # pwmFrequency in Hz = 19.2 MHz / pwmClock / pwmRange frequency = 19200000 / pwm_divisor / pwm_range pwm_value = pwm_range * percentage / 100 print ("frequency={} Hz (divisor={}, range={}, value={})".format(frequency, pwm_divisor, pwm_range, pwm_value)) for i in range(1, 5): print "on" wiringpi2.digitalWrite(port_motor_left_forward, wiringpi2.GPIO.HIGH) wiringpi2.digitalWrite(port_motor_right_forward, wiringpi2.GPIO.HIGH) wiringpi2.pwmWrite(port_motor_left_pwm, pwm_value) wiringpi2.pwmWrite(port_motor_right_pwm, pwm_value)
#!/usr/bin/python import wiringpi2 as gpio import time #init the GPIO #prepare PWM pins gpio.wiringPiSetupGpio() gpio.pinMode(12, gpio.GPIO.PWM_OUTPUT) gpio.pinMode(13, gpio.GPIO.PWM_OUTPUT) #prepare PWM channels gpio.pwmSetMode(gpio.GPIO.PWM_MODE_MS) gpio.pwmSetRange(480) gpio.pwmSetClock(2) #prepare direction pins gpio.pinMode(5, gpio.GPIO.OUTPUT) gpio.pinMode(6, gpio.GPIO.OUTPUT) #movements def straight_fw(speed): gpio.digitalWrite(5, 1) gpio.digitalWrite(6, 1) gpio.pwmWrite(12, speed) gpio.pwmWrite(13, speed) def straight_bw(speed): gpio.digitalWrite(5, 0) gpio.digitalWrite(6, 0) gpio.pwmWrite(12, speed) gpio.pwmWrite(13, speed)
def setRange(self, pwmRange): self.pwmRange = pwmRange wiringpi.pwmSetRange(pwmRange)
# Tendo que o clock base do pwm é 19.2mhz: # divisor = (19.2x1000)/440 # divisor = 43.6363636364 divisor = (19.2 * (10**3)) / 440 wiringpi2.pwmSetClock(int(divisor)) def calcParams(freq): period = (1 / freq) * (10**6) dutyCycle = period / 2 return (int(period), int(dutyCycle)) try: while True: for freq in range(500, 1001, 1): period, dutyCycle = calcParams(freq) wiringpi2.pwmSetRange(period) wiringpi2.pwmWrite(SPEAKER, dutyCycle) wiringpi2.delay(1) for freq in range(1000, 499, -1): period, dutyCycle = calcParams(freq) wiringpi2.pwmSetRange(period) wiringpi2.pwmWrite(SPEAKER, dutyCycle) wiringpi2.delay(1) except KeyboardInterrupt: wiringpi2.pwmWrite(SPEAKER, 0) wiringpi2.pinMode(SPEAKER, 0) exit(0)
port_motor_left_backward, port_motor_right_backward, \ port_motor_left_pwm, port_motor_right_pwm, port_encoder_left_a, \ port_encoder_left_b, port_encoder_right_a, port_encoder_right_b # ./test_pwm_encoder.py 20 100 pwm_divisor = int(sys.argv[1]) pwm_range = int(sys.argv[2]) wiringpi2.pinMode(port_motor_left_pwm, wiringpi2.GPIO.PWM_OUTPUT) wiringpi2.pinMode(port_motor_right_pwm, wiringpi2.GPIO.PWM_OUTPUT) wiringpi2.pinMode(port_motor_left_forward, wiringpi2.GPIO.OUTPUT) wiringpi2.pinMode(port_motor_right_forward, wiringpi2.GPIO.OUTPUT) wiringpi2.pwmSetMode(wiringpi2.GPIO.PWM_MODE_MS) wiringpi2.pwmSetRange(pwm_range) wiringpi2.pwmSetClock(pwm_divisor) # pwmFrequency in Hz = 19.2 MHz / pwmClock / pwmRange frequency = 19200000 / pwm_divisor / pwm_range print ("frequency={} Hz (divisor={}, range={})".format(frequency, pwm_divisor, pwm_range)) #encoderLeft = EncoderReader(port_encoder_left_a, port_encoder_left_b) encoderRight = EncoderReader(port_encoder_right_a, port_encoder_right_b) def shutdown(): wiringpi2.pwmWrite(port_motor_left_pwm, 0) wiringpi2.pwmWrite(port_motor_right_pwm, 0) wiringpi2.digitalWrite(port_motor_left_backward, wiringpi2.GPIO.LOW)
# import wiringpi2 as wiringpi import time # OK input for servos seems to be between 30 and 110, it jitters if PWM exceeds those lo = 30 hi = 110 t = 5 wiringpi.wiringPiSetupGpio() wiringpi.pinMode(13, 2) wiringpi.pwmSetMode(0) wiringpi.pwmSetClock(400) wiringpi.pwmSetRange(1024) wiringpi.pinMode(18, 2) wiringpi.pwmSetMode(0) wiringpi.pwmSetClock(400) wiringpi.pwmSetRange(1024) wiringpi.pwmWrite(13, hi) time.sleep(t) wiringpi.pwmWrite(18, hi) time.sleep(t) wiringpi.pwmWrite(13, lo) time.sleep(t) wiringpi.pwmWrite(18, lo) time.sleep(t) wiringpi.pwmWrite(13, hi)
hz = 1 / (interval * 0.001) clock = int(18750 / hz) duty = int(duty_ratio * duty_range) print("pin = ", PWM18, " interval[ms] = ", interval, " upper_pulse[ms] = ", upper_pulse) print("clock = ", clock, " duty=", duty, " duty_ratio=", duty_ratio) # 初期設定 wiringpi.wiringPiSetupGpio() wiringpi.pinMode(PWM18, wiringpi.GPIO.PWM_OUTPUT) wiringpi.pwmSetMode(wiringpi.GPIO.PWM_MODE_MS) # ClockとDutyを設定してPWMを生成する wiringpi.pwmSetClock(clock) wiringpi.pwmWrite(PWM18, duty) wiringpi.pwmSetRange(duty_range) #max min値を計算 min_duty = int((under_pulse / interval) * duty_range * 1.1) max_duty = int((upper_pulse - under_pulse) / interval * duty_range + min_duty * 0.9) print("min_duty = ", min_duty, " max_duty=", max_duty) value = min_duty def DagToVal(dag): par_dag = (max_duty - min_duty) / 180.0 return int(dag * par_dag + min_duty)
#-*-coding:utf-8 -*- import wiringpi2 as wiringpi import FaBo9Axis_MPU9250 import time import serial import sys from math import degrees, radians, atan, atan2, sin, cos, pi, asin, sqrt #initialize setting wiringpi.wiringPiSetup() #initialize wiringPi wiringpi.pinMode(23, 2) # alternative function = PWM wiringpi.pinMode(26, 2) # alternative function = PWM wiringpi.pwmSetMode(0) wiringpi.pwmSetRange(12000) wiringpi.pwmWrite(23, 700) wiringpi.pwmWrite(26, 900) mpu9250 = FaBo9Axis_MPU9250.MPU9250() #mpu9250 initialize ser = serial.Serial('/dev/ttyAMA0', 9600) #gps serial port #initialize variable speed = 0 angle = 2 state = 0 def SpeedWrite(speed): #desire speed if speed > 10: print 'speed cannot be greater than 10.' elif speed > 0: wiringpi.pwmWrite(23, 880 + (speed - 1) * 10)
import wiringpi2 as GPIO import time r = 200; div = 100; outPin = 1; GPIO.wiringPiSetup() GPIO.pinMode(outPin,2) GPIO.pwmSetMode(0) GPIO.pwmSetClock(div) GPIO.pwmSetRange(r) for duty in range(r,0,-1): GPIO.pwmWrite(1,duty) if not duty%20: print str(int(float(duty)/float(r)*100)) + "%" time.sleep(0.01) for duty in range(0,r+1): GPIO.pwmWrite(1,duty) if not duty%20: print str(int(float(duty)/float(r)*100)) + "%" time.sleep(0.01)
from twisted.internet.protocol import Protocol, Factory from twisted.internet import reactor import RPi.GPIO as GPIO import wiringpi2 as wiringpi import time wiringpi.wiringPiSetupGpio() wiringpi.pinMode(18, wiringpi.GPIO.PWM_OUTPUT) wiringpi.pwmSetMode(wiringpi.GPIO.PWM_MODE_MS) wiringpi.pwmSetClock(400) wiringpi.pwmSetRange(1024) # 速度の調整 duty = 3 unduty = -3 set_time = 0.01 class RaspberryLight(Protocol): def connectionMade(self): self.factory.clients.append(self) def connectionLost(self, reason): self.factory.clients.remove(self) def dataReceived(self, data): if (data == "start"): print "start" # 回転角の調整 for deg in range(0, 171, duty): wiringpi.pwmWrite(18, deg / 2 + 910)
hz = 1 / (interval * 0.001) clock = int(18750 / hz) duty = int(duty_ratio * range) print("pin = ", PWM18, " interval[ms] = ", interval, " upper_pulse[ms] = ", upper_pulse) print("clock = ", clock, " duty=", duty, " duty_ratio=", duty_ratio) # 初期設定 wiringpi.wiringPiSetupGpio() wiringpi.pinMode(PWM18, wiringpi.GPIO.PWM_OUTPUT) wiringpi.pwmSetMode(wiringpi.GPIO.PWM_MODE_MS) # ClockとDutyを設定してPWMを生成する wiringpi.pwmSetClock(clock) wiringpi.pwmWrite(PWM18, duty) wiringpi.pwmSetRange(range) #max min値を計算 min_duty = int((under_pulse / interval) * range * 1.1) max_duty = int((upper_pulse - under_pulse) / interval * range + min_duty * .9) print("min_duty = ", min_duty, " max_duty=", max_duty) value = min_duty def DagToVal(dag): if dag < 0: dag = 0 if dag > 180: dag = 180 par_dag = (max_duty - min_duty) / 180.0 return int(dag * par_dag + min_duty)