def turnLeft(): wiringpi.softPwmWrite(enableA, 255) wiringpi.digitalWrite(h1, 1) wiringpi.digitalWrite(h2, 0) wiringpi.softPwmWrite(enableB, 255) wiringpi.digitalWrite(h3, 0) wiringpi.digitalWrite(h4, 1)
def turnRight(): wiringpi.softPwmWrite(enableA, 255) wiringpi.digitalWrite(h1, 0) wiringpi.digitalWrite(h2, 1) wiringpi.softPwmWrite(enableB, 255) wiringpi.digitalWrite(h3, 1) wiringpi.digitalWrite(h4, 0)
def backward(): wiringpi.softPwmWrite(enableA, 255) wiringpi.digitalWrite(h1, 1) wiringpi.digitalWrite(h2, 0) wiringpi.softPwmWrite(enableB, 255) wiringpi.digitalWrite(h3, 1) wiringpi.digitalWrite(h4, 0)
def forward(): wiringpi.softPwmWrite(enableA, 255) wiringpi.digitalWrite(h1, 0) wiringpi.digitalWrite(h2, 1) wiringpi.softPwmWrite(enableB, 235) wiringpi.digitalWrite(h3, 0) wiringpi.digitalWrite(h4, 1)
def updateLights(): global tlConfig STEP_INTERVAL = 1 / PWM_FREQ # how often STEP_BRIGHTNESS = 1 / (tlConfig.lightsFadeTime * PWM_FREQ) # how much currentBrightness = 1.00 while not shutdownFlag: changed = False if abs(currentBrightness - targetBrightness) > STEP_BRIGHTNESS / 2: if currentBrightness < targetBrightness: currentBrightness += STEP_BRIGHTNESS changed = True elif currentBrightness > targetBrightness: currentBrightness -= STEP_BRIGHTNESS changed = True if changed: if config.get('lights', 'pwm_mode') == 'HW': wiringpi.pwmWrite(TL_PWM, math.trunc(1024 * (1.00 - currentBrightness))) elif config.get('lights', 'pwm_mode') == 'SW': wiringpi.softPwmWrite(TL_PWM, math.trunc(100 * (1.00 - currentBrightness))) rootLogger.debug("{0:.2f} <- {1} <- {2:.2f}".format(currentBrightness, lastEvent, targetBrightness)) time.sleep(STEP_INTERVAL)
def rmotor(self, msg): if msg.data>=0.0: self.io.digitalWrite(10,1) self.io.digitalWrite(9,0) wp.softPwmWrite(25, int(msg.data*self.pwm_range)) else: self.io.digitalWrite(10,0) self.io.digitalWrite(9,1) wp.softPwmWrite(25, int(-msg.data*self.pwm_range))
def lmotor(self, msg): if msg.data>=0.0: self.io.digitalWrite(23,0) self.io.digitalWrite(24,1) wp.softPwmWrite(22, int(msg.data*self.pwm_range)) else: self.io.digitalWrite(23,1) self.io.digitalWrite(24,0) wp.softPwmWrite(22, int(-msg.data*self.pwm_range))
def setSpeed(self, pwm_pin, dir_pin, speed): """Set the motor PWM & dir based on pins and speed. From the mc33926 library. Thanks, Pololu. """ dir_value = 1 if speed < 0 else 0 speed = speed if speed > 0 else -speed speed = self.get_valid_speed(speed) wiringpi.digitalWrite(dir_pin, dir_value) # TODO: Change this to pwmWrite wiringpi.softPwmWrite(pwm_pin, speed)
def gpio_zero(self): # set everyone to 0 wp.softPwmWrite(self.pin_power_left, 0) wp.softPwmWrite(self.pin_power_right, 0) wp.digitalWrite(self.pin_direction_left_forward, 0) wp.digitalWrite(self.pin_direction_right_forward, 0) wp.digitalWrite(self.pin_direction_left_rear, 0) wp.digitalWrite(self.pin_direction_right_rear, 0)
def gpio_steer(self, drive_vector): wp.softPwmWrite(self.pin_power_left, int(self.current_speed*drive_vector[0])) wp.softPwmWrite(self.pin_power_right, int(self.current_speed*drive_vector[1])) wp.digitalWrite(self.pin_direction_left_forward, drive_vector[2]) wp.digitalWrite(self.pin_direction_right_forward, drive_vector[3]) wp.digitalWrite(self.pin_direction_left_rear, drive_vector[4]) wp.digitalWrite(self.pin_direction_right_rear, drive_vector[5]) actual_vec = (int(self.current_speed*drive_vector[0]), int(self.current_speed*drive_vector[1]),drive_vector[2], drive_vector[3], drive_vector[4], drive_vector[5]) msg='drive:steering, drive vector: %s, ppl %d ppr %d pdlf %d pdrf %d pdlr %d pdrr %d'%(str(actual_vec),self.pin_power_left, self.pin_power_right, self.pin_direction_left_forward, self.pin_direction_right_forward, self.pin_direction_left_rear, self.pin_direction_right_rear) self.queues['tx_msg'].put(msg) self.queues['log'].put(msg)
def setup_and_test(pin, small, large, first): print('{} {} {}'.format(small, large, first)) print('creating pin') cr = wiringpi.softPwmCreate(pin, small, large) print('Create results: {}'.format(cr)) print('first write') wres = wiringpi.softPwmWrite(servo_gpio_pin, first) print('Write res {}'.format(wres)) time.sleep(1) print('done sleeping') wiringpi.softPwmWrite(servo_gpio_pin, small)
def cleanMotorsPins(self): print 'Cleaning the motors\' pins up.' wiringpi.softPwmWrite(self.MOTOR_RIGHT_F, 0) wiringpi.softPwmWrite(self.MOTOR_RIGHT_B, 0) wiringpi.softPwmWrite(self.MOTOR_LEFT_F, 0) wiringpi.softPwmWrite(self.MOTOR_LEFT_B, 0) return True
def speed(self, speed): speed = int(speed) if (speed >= 0): wiringpi.softPwmWrite(self.pinForward, speed) wiringpi.softPwmWrite(self.pinBack, 0) else: wiringpi.softPwmWrite(self.pinForward, 0) wiringpi.softPwmWrite(self.pinBack, -speed)
def handle_connection(self, connection, client_address): while True: recieved = connection.recv(8) if recieved: # print 'recv({})={}'.format(len(recieved), \ # ' '.join(['{:X}'.format(ord(byte)) for byte in recieved])), \ # '|', recieved wp.digitalWrite(robot_api['dir_left_pin'], wp.HIGH if int(recieved[0]) else wp.LOW) wp.digitalWrite(robot_api['dir_right_pin'], wp.HIGH if int(recieved[1]) else wp.LOW) wp.softPwmWrite(robot_api['pwm_left_pin'], int(recieved[2:5])) wp.softPwmWrite(robot_api['pwm_right_pin'], int(recieved[5:8])) connection.send('OK') else: print 'recv 0 bytes', recieved connection.send('error: {}'.format('recv 0 bytes'))
def initialize_state(self): self._not_initialized = False self._MAX_SPEED = self.config.get('max_motor_speed', 10) self._MIN_SPEED = self._MAX_SPEED * -1 self.MIN_SERVO = self.config.get('min_servo', 100) self.MAX_SERVO = self.config.get('max_servo', 100) self.ZERO_SERVO = self.config.get('zero-servo', 150) self._INITIAL_SERVO = 100 gears = 5 for index in range(1, gears + 1): self.gear_lookup[index] = int((self._MAX_SPEED / gears) * index) logger.info('Setting gears: ' + str(self.gear_lookup)) if self.use_motors or self.use_servo: res = wiringpi.wiringPiSetupGpio() if res != 0: logger.error('Could not set up software PWM') if self.use_motors: logger.info('Setting up Motor Software-Based PWM') # a cluster of horrible looking code, should be refactored but is it single use??? wiringpi.pinMode(self.m1conf.get('en_pin'), wiringpi.GPIO.OUTPUT) wiringpi.pinMode(self.m1conf.get('dir_pin'), wiringpi.GPIO.OUTPUT) wiringpi.pinMode(self.m2conf.get('en_pin'), wiringpi.GPIO.OUTPUT) wiringpi.pinMode(self.m2conf.get('dir_pin'), wiringpi.GPIO.OUTPUT) logger.info('Pull both motors out of low-power mode') wiringpi.digitalWrite(self.m1conf.get('en_pin'), 1) wiringpi.digitalWrite(self.m2conf.get('en_pin'), 1) logger.info('Set up motor software pwm') wiringpi.softPwmCreate(self.m1conf.get('pwm_pin'), 0, self._MAX_SPEED) wiringpi.softPwmCreate(self.m2conf.get('pwm_pin'), 0, self._MAX_SPEED) wiringpi.softPwmWrite(self.m1conf.get('pwm_pin'), 0) wiringpi.softPwmWrite(self.m2conf.get('pwm_pin'), 0) logger.info('Setting motor speeds to zero.') if self.use_servo: logger.info('Setting up Servo Hardware-Based PWM') wiringpi.pinMode(self.servo_gpio_pin, wiringpi.GPIO.PWM_OUTPUT) wiringpi.pwmSetMode(wiringpi.GPIO.PWM_MODE_MS) wiringpi.pwmSetClock(192) wiringpi.pwmSetRange(2000) logger.info('Servo config done') wiringpi.pwmWrite(self.servo_gpio_pin, self.ZERO_SERVO) logger.info('Setting servo speed to zero.')
def set(self, red, green, blue): self.red = red self.green = green self.blue = blue wiringpi.softPwmWrite(self.r_pin, red) wiringpi.softPwmWrite(self.g_pin, green) wiringpi.softPwmWrite(self.b_pin, blue)
def set_wheel_speeds(speed, forward, backward): abs_speed = int(abs(speed)) if abs_speed < 10 and abs_speed != 0: abs_speed = 10 elif abs_speed > 90: abs_speed = 90 if speed >= 0: wiringpi.softPwmWrite(forward, abs_speed) wiringpi.softPwmWrite(backward, 0) else: # print "BackWard %d" % abs_speed wiringpi.softPwmWrite(forward, 0) wiringpi.softPwmWrite(backward, abs_speed)
def fade(self, red, green, blue, delay=500, step=5): for i in range(0, delay, step): f = (0.0+i)/delay r = self.red + (red -self.red) * f wiringpi.softPwmWrite(self.r_pin, int(r)) g = self.green + (green-self.green) * f wiringpi.softPwmWrite(self.g_pin, int(g)) b = self.blue + (blue -self.blue) * f wiringpi.softPwmWrite(self.b_pin, int(b)) wiringpi.delay(step) self.red = red self.blue = blue self.green = green
def main(): context = zmq.Context() socket = context.socket(zmq.SUB) socket.setsockopt(zmq.SUBSCRIBE, '') socket.connect('tcp://192.168.2.1:7777') wiringpi.wiringPiSetup() wiringpi.softPwmCreate(RED_PIN, 0, 255) wiringpi.softPwmCreate(GREEN_PIN, 0, 255) wiringpi.softPwmCreate(BLUE_PIN, 0, 255) while True: rgb = int(socket.recv()) red = (rgb >> 16) & 0xFF green = (rgb >> 8) & 0xFF blue = rgb & 0xFF wiringpi.softPwmWrite(RED_PIN, red) wiringpi.softPwmWrite(GREEN_PIN, green) wiringpi.softPwmWrite(BLUE_PIN, blue)
def pwm(value): #HW PWM #wiringpi.pwmWrite(GPIO_BCM, value) #SW PWM wiringpi.softPwmWrite(GPIO_BCM, value)
import time, wiringpi as pi led_pin = 14 pi.wiringPiSetupGpio() pi.pinMode( led_pin, pi.OUTPUT ) pi.softPwmCreate( led_pin, 0, 100) pi.softPwmWrite( led_pin, 0 ) while True: strong = 0 while ( strong <= 100 ): pi.softPwmWrite( led_pin, strong ) time.sleep(0.1) strong = strong + 1 pi.softPwmWrite( led_pin, 0 ) time.sleep(2)
io.pinMode(DRIVEA0, io.OUTPUT) io.pinMode(DRIVEA1, io.OUTPUT) io.pinMode(STANDBY, io.OUTPUT) io.pinMode(DRIVEB0, io.OUTPUT) io.pinMode(DRIVEB1, io.OUTPUT) # Set all the drives to 'off' io.digitalWrite(DRIVEA0, A0) io.digitalWrite(DRIVEA1, A1) io.digitalWrite(STANDBY, io.HIGH) io.digitalWrite(DRIVEB0, B0) io.digitalWrite(DRIVEB1, B1) # Enable PWM wiringpi.softPwmWrite(PWMA, 0) wiringpi.softPwmWrite(PWMB, 0) # Confgure the Servo configurations ServoStruct = namedtuple("ServoStruct", "channel axis min max park") # Create the list of Servos for the arm servos = [] servos.append(ServoStruct(HAND, PS3_AXIS_R1, 800, 1700, 1400)) servos.append(ServoStruct(WRIST, PS3_AXIS_RIGHT_H, 400, 2000, 1250)) servos.append(ServoStruct(FORE_ARM, PS3_AXIS_L1, 1000, 2000, 1500)) servos.append(ServoStruct(ELBOW, PS3_AXIS_RIGHT_V, 500, 2500, 1500)) servos.append(ServoStruct(SHOULDER, PS3_AXIS_LEFT_V, 500, 2500, 1500)) servos.append(ServoStruct(BASE, PS3_AXIS_LEFT_H, 500, 2500, 1500)) servoMids = []
import wiringpi as pi import time LED_PIN = 23 pi.wiringPiSetupGpio() pi.pinmode(LED_PIN, pi.OUTPUT) pi.softPwmCreate(LED_PIN, 0, 100) pi.softPwmWrite(LED_PIN, 50) time.sleep(100)
def testrange(top): for i in range(0, top, int(top/10)): print('sending {}'.format(i)) wiringpi.softPwmWrite(servo_gpio_pin, i) time.sleep(1)
# Pulsates an LED connected to GPIO pin 1 with a suitable resistor 4 times using softPwm # softPwm uses a fixed frequency import wiringpi OUTPUT = 1 PIN_TO_PWM = 15 # wPi wiringpi.wiringPiSetup() wiringpi.pinMode(PIN_TO_PWM, OUTPUT) wiringpi.softPwmCreate( PIN_TO_PWM, 0, 100) # Setup PWM using Pin, Initial Value and Range parameters # int softPwmCreate (int pin, int initialValue, int pwmRange) for time in range(0, 4): # 亮三次 for brightness in range( 0, 100): # Going from 0 to 100 will give us full off to full on wiringpi.softPwmWrite(PIN_TO_PWM, brightness) # Change PWM duty cycle wiringpi.delay(50) # Delay for 0.2 seconds for brightness in reversed(range(0, 100)): # 相反,從100到0 wiringpi.softPwmWrite(PIN_TO_PWM, brightness) wiringpi.delay(50)
def _stop(self): logger.debug('motor_stop') wiringpi.softPwmWrite(self._pin, 0)
def set_pulse(self, pulse): wiringpi.softPwmWrite(self._pin, pulse)
def _left_backward(self, pulse): GPIO.output(self.ENB, GPIO.HIGH) wiringpi.softPwmWrite(self.IN3, 0) wiringpi.softPwmWrite(self.IN4, pulse)
def _right_backward(self, pulse): GPIO.output(self.ENA, GPIO.HIGH) wiringpi.softPwmWrite(self.IN2, 0) wiringpi.softPwmWrite(self.IN1, pulse)
def softPWMWrite(pin, value): wiringpi.softPwmWrite(pin, value)
def Move(self, com, val=0): wpi.digitalWrite(M_SETUP, HIGH) if com == "R_FW": wpi.softPwmWrite(R_IN1, val) wpi.softPwmWrite(R_IN2, 0) elif com == "R_RW": wpi.softPwmWrite(R_IN1, 0) wpi.softPwmWrite(R_IN2, val) elif com == "L_FW": wpi.softPwmWrite(L_IN1, val) wpi.softPwmWrite(L_IN2, 0) elif com == "L_RW": wpi.softPwmWrite(L_IN1, 0) wpi.softPwmWrite(L_IN2, val) elif com == "R_STOP": wpi.softPwmWrite(R_IN1, 0) wpi.softPwmWrite(R_IN2, 0) elif com == "L_STOP": wpi.softPwmWrite(L_IN1, 0) wpi.softPwmWrite(L_IN2, 0) elif com == "BRK": wpi.softPwmWrite(R_IN1, 100) wpi.softPwmWrite(R_IN2, 100) wpi.softPwmWrite(L_IN1, 100) wpi.softPwmWrite(L_IN2, 100) wpi.delay(500) wpi.softPwmWrite(R_IN1, 0) wpi.softPwmWrite(R_IN2, 0) wpi.softPwmWrite(L_IN1, 0) wpi.softPwmWrite(L_IN2, 0) wpi.digitalWrite(M_SETUP, LOW)
# start the thread for each of the triggers for thread in trigger: thread.start() try: while True: time.sleep(patch.getfloat('general', 'delay')) for gpio, channel in config.items('control'): val = patch.getfloat('control', gpio) if val == None: continue # it should be skipped when not present if val == previous_val[gpio]: continue # it should be skipped when identical to the previous value previous_val[gpio] = val val = EEGsynth.rescale(val, slope=input_scale, offset=input_offset) val = EEGsynth.limit(val, 0, 100) val = int(val) lock.acquire() wiringpi.softPwmWrite(pin[gpio], val) lock.release() except KeyboardInterrupt: print("Closing threads") for thread in trigger: thread.stop() r.publish('OUTPUTGPIO_UNBLOCK', 1) for thread in trigger: thread.join() sys.exit()
import time import wiringpi as pi motor_pin = 23 pi.wiringPiSetupGpio() pi.pinMode(motor_pin, pi.OUTPUT) pi.softPwmCreate(motor_pin, 0, 100) pi.softPwmWrite(motor_pin, 0) while True: speed = 0 while (speed <= 100): pi.softPwmWrite(motor_pin, speed) time.sleep(0.3) speed = speed + 1 pi.softPwmWrite(motor_pin, 0) time.sleep(2)
def leye(self, msg): wp.softPwmWrite(11, int(msg.r*self.pwm_range)) wp.softPwmWrite(7, int(msg.g*self.pwm_range)) wp.softPwmWrite(8, int(msg.b*self.pwm_range))
import wiringpi as pi import hsv_to_rgb import time green_pin = 18 blue_pin = 23 red_pin = 24 pi.wiringPiSetupGpio() pi.pinMode(green_pin, pi.OUTPUT) pi.pinMode(blue_pin, pi.OUTPUT) pi.pinMode(red_pin, pi.OUTPUT) pi.softPwmCreate(green_pin, 0, 100) pi.softPwmCreate(blue_pin, 0, 100) pi.softPwmCreate(red_pin, 0, 100) while True: hue = 0 while (hue < 1): (red, green, blue) = hsv_to_rgb.hsv_to_rgb(hue, 1.0, 1.0) pi.softPwmWrite(green_pin, int(green * 100)) pi.softPwmWrite(blue_pin, int(blue * 100)) pi.softPwmWrite(red_pin, int(red * 100)) hue = hue + 0.01 time.sleep(0.1)
# pin: 11, 12, 15, 16 # BCM: 17, 18, 22, 23 front_l = 17 front_r = 18 back_l = 22 back_r = 23 pi.wiringPiSetupGpio() pi.pinMode(front_l, pi.OUTPUT) pi.pinMode(front_r, pi.OUTPUT) pi.pinMode(back_l, pi.OUTPUT) pi.pinMode(back_r, pi.OUTPUT) pi.softPwmCreate(front_l, 0, 100) pi.softPwmWrite(front_l, 0) pi.softPwmCreate(front_r, 0, 100) pi.softPwmWrite(front_r, 0) pi.softPwmCreate(back_l, 0, 100) pi.softPwmWrite(back_l, 0) pi.softPwmCreate(back_r, 0, 100) pi.softPwmWrite(back_r, 0) try: while True: speed = 50 pi.softPwmWrite(front_l, speed) pi.softPwmWrite(front_r, speed)
elif i>=7: s1+=4 else: s1=s1+1 l.append([s1,8+k,s]) cnt+=1 print l ''' # CORRECT cnt = 0 l = [] #input('key :') for k in range(1): wp.softPwmWrite(servo,14) wp.delay(200) wp.softPwmWrite(servo,0) wp.delay(200) wp.softPwmWrite(servo1,14) wp.delay(200) wp.softPwmWrite(servo1,0) wp.delay(200) wp.softPwmWrite(ser,8+k) wp.delay(200) wp.softPwmWrite(ser,0) wp.delay(2000) for j in range(1,10): print cnt cnt+=1
def stop(): wiringpi.softPwmWrite(enableA, 0) wiringpi.softPwmWrite(enableB, 0)
#!/usr/bin/python import wiringpi import time import sys wiringpi.wiringPiSetup() #wiringpi.pinMode(1, wiringpi.PWM_OUTPUT) #for x in xrange(0,1024): # print x # wiringpi.pwmWrite(1,x) # time.sleep(0.005) #for x in xrange(1024,0,-1): # print x # wiringpi.pwmWrite(1,x) # time.sleep(0.005) wiringpi.softPwmCreate(8, 0, 100) for x in xrange(0, 100): wiringpi.softPwmWrite(8, x) time.sleep(0.01) for x in xrange(100, 0, -1): wiringpi.softPwmWrite(8, x) time.sleep(0.01) wiringpi.softPwmWrite(8, int(sys.argv[1]))
def handler_wakeword(self, message): for brightness in range(0,100): wiringpi.softPwmWrite(blue_back, brightness) wiringpi.softPwmWrite(blue_front, brightness) wiringpi.softPwmWrite(green_back, brightness) wiringpi.softPwmWrite(green_front, brightness) wiringpi.softPwmWrite(red_back, brightness) wiringpi.softPwmWrite(red_front, brightness) for brightness in reversed(range(0,100)): wiringpi.softPwmWrite(blue_back, brightness) wiringpi.softPwmWrite(blue_front, brightness) wiringpi.softPwmWrite(green_back, brightness) wiringpi.softPwmWrite(green_front, brightness) wiringpi.softPwmWrite(red_back, brightness) wiringpi.softPwmWrite(red_front, brightness)"""
def setSpeed(motor_number, speed): if 0 < speed <= 1000: wiringpi.softPwmWrite(pins.motorPWM[motor_number], speed) else: wiringpi.softPwmWrite(pins.motorPWM[motor_number], 0)
valn=fb if(valn!=val): val=valn print "valn=",valn id = valn/100 print "id=",id pwmval = valn%100 print "pwmval=",pwmval if(id==1): wiringpi.pwmWrite(motor,pwmval*10) elif(id==2): wiringpi.softPwmWrite(servo,pwmval) valn2=lr if(valn2!=val2): val2=valn2 print "valn=",valn id = valn2/100 print "id=",id pwmval = valn2%100 print "pwmval=",pwmval if(id==1): wiringpi.pwmWrite(motor,pwmval*10)
def stop(): wiringpi.softPwmWrite(1, 0) wiringpi.softPwmWrite(4, 0) wiringpi.softPwmWrite(5, 0) wiringpi.softPwmWrite(6, 0)
exit() # wiringpi.wiringPiSetup() wiringpi.wiringPiSetupGpio() pinR = config.getint('output', 'red') pinG = config.getint('output', 'green') pinB = config.getint('output', 'blue') wiringpi.pinMode(pinR, wiringpi.OUTPUT) wiringpi.pinMode(pinG, wiringpi.OUTPUT) wiringpi.pinMode(pinB, wiringpi.OUTPUT) wiringpi.softPwmCreate(pinR, 0, 127) wiringpi.softPwmCreate(pinG, 0, 127) wiringpi.softPwmCreate(pinB, 0, 127) while True: time.sleep(config.getfloat('general', 'delay')) valR = EEGsynth.getint('input', 'red', config, r) * EEGsynth.getfloat('scaling', 'red', config, r) valG = EEGsynth.getint('input', 'green', config, r) * EEGsynth.getfloat('scaling', 'green', config, r) valB = EEGsynth.getint('input', 'blue', config, r) * EEGsynth.getfloat('scaling', 'blue', config, r) print valR, valG, valB wiringpi.softPwmWrite(pinR, int(valR)) wiringpi.softPwmWrite(pinG, int(valG)) wiringpi.softPwmWrite(pinB, int(valB))
def turnOn(pins, onTime): for brightness in reversed(range(0, 100)): for pin in pins: wiringpi.softPwmWrite(pin, brightness) wiringpi.delay(onTime)
for thread in trigger: thread.start() try: while True: time.sleep(patch.getfloat('general', 'delay')) for gpio, channel in config.items('control'): val = patch.getfloat('control', gpio) if val == None: continue # it should be skipped when not present if val == previous_val[gpio]: continue # it should be skipped when identical to the previous value previous_val[gpio] = val val = EEGsynth.rescale(val, slope=input_scale, offset=input_offset) val = EEGsynth.limit(val, 0, 100) val = int(val) lock.acquire() wiringpi.softPwmWrite(pin[gpio], val) lock.release() except KeyboardInterrupt: print("Closing threads") for thread in trigger: thread.stop() r.publish('OUTPUTGPIO_UNBLOCK', 1) for thread in trigger: thread.join() sys.exit()
if ( pi.digitalRead( button_pin ) == 0 ): time.sleep( 0.01 ) if ( select == 0 ): select = 1 print( "Control Green." ) elif ( select == 1 ): select = 2 print( "Control Blue." ) elif ( select == 2 ): select = 0 print( "Control Red." ) while ( pi.digitalRead( button_pin ) == 0 ): time.sleep( 0.01 ) val = mcp3002.read( pi, READ_CH ) tmp_val = val / 1023 if ( select == 0 ): red = tmp_val elif ( select == 1 ): green = tmp_val elif ( select == 2 ): blue = tmp_val pi.softPwmWrite( green_pin, int( green * 100 ) ) pi.softPwmWrite( blue_pin, int( blue * 100 ) ) pi.softPwmWrite( red_pin, int( red * 100 ) ) time.sleep( 0.01 )
def turnOff(pins, offTime): for brightness in range(0, 100): for pin in pins: wiringpi.softPwmWrite(pin, brightness) wiringpi.delay(offTime)
def set_zero(servos): for i in range(3, 0, -1): wp.softPwmWrite(servos[i],0) wp.delay(200)
if mr > 1.0: mr = 1.0 elif mr < -1.0: mr = -1.0 # print("ml: " + str(ml) + " mr: " + str(mr)) motorspeed(ml, mr) #servo control #duty = (((rs+1)/2)*17)+7 #convert -1 to 1 into 0-1 then multiply up to 0-17, add constant for 7-24 if (up): duty += 1 servo_flag = 1 elif (down): duty -= 1 servo_flag = 1 else: servo_flag = 0 if duty < 15: duty = 15 elif duty > 22: duty = 22 wiringpi.softPwmWrite(ServoPWM, duty) # print('Duty: ', str(duty)) if (servo_flag): sleep(0.2) else: sleep(0.01) #limit the frequency to 100Hz
# Initialize pin numbers redPin = 20 greenPin = 21 bluePin = 16 # Setup wiringpi wiringpi.wiringPiSetupGpio() wiringpi.pinMode(redPin, 1) wiringpi.softPwmCreate(redPin, 0, 100) wiringpi.pinMode(bluePin, 1) wiringpi.softPwmCreate(bluePin, 0, 100) wiringpi.pinMode(greenPin, 1) wiringpi.softPwmCreate(greenPin, 0, 100) # Turn off all pins wiringpi.softPwmWrite(redPin, 100) wiringpi.softPwmWrite(greenPin, 100) wiringpi.softPwmWrite(bluePin, 100) def getPins(state): ''' Determins which pins are necessary to show a certain color with an RGB LED that describes the state or mood. Parameters ---------- state : str The state or mood that you wish to describe. Returns
i2c = pi.I2C() #BME280とMPU6050のセットアップ acc = mpu6050.mpu6050(i2c, mpu6050_addr) press = bme280.bme280(i2c, bmp280_addr, s_press) acc.setup() press.setup() pi.pinMode(MOTOR_R_1PIN, pi.OUTPUT) #駆動部のセットアップ pi.pinMode(MOTOR_R_2PIN, pi.OUTPUT) pi.pinMode(MOTOR_L_1PIN, pi.OUTPUT) pi.pinMode(MOTOR_L_2PIN, pi.OUTPUT) pi.softPwmCreate(MOTOR_R_1PIN, 0, 100) pi.softPwmCreate(MOTOR_R_2PIN, 0, 100) pi.softPwmCreate(MOTOR_L_1PIN, 0, 100) pi.softPwmCreate(MOTOR_L_2PIN, 0, 100) pi.softPwmWrite(MOTOR_R_1PIN, 0) pi.softPwmWrite(MOTOR_R_2PIN, 0) pi.softPwmWrite(MOTOR_L_1PIN, 0) pi.softPwmWrite(MOTOR_L_2PIN, 0) while True: t = time.time() dt = t - t0 synthetic_acc = acc.synthetic_acc_cal() altitude = press.alititude_cal() gprmc_data = [] gprmc_data = location.gprmc() lat_data = float(data[3]) / 100.0 lng_data = float(data[5]) / 100.0
def run(self): while 1: left_pwm = getPWM(self.jr.velocity_left_weel) if left_pwm > 0: if left_pwm < self.jr.MIN_SPEED: left_pwm = 0 elif left_pwm > 70: left_pwm = 70 wiringpi.softPwmWrite(self.jr.MOTOR_LEFT_F, left_pwm) wiringpi.softPwmWrite(self.jr.MOTOR_LEFT_B, 0) elif left_pwm < 0: if left_pwm > - self.jr.MIN_SPEED: left_pwm = 0 elif left_pwm < - 70: left_pwm = - 70 wiringpi.softPwmWrite(self.jr.MOTOR_LEFT_F, 0) wiringpi.softPwmWrite(self.jr.MOTOR_LEFT_B, -left_pwm) else: wiringpi.softPwmWrite(self.jr.MOTOR_LEFT_F, 0) wiringpi.softPwmWrite(self.jr.MOTOR_LEFT_B, 0) right_pwm = getPWM(self.jr.velocity_right_weel) if right_pwm > 0: if right_pwm < self.jr.MIN_SPEED: right_pwm = 0 wiringpi.softPwmWrite(self.jr.MOTOR_RIGHT_F, right_pwm) wiringpi.softPwmWrite(self.jr.MOTOR_RIGHT_B, 0) elif right_pwm < 0: if right_pwm > - self.jr.MIN_SPEED: right_pwm = 0 wiringpi.softPwmWrite(self.jr.MOTOR_RIGHT_F, 0) wiringpi.softPwmWrite(self.jr.MOTOR_RIGHT_B, -right_pwm) else: wiringpi.softPwmWrite(self.jr.MOTOR_RIGHT_F, 0) wiringpi.softPwmWrite(self.jr.MOTOR_RIGHT_B, 0) wiringpi.delay(1) # 1 ms #print "self.jr.new_yaw_angle:,", self.jr.new_yaw_angle if self.jr.new_yaw_angle: if time.time() - self.jr.new_yaw_angle_arrived < self.jr.camera_motors_max_time: self.jr.yaw_motor_pwm.ChangeDutyCycle(self.jr.yawDegreeToDutyCycle(self.jr.yaw_angle)) else: self.jr.new_yaw_angle = False self.jr.yaw_motor_pwm.ChangeDutyCycle(0.0) if self.jr.new_pitch_angle: if time.time() - self.jr.new_pitch_angle_arrived < self.jr.camera_motors_max_time: self.jr.pitch_motor_pwm.ChangeDutyCycle(self.jr.pitchDegreeToDutyCycle(self.jr.pitch_angle)) else: self.jr.pitch_motor_pwm.ChangeDutyCycle(0.0) self.jr.new_pitch_angle = False
def reye(self, msg): wp.softPwmWrite(17, int(msg.r*self.pwm_range)) wp.softPwmWrite(21, int(msg.g*self.pwm_range)) wp.softPwmWrite(18, int(msg.b*self.pwm_range))
import wiringpi as wpi pin = 18 wpi.wiringPiSetupGpio() wpi.pinMode(pin, wpi.OUTPUT) wpi.softPwmCreate(pin, 0, 100) while True: for i in range(0, 101): wpi.softPwmWrite(pin, i) wpi.delay(10) for i in range(100, -1, -1): wpi.softPwmWrite(pin, i) wpi.delay(10)
def Position(servo,pos): (pin)=servo wp.softPwmWrite(pin,pos)
def go_to(servos,val_list): for i in range(3, 0, -1): wp.softPwmWrite(servos[i],val_list[i]) wp.delay(200)
def _set_color(self, color): self.color = color wiringpi.softPwmWrite(PIN_RED, color[0]) wiringpi.softPwmWrite(PIN_GREEN, color[1]) wiringpi.softPwmWrite(PIN_BLUE, color[2])
# time.sleep(0.2) # for i in range(10): # turn_right(turn_speed) # time.sleep(0.2) print("=========GUIDE=========") print("Press 'w' then it go forward") print("Press 's' then it go backward") print("Press 'a' then it turn left") print("Press 'd' then it turn right") print("Press 'p' to exit") while True: key = readkey() if key == 'w': go_forward(speed) elif key == 's': go_backward(speed) elif key == 'd': turn_right(turn_speed) elif key == 'a': turn_left(turn_speed) elif key == 'p': break print('\r') # stop print("stop") wiringpi.softPwmWrite(1, 0) wiringpi.softPwmWrite(4, 0) wiringpi.softPwmWrite(5, 0) wiringpi.softPwmWrite(6, 0)