def turn_on_pwm(pin): PWM.start(pin, 50) PWM.set_duty_cycle(pin, 25.5) PWM.set_frequency(pin, 10) print sys._getframe().f_code.co_name, print(' '+pin) raw_input('press a key to continue')
def speed(self, duty=55, offset=0): print 'duty ',duty if duty>83 or duty<-20: print 'bad pwm' return else: print ' ' #check if negative l_speed=duty+offset r_speed=duty-offset if l_speed<0:#negative speed, reverse GPIO.output(self.left_wheel_dir, GPIO.LOW) if l_speed<-20: l_speed=-20 elif l_speed>85:#check if speed too high l_speed=85 GPIO.output(self.left_wheel_dir, GPIO.HIGH) else: GPIO.output(self.left_wheel_dir, GPIO.HIGH) if r_speed<0:#negative speed, reverse GPIO.output(self.right_wheel_dir, GPIO.LOW) if r_speed<-20: r_speed=-20 elif r_speed>85:#check if speed too high r_speed=85 GPIO.output(self.right_wheel_dir, GPIO.HIGH) else: GPIO.output(self.right_wheel_dir, GPIO.HIGH) PWM.set_duty_cycle(self.left_wheel, abs(l_speed)) PWM.set_duty_cycle(self.right_wheel, abs(r_speed))
def OnAdjust(self,event): #val = self.sld.GetValue() #self.SetSize((val*2,val)) valuex1 = acc.readU8(0x32) valuex2 = acc.readU8(0x33) valuex = (valuex2 << 8) + valuex1 if valuex > (1<<15): valuex = int(bin(valuex),2)-(1<<16) #print (bin(value),2) print "val %i, val1: %i, val2: %i\n" % (valuex, valuex1, valuex2) PWM.set_duty_cycle("P9_14",((valuex+512.0)/1024.0)*100.0) valuey1 = acc.readU8(0x34) valuey2 = acc.readU8(0x35) valuey = (valuey2 << 8) + valuey1 if valuey > (1<<15): valuey = int(bin(valuey),2)-(1<<16) valuez1 = acc.readU8(0x36) valuez2 = acc.readU8(0x37) valuez = (valuez2 << 8) + valuez1 if valuez > (1<<15): valuez = int(bin(valuez),2)-(1<<16) self.sldx.SetValue(valuex+512) self.sldy.SetValue(valuey+512) self.sldz.SetValue(valuez+512)
def hardStop(cls, motorName): if motorName == cls.Channel.RIGHT: PWM.set_duty_cycle(PWMA) elif motorName == cls.Channel.LEFT: PWM.set_duty_cycle(PWMB) else: logger.error("Wrong motor name: LEFT or RIGHT possible only")
def setMotorSpeed(self,motor_num, percent): if percent >= 100: percent = 99 elif percent < 0: percent = 0 self.curr_motor_speed[motor_num] = percent PWM.set_duty_cycle(HardwareBase.MOTORS[motor_num], self.percent_to_duty(percent))
def rightFront(self, motorspeed): # motorspeed -100-100, if motorspeed < 0: Channel_A = 0 Channel_B = 1 elif motorspeed > 0: Channel_A = 1 Channel_B = 0 elif motorspeed == 0: Channel_A = 0 Channel_B = 0 # convert speed to a positive number motorspeed = abs(motorspeed) # makes sure motorspeed doesn't go out of range if motorspeed > 100: motorspeed = 100 # pwm for motor 3 PWM.set_duty_cycle("P8_13", motorspeed) # right side # writes to motor 3 GPIO.output("P8_14", Channel_A) GPIO.output("P8_16", Channel_B)
def analog_write(self): """ Set a PWM configured pin to the requested value :return: None """ # clear out any residual problem strings self.last_problem = '4-0\n' pin = self.validate_pin(self.pwm_pins) if pin == 99: self.last_problem = '4-1\n' return # get pin information index = self.pwm_pins.index(pin) pin_entry = self.pwm_pin_states[index] if not pin_entry['enabled']: self.last_problem = '4-2\n' return value = float(self.payload['value']) if not (0 <= value <= 100): self.last_problem = '4-3\n' PWM.set_duty_cycle(pin, value)
def center(self): """ center: Center the servomotor. """ PWM.set_duty_cycle(self.pin, self.duty_min+(self.duty_max-self.duty_min)/2) return
def set_servo_position(self): """ Set a servo position :return: """ duty_min = 3 duty_max = 14.5 duty_span = duty_max - duty_min self.last_problem = '7-0\n' pin = self.validate_pin(self.pwm_pins) if pin == 99: self.last_problem = '7-1\n' return index = self.pwm_pins.index(pin) pin_entry = self.pwm_pin_states[index] if not pin_entry['enabled']: self.last_problem = '7-1\n' return position = int(self.payload['position']) angle_f = float(position) duty = 100 - ((angle_f / 180) * duty_span + duty_min) PWM.set_duty_cycle(pin, duty)
def play_tone(self): """ This method will play a tone using PWM. """ # clear out any residual problem strings self.last_problem = '5-0\n' pin = self.validate_pin(self.pwm_pins) if pin == 99: self.last_problem = '5-1\n' return # get pin information index = self.pwm_pins.index(pin) pin_entry = self.pwm_pin_states[index] if not pin_entry['enabled']: self.last_problem = '5-2\n' return frequency = int(self.payload['frequency']) duration = float(self.payload['duration']) / 1000 PWM.set_duty_cycle(pin, 50) PWM.set_frequency(pin, frequency) if duration == 0: return time.sleep(duration) PWM.set_duty_cycle(pin, 0.0)
def engine(angle): #PWM.set_duty_cycle("P9_14", (angle/36)+5) if angle < 0: angle = 0 elif angle > 180: angle = 180 PWM.set_duty_cycle("P9_14", (angle+60)/18)
def run(self): print "Thread start" # have to do this line here because sometimes (don't know why) if fails during initialization # so let's do PWM initialization when we start LED thread import Adafruit_BBIO.PWM as PWM PWM.start(led0, 0, 30000) PWM.start(led1, 0, 30000) i=0 while True: if ledEvent.is_set(): sleep(0.2) try: PWM.set_duty_cycle(led0, 45 * math.sin(i) + 55) PWM.set_duty_cycle(led1, 45 * math.cos(i) + 55) except Exception as inst: print type(inst) # the exception instance print inst.args # arguments stored in .args print "Unexpected error:", sys.exc_info()[0] break i += math.pi/2 else: PWM.start(led0, 0, 30000) PWM.start(led1, 0, 30000) ledEvent.wait()
def leftRear(self, motorspeed): # motorspeed -100-100, if motorspeed < 0: Channel_A = 1 Channel_B = 0 elif motorspeed > 0: Channel_A = 0 Channel_B = 1 elif motorspeed == 0: Channel_A = 0 Channel_B = 0 # convert speed to a positive number motorspeed = abs(motorspeed) # makes sure motorspeed doesn't go out of range if motorspeed > 100: motorspeed = 100 # pwm for motor 2 PWM.set_duty_cycle("P9_16", motorspeed) # left Side # writes to motor 2 GPIO.output("P9_13", Channel_A) GPIO.output("P9_15", Channel_B)
def testH(): PWM.set_duty_cycle(EnA, 100) PWM.set_duty_cycle(EnB, 100) while(1): GPIO.output(I1A, GPIO.HIGH) GPIO.output(I2A, GPIO.LOW) GPIO.output(I1B, GPIO.HIGH) GPIO.output(I2B, GPIO.LOW) time.sleep(1) #Rotate GPIO.output(I1B, GPIO.LOW) GPIO.output(I2B, GPIO.LOW) time.sleep(1) #Invert GPIO.output(I1A, GPIO.LOW) GPIO.output(I2A, GPIO.HIGH) GPIO.output(I1B, GPIO.LOW) GPIO.output(I2B, GPIO.HIGH) time.sleep(1) #Rotate GPIO.output(I1A, GPIO.LOW) GPIO.output(I2A, GPIO.LOW) time.sleep(1)
def speed(self, duty=55, offset=0): if offset>15: x=15 elif offset<-15: x=-15 else: x=offset print 'duty ',duty if duty>75 or duty<-20: print 'bad pwm' return else: print ' ' #smaller offset for high speeds, assumed top offset of 15 i=x/15#i between -1 and 1 mult=-.19*abs(duty)+25 #mult=-.199*abs(duty)+25 x=mult*i print 'effective offset ', x #check if negative l_speed=duty+x r_speed=duty-x if l_speed<0:#negative speed, reverse GPIO.output(self.left_wheel_dir, GPIO.LOW) else: GPIO.output(self.left_wheel_dir, GPIO.HIGH) if r_speed<0:#negative speed, reverse GPIO.output(self.right_wheel_dir, GPIO.LOW) else: GPIO.output(self.right_wheel_dir, GPIO.HIGH) PWM.set_duty_cycle(self.left_wheel, abs(l_speed)) PWM.set_duty_cycle(self.right_wheel, abs(r_speed))
def do_blink(): print 'running LEDbar' r_speeds = redis.StrictRedis(host='localhost', port=6379, db=0) # get last 60 results (ie LRANGE times 0 59) # find max and min # scale most recent result to max and min # tell LEDbar to be that color recent = r_speeds.lrange('times',0,59) pingAv = [] ulAv = [] dlAv = [] for event in recent: # redis stores dict as a string, this is working ok to re-dict event = ast.literal_eval(event) for entry in event: if entry=='UL': ulAv.append(float(event[entry])) if entry=='DL': dlAv.append(float(event[entry])) if entry=='ping': pingAv.append(float(event[entry])) current = ast.literal_eval(recent[0]) print current ulOutput = mapVals(float(current['UL']), min(ulAv), max(ulAv),0.0,100.0) dlOutput = mapVals(float(current['DL']), min(dlAv),max(dlAv),0,100.0) pingOutput = 10.0*mapVals(float(current['ping']), min(pingAv),max(pingAv),0,100.0) # times 10 just for pingtime to be noticeable print ulOutput, dlOutput, pingOutput pwm.set_duty_cycle(redPin, dlOutput+0.0) pwm.set_duty_cycle(greenPin, 100.0-dlOutput)
def lockThruster(self): GPIO.setup(self.dir1_pin, GPIO.OUT) GPIO.setup(self.dir2_pin, GPIO.OUT) PWM.start("P8_13", 0.1) GPIO.output(self.dir1_pin, GPIO.HIGH) GPIO.output(self.dir2_pin, GPIO.HIGH) PWM.set_duty_cycle("P8_13", float(100)) self.pwm = 0
def updateduty(self, t0): modpunch = int(t0*200) % 8 bpunch = (modpunch <= 0) if abs(self.qvolts) > 3.1 else (modpunch <= 2) fac = 3 if bpunch else 1 bnds = 25 if bpunch else 15 dc = max(-bnds, min(bnds, self.qvolts*fac)) self.dc = dc+50 PWM.set_duty_cycle(self.pwm, dc+50)
def go(self): """ @brief update the motor PWM according to the class duty attributes """ # DC Brushed motors # PWM.set_duty_cycle(self.motor_pin[0],self.duty_IN1) # PWM.set_duty_cycle(self.motor_pin[1],self.duty_IN2) # DC Brushless motors PWM.set_duty_cycle(self.motor_pin, self.duty)
def stop(self): self.stop_event.set() self.stop_confirm.wait() self.stop_event.clear() self.stop_confirm.clear() gpio.output(self.dir_a, gpio.LOW) gpio.output(self.dir_b, gpio.LOW) pwm.set_duty_cycle(self.pwm, 100.0)
def reset(): if not ENABLED: if DEGREES_TURNED == 0: return else: enable() PWM.set_duty_cycle(CAMERA_SERVO_TX, SERVO_REVERSE_ON) sleep(DEGREES_TURNED * DEGREE_TURN_TIME) PWM.set_duty_cycle(CAMERA_SERVO_TX, SERVO_OFF) DEGREES_TURNED = 0
def set_camera_pan(angle=90.0): global pan_duty_min global pan_duty_span global pan_servo_pin angle_f = float(angle) duty = ((angle_f / 180.0) * pan_duty_span + pan_duty_min) pwm.set_duty_cycle(pan_servo_pin, duty)
def setDuty(value): ##duty values are valid 0-100 if value > 100: value = 100 if value < 0: value = 0 PWM.set_duty_cycle(pin, value)
def set_angle(self, angle, clamp = True): if clamp: self.angle = float(self.clamp(angle)) else: self.angle = float(angle) if self.running: duty = self.angle / 180. * self.duty_span + self.duty_min PWM.set_duty_cycle(self.servo_pin, duty) # print str(self.angle) return
def set_camera_tilt(angle=90.0): global tilt_duty_min global tilt_duty_span global tilt_servo_pin angle_f = float(angle) duty = ((angle_f / 180.0) * tilt_duty_span + tilt_duty_min) pwm.set_duty_cycle(tilt_servo_pin, duty)
def turnRight(): turn_speed = 15 GPIO.output(Right_Motor_Direction, GPIO.LOW) #set right motor to go backwards. GPIO.output(Left_Motor_Direction, GPIO.HIGH) #set left to go forwards PWM.set_duty_cycle(Left_Motor_Pin, turn_speed) PWM.set_duty_cycle(Right_Motor_Pin, turn_speed) time.sleep(1) #wait one second for turn GPIO.output(Left_Motor_Direction, GPIO.HIGH) #set left motor back to forwards direction
def set_brightness(led_value): if LED == 1: if (0 <= led_value <= 100): PWM.set_duty_cycle(LED,led_value) else: if led_value == 0: GPIO.output(LED, GPIO.LOW) else: GPIO.output(LED, GPIO.HIGH)
def steer(self, v): v = float(v) / 100 if v < 0: v = 0 elif v > 1: v = 1 duty = float(v) * self.duty_span + self.duty_min PWM.set_duty_cycle(self.servo_pin, duty) print "steer: " + str(v)
def set_speed(self, newSpeed): """ Method to change the speed of the motor, direciton is unchanged Arugments newSpeed : the desired new speed 0-100 (as percentage of max) """ PWM.set_duty_cycle(self.PWMpin, newSpeed) self.currentSpeed = newSpeed
def hard_stop(self): """ Method to hard stop an individual motor""" PWM.set_duty_cycle(self.PWMpin, 0.0) # set the status attributes self.isRunning = False self.currentDirection = None self.currentSpeed = 0
def stopAll(self): PWM.set_duty_cycle(self.pwm1, 0) PWM.set_duty_cycle(self.pwm2, 0)
def set_value(self, value): """ Set the amount of on-time from 0..1 """ PWM.set_duty_cycle(self.pin, value * 100)
def stop(): PWM.set_duty_cycle(channel_1_pwm, 0) PWM.set_duty_cycle(channel_2_pwm, 0) PWM.set_duty_cycle(channel_3_pwm, 0) PWM.set_duty_cycle(channel_4_pwm, 0)
def rotate_rover(duty, direction): if direction is "L": GPIO.output(channel_1_dir, GPIO.HIGH) GPIO.output(channel_2_dir, GPIO.LOW) GPIO.output(channel_3_dir, GPIO.LOW) GPIO.output(channel_4_dir, GPIO.HIGH) PWM.set_duty_cycle(channel_1_pwm, duty) PWM.set_duty_cycle(channel_2_pwm, duty) PWM.set_duty_cycle(channel_3_pwm, duty) PWM.set_duty_cycle(channel_4_pwm, duty) elif direction is "R": GPIO.output(channel_1_dir, GPIO.LOW) GPIO.output(channel_2_dir, GPIO.HIGH) GPIO.output(channel_3_dir, GPIO.HIGH) GPIO.output(channel_4_dir, GPIO.LOW) PWM.set_duty_cycle(channel_1_pwm, duty) PWM.set_duty_cycle(channel_2_pwm, duty) PWM.set_duty_cycle(channel_3_pwm, duty) PWM.set_duty_cycle(channel_4_pwm, duty) else: stop()
elif cmd == 'f': freqM1 = input("Enter a desired frequency: ") elif cmd == 'i': directionM1 = not directionM1 elif motor == '2': if cmd == 'x': break elif cmd == 'd': dutyM2 = input("Enter a duty cycle between 0 and 100: ") elif cmd == 'f': freqM2 = input("Enter a desired frequency: ") elif cmd == 'i': directionM2 = not directionM2 os.system('clear') if dutyM1 >= 0 and dutyM2 >= 0 and dutyM1 <= 100 and dutyM2 <= 100 and freqM1 >= 1 and freqM2 >= 1: #and (motor == 1 or motor == 2): PWM.set_duty_cycle(M1_PWM_PIN, dutyM1) PWM.set_frequency(M1_PWM_PIN, freqM1) PWM.set_duty_cycle(M2_PWM_PIN, dutyM2) PWM.set_frequency(M2_PWM_PIN, freqM2) if directionM1: GPIO.output(M1_DIRECTION_PIN, GPIO.HIGH) else: GPIO.output(M1_DIRECTION_PIN, GPIO.LOW) if directionM2: GPIO.output(M2_DIRECTION_PIN, GPIO.HIGH) else: GPIO.output(M2_DIRECTION_PIN, GPIO.LOW) else: print("Non-legal value given.") print GPIO.cleanup()
def ChangeDutyCycle(pin, duty_cycle): PWM.set_duty_cycle(pin, duty_cycle)
def servo_down(): GPIO.output("P8_9", GPIO.LOW) GPIO.output("P8_11", GPIO.HIGH) PWM.set_duty_cycle(servo_pin, duty) PWM.stop(servo_pin) PWM.cleanup()
#IMU's try: mp1 = mpu9250.SL_MPU9250(0x68, 2) mp2 = mpu9250.SL_MPU9250(0x69, 2) except: print( "IMU's : Failed to import or execute mpu9250 library, IMU is probably not connected rightly" ) while True: #Servo ctr = ctr + 10 angle = ctr angle_f = float(angle) duty = 100 - ((angle_f / 180) * dutySpan + dutyMin) pwm.set_duty_cycle(servopin, duty) #print("ctr: %s angle: %s duty: %s"%(ctr,angle,duty)) if ctr >= 179: ctr = 1 #time.sleep(0.1) #Insole valueHeelL = adc.read(heelL) print("heelL: " + str(valueHeelL)) #time.sleep(0.1) #Encoder res = spi.xfer2([0xFFFF, 0xFFFF]) #deliver two bytes res1 = spi.readbytes(2) #Read 2 bytes angle = (res1[0] << 8) | res1[1] #merge leftbyte and rightbyte angle1 = angle & 0x3FFF #move the first two bits
ctrlPitch = control.Control() """ Overlay DTO using pyBBIO """ PWM.start("P9_14", 10.9, 50, 0) # PWM.start(pin, duty, freq, polarity) PWM.start("P9_16", 9.4, 50, 0) # PWM.start(pin, duty, freq, polarity) UART.setup("UART1") # IMU ADC.setup() # ADC """ Set port and baud rate """ ser = serial.Serial(port = "/dev/ttyO1", baudrate = 115200) ser.close() ser.open() if ser.isOpen(): """ Position initialization for the static test """ time.sleep(0.5) mot1_pos = 10.9 # PWM_mot1 with interceptor Stroke 0% mot2_pos = 9.4 # PWM_mot2 with interceptor Stroke 0% PWM.set_duty_cycle("P9_14", mot1_pos) PWM.set_duty_cycle("P9_16", mot2_pos) time.sleep(0.5) """ Set gain of a PID controllers """ KD = 50 ctrlRoll.ctrlGain(0,0,KD) # pidGain(KP, KI, KD) ctrlPitch.ctrlGain(0,0,KD) # pidGain(KP, KI, KD) """ Error matrices initialization """ rollEr = np.matrix('0.0 0.0;1 1') #[t0 e0; t1 e1] pitchEr = np.matrix('0.0 0.0;1 1') #[t0 e0; t1 e1] """ initial time stamp """ timeSTMP0.timeSTMP() """ Open .dat file to record values """ f = open('writeTest.dat','w') f.write(str('t') + '\t' + str('rollEr') + '\t' + str('pitchEr') + '\t' + str('ADC_Ch1') + '\t' + str('ADC_Ch2') + '\t' + str('pitchPOT') + '\t' + str('heavePOT') + '\t' + str('q') + '\t' + str('mot1_pos') + '\t' + str('mot2_pos') + '\n') #Indices
def setPWM(self, pwm): # [leftSpeed, rightSpeed]: 0 is off, caps at min and max values self.pwm[LEFT] = min( max(pwm[LEFT], self.pwmLimits[MIN]), self.pwmLimits[MAX]) self.pwm[RIGHT] = min( max(pwm[RIGHT], self.pwmLimits[MIN]), self.pwmLimits[MAX]) # Left motor if self.pwm[LEFT] > 0: GPIO.output(self.dir1Pin[LEFT], GPIO.LOW) GPIO.output(self.dir2Pin[LEFT], GPIO.HIGH) PWM.set_duty_cycle(self.pwmPin[LEFT], abs(self.pwm[LEFT])) elif self.pwm[LEFT] < 0: GPIO.output(self.dir1Pin[LEFT], GPIO.HIGH) GPIO.output(self.dir2Pin[LEFT], GPIO.LOW) PWM.set_duty_cycle(self.pwmPin[LEFT], abs(self.pwm[LEFT])) else: GPIO.output(self.dir1Pin[LEFT], GPIO.LOW) GPIO.output(self.dir2Pin[LEFT], GPIO.LOW) PWM.set_duty_cycle(self.pwmPin[LEFT], 0) # Right motor if self.pwm[RIGHT] > 0: GPIO.output(self.dir1Pin[RIGHT], GPIO.LOW) GPIO.output(self.dir2Pin[RIGHT], GPIO.HIGH) PWM.set_duty_cycle(self.pwmPin[RIGHT], abs(self.pwm[RIGHT])) elif self.pwm[RIGHT] < 0: GPIO.output(self.dir1Pin[RIGHT], GPIO.HIGH) GPIO.output(self.dir2Pin[RIGHT], GPIO.LOW) PWM.set_duty_cycle(self.pwmPin[RIGHT], abs(self.pwm[RIGHT])) else: GPIO.output(self.dir1Pin[RIGHT], GPIO.LOW) GPIO.output(self.dir2Pin[RIGHT], GPIO.LOW) PWM.set_duty_cycle(self.pwmPin[RIGHT], 0)
import time import Adafruit_BBIO.GPIO as GPIO import Adafruit_BBIO.PWM as PWM import Adafruit_I2C as I2C import time PWM.start("P9_22", 50) sol = 783 if __name__ == "__main__": while True: PWM.set_frequency("P9_22", sol) lst = range(100, -1, -1) for i in lst: print(i) PWM.set_duty_cycle("P9_22", i) time.sleep(0.005)
#PWM.stop(pinA) PWM.stop(pinB) PWM.cleanup() PWM.start(pinA, 0) PWM.start(pinB, 0) time.sleep(1) print('switch') #PWM.set_frequency(pinA, 50) # fine if one servo is commented # as soon as both are used: # all following commands for the servo # don't result in a change #PWM.set_frequency(pinB, 50) PWM.set_duty_cycle(pinA, 97) PWM.set_duty_cycle(pinB, 97) time.sleep(2) print('switch') #PWM.set_frequency(pinA, 50) #PWM.set_frequency(pinB, 50) PWM.set_duty_cycle(pinA, 30) PWM.set_duty_cycle(pinB, 30) time.sleep(2) print('stop') #PWM.stop(pinA) PWM.stop(pinB) PWM.cleanup() print('done')
def listener(): # anonymous=False flag means that rospy will choose exaclty the # given name for the 'listener' node. If two nodes with the same # name are launched, the previous one is kicked off. rospy.init_node('motor_listener_int', anonymous=False) rospy.Subscriber('range_val', UInt16, callback) # spin() simply keeps python from exiting until this node is stopped rospy.spin() if __name__ == '__main__': try: PWM.start(pin_motL, 0, FREQ, 0) PWM.start(pin_motR, 0, FREQ, 0) GPIO.setup(pin_in1, GPIO.OUT) GPIO.setup(pin_in2, GPIO.OUT) print('PWM outpunt enabled...') GPIO.output(pin_in1, GPIO.HIGH) GPIO.output(pin_in2, GPIO.LOW) listener() print('') finally: PWM.set_duty_cycle(pin_motL, 0) PWM.set_duty_cycle(pin_motR, 0) PWM.cleanup() print('...Pin disabled.') print('Byebye...')
GPIO.setup(M1_DIRECTION_PIN, GPIO.OUT) os.system('clear') while 1: print("Duty Cycle: " + str(duty)) print("Frequency: " + str(freq)) print("Forward Direction: " + str(direction)) print("---------------------------------------------------------") print("Enter a command") cmd = raw_input("Edit (d)uty cycle,(f)requency, d(i)rection, or e(x)it: ") if cmd == 'x': break; elif cmd == 'd': duty = input("Enter a duty cycle between 0 and 100: ") elif cmd == 'f': freq = input("Enter a desired frequency: ") elif cmd == 'i': direction = not direction os.system('clear') if duty >= 0 and duty <= 100 and freq >= 1 : PWM.set_duty_cycle(M1_PWM_PIN, duty) PWM.set_frequency(M1_PWM_PIN, freq) if direction : GPIO.output(M1_DIRECTION_PIN, GPIO.HIGH) else : GPIO.output(M1_DIRECTION_PIN, GPIO.LOW) else : print("Non-legal value given.") print GPIO.cleanup() PWM.stop(M1_PWM_PIN)
f = raw_input('file name : ') filename = f + '.txt' tdata = open(filename, 'a+') tdata.write("Cal_Disp(mm),Temperature('c),Time(s) \n") a=0 R=0.0 while a!=4: a= int(input('act=1, cool=2, cycle=3, stop=4, zero=5 ')) if a==1: c = int(input('heating time(s) : ')) b = c*10+1 PWM.set_duty_cycle(Act, 100) tdata.write("Cal_Disp(mm),Temperature('c),Resistance(Ohm),Time(s) \n") for count in range(1,b): distance = myEncoder.position * 0.02 temp = max(sensor.readPixels()) #Vr=ADC.read(analogPin) #R=10000*Vr/(1.8-Vr) print("%.2f,%d,%d,%.1f\n" % (distance, temp, R, count * 0.1)) tdata.write("%.2f,%d,%d,%.1f\n" % (distance, temp, R, count * 0.1)) time.sleep(0.1) PWM.set_duty_cycle(Act, 0) for count in range(b,b+11): distance = myEncoder.position * 0.02 temp = max(sensor.readPixels()) #Vr=ADC.read(analogPin) #R=10000*Vr/(1.8-Vr)
subprocess.Popen("sudo sh -c 'echo 16666666 > period'", cwd="/sys/class/pwm/pwm-1:0", shell=True) subprocess.Popen("sudo sh -c 'echo 16666666 > period'", cwd="/sys/class/pwm/pwm-1:1", shell=True) subprocess.Popen("sudo sh -c 'echo 1 > enable'", cwd="/sys/class/pwm/pwm-1:0", shell=True) subprocess.Popen("sudo sh -c 'echo 1 > enable'", cwd="/sys/class/pwm/pwm-1:1", shell=True) PWM.start(LED2) PWM.set_frequency(LED2, LED_PWM_f) PWM.set_duty_cycle(LED2, 20) # print(PWM.VERSION) # GPIO.setup(in_OTW, GPIO.IN) # GPIO.setup(in_FAULT, GPIO.IN) GPIO.setup(reset_AB, GPIO.OUT) GPIO.setup(reset_CD, GPIO.OUT) GPIO.setup(EN_3V3, GPIO.OUT) GPIO.output(reset_AB, GPIO.LOW) GPIO.output(reset_CD, GPIO.LOW) GPIO.output(EN_3V3, GPIO.LOW) PWM.start(motor_l) PWM.start(motor_p) def cls(): os.system("clear")
import Adafruit_BBIO.ADC as ADC import Adafruit_BBIO.PWM as PWM from time import sleep LED="P9_14" pot="P9_33" ADC.setup() PWM.start(LED,0,1000) while(1): analogRead=ADC.read(pot) dutyCycle=(101**analogRead)-1 print(dutyCycle) PWM.set_duty_cycle(LED,dutyCycle) sleep(0.2)
def center_turn(): print 'Turning Straight \n' turn_c = 90 TC = float(turn_c) center = 100 - ((TC / 180) * duty_span + duty_min) PWM.set_duty_cycle(steer_pin, center)
def run_servo(pin, degrees): dc = angle_to_dc(degrees) PWM.set_duty_cycle(pin, dc)
tdata.write("Test start,start,start,start,start\n") a = 0 R = 0 global pwm pwm = 20 #main function while a != 4: a = int(input('act=1, cool=2, cycle=3, stop=4, zero=5, PWMset=6 ')) if a == 1: c = int(input('heating time(s) : ')) b = c * 10 + 1 tdata.write("Cal_Disp(mm),Temperature('c),Resistance(ohm),Time(s) \n") for count in range(1, b): PWM.set_duty_cycle(Act, pwm) distance = myEncoder.position * 0.02 temp = max(sensor.readPixels()) # Vr=ADC.read(analogPin) # R=10000*Vr/(1.8-Vr) print("%.2f,%d,%d,%.1f\n" % (distance, temp, R, count * 0.1)) tdata.write("%.2f,%d,%d,%.1f\n" % (distance, temp, R, count * 0.1)) time.sleep(0.1) PWM.set_duty_cycle(Act, 0) for count in range(b, b + 1): distance = myEncoder.position * 0.02 temp = max(sensor.readPixels()) # Vr=ADC.read(analogPin) # R=10000*Vr/(1.8-Vr) print("%.2f,%d,%d,%.1f\n" % (distance, temp, R, count * 0.1)) tdata.write("%.2f,%d,%d,%.1f\n" % (distance, temp, R, count * 0.1))
import Adafruit_BBIO.GPIO as GPIO import Adafruit_BBIO.PWM as PWM import datetime import sys from time import sleep import struct infile_path = "/dev/input/js0" EVENT_SIZE = struct.calcsize("llHHI") file = open(infile_path, "rb") event = file.read(EVENT_SIZE) pwmpin = "P9_42" dutycycle = 50 PWM.start(pwmpin, dutycycle, 10) while event: (tv_sec, tv_usec, type, code, value) = struct.unpack("llHHI", event) if value / 1000000 == 302: if dutycycle > 0: dutycycle = dutycycle - 1 elif value / 1000000 == 268: if dutycycle < 100: dutycycle = dutycycle + 1 PWM.set_duty_cycle(pwmpin, dutycycle) event = file.read(EVENT_SIZE)
def set_power(self, power): if abs(power) > self.MAX: power = copysign(self.MAX, power) time = self.stop_time + power * self.time_delta percent = 100.0 * time * self.frequency PWM.set_duty_cycle(self.port, percent)
def setSpeed(self, rpm): if rpm != 0: PWM.set_frequency(self.stepPin, SteppingMode.getFrequency(rpm)) PWM.set_duty_cycle(self.stepPin, 50) else: PWM.set_duty_cycle(self.stepPin, 0)
def __del__(self): try: PWM.set_duty_cycle(self._output, 0) finally: PWM.stop(self._output)
def Brake(): PWM.set_duty_cycle(motor_l, 50) PWM.set_duty_cycle(motor_p, 50) GPIO.output(reset_AB, GPIO.LOW) GPIO.output(reset_CD, GPIO.LOW)
def run_esc(pin, percent): dc = percent_to_dc(percent) PWM.set_duty_cycle(pin, dc)
def Light(): global LED_duty_current LED_duty_current = LED_duty_current + 10 if LED_duty_current > LED_duty_max: LED_duty_current = LED_duty_min PWM.set_duty_cycle(LED2, LED_duty_current)
def StopMotors(): PWM.set_duty_cycle(motor_l, 50) PWM.set_duty_cycle(motor_p, 50) GPIO.output(reset_AB, GPIO.HIGH) GPIO.output(reset_CD, GPIO.HIGH)
target_aoa = -target_aoa # flip if needed output_angle = target_aoa + 90 # convert aoa to absolute angle for servo # update error terms last_error = error # threshold servo commands in case of errors if output_angle > servo_max: output_angle = servo_max elif output_angle < servo_min: output_angle = servo_min # print(output_angle) if enable_output: duty = float(output_angle) duty = ((duty / 180) * duty_span + duty_min) PWM.set_duty_cycle(servo_pin, duty) except KeyboardInterrupt: # allows for easy program stop by tester break # clean up ser.close() if enable_output: PWM.stop(servo_pin) PWM.cleanup() # timing wrap up total = 0 # total time for n loops for lap in loop_times: total = total + lap loop_time_average = total / num_loops_avg
def stop(self): PWM.set_duty_cycle(self.stepPin, 0)