self._cb.cancel() if __name__ == "__main__": import time import pigpio import read_PWM PWM_GPIO = 4 RUN_TIME = 1000.0 SAMPLE_TIME = 2.0 pi = pigpio.pi() # Grants access t Pi's GPIO p = read_PWM.reader(pi, PWM_GPIO) start = time.time() while (time.time() - start) < RUN_TIME: time.sleep(SAMPLE_TIME) pp = p.pulse_period() pw = p.pulse_width() conc = 5000.0 * (pw - 2) / (pp - 4) print("t={} pp={} pw={} c={:.1f}".format(time.time(), pp, pw, conc)) p.cancel()
#motor command pigpio Servo = pigpio.pi() #motor pigpio mode Servo.set_mode(ServoPin, pigpio.OUTPUT) #reciever pigpio RCJoyXPigpio = pigpio.pi() # RC Channel 1 (left/right) RCJoyYPigpio = pigpio.pi() # RC channel 2 (forward/back) ServoControlPigpio = pigpio.pi() # RC channel 3 (forward/back) KillSwitchPigpio = pigpio.pi() # RC channel 5 (robot kill switch) RCSwitchPigpio = pigpio.pi() # RC channel 6 (RC/Autonomous switch) #start pigpio reader RCJoyXReader = read_PWM.reader(RCJoyXPigpio, RCJoyXPin) # RC Channel 1 (left/right) RCJoyYReader = read_PWM.reader(RCJoyYPigpio, RCJoyYPin) # RC channel 2 (forward/back) ServoControlReader = read_PWM.reader( ServoControlPigpio, ServoControlPin) # RC channel 3 (forward/back) KillSwitchReader = read_PWM.reader( KillSwitchPigpio, KillSwitchPin) # RC channel 5 (robot kill switch) RCSwitchReader = read_PWM.reader( RCSwitchPigpio, RCSwitchPin) # RC channel 6 (RC/Autonomous switch) #Give time to initialize time.sleep(1.0) try: while True: LeftMotor.ChangeDutyCycle(100)
def acquire_data(main_queue, killer, pi, i2c_handle_6b, i2c_handle_69, i2c_handle_ADS7828, reporting_interval): #new_data_event): #target data string: #timestamp ,lat, ,lon ,el ,press , temp , level, qual,rpm #2016-03-16T16:15:04Z,32.758,-97.448,199.700,32.808,63.722,26.887,2.144,0.000 PWM_GPIO = 24 # to pin 18 on pi, mistakenly labeled "5" on rev c of the interface PCB RPM_GPIO = 17 # to pin 11 on the pi. #for reference, the ups uses gpio 27 (pin 13) to shutdown the pi (FSSD) and 22 (pin 15) is use for pulse train output to the ups (watchdog), 4 is used by the GPS PPS output, STACounter enable on 25 (pin 22) led_state = 0 analog_sensors = ADS7828.ADS7828(pi, i2c_handle_ADS7828) oil_sensor = read_PWM.reader(pi, PWM_GPIO) rpm_sensor = read_RPM.reader(pi, RPM_GPIO) UPS_Pico_run_prior = 0 UPS_Pico_run_temp = 0 UPS_Pico_run_read_count = 0 UPS_Pico_run_read_errors = 0 UPS_PICO_RUN_MAX_READ_ERRORS = 5 if (reporting_interval == 0): reporting_interval = 5 gps_sensor = GPS_AdafruitSensor(interface='/dev/ttyAMA0') time_format_str = '%Y-%m-%dT%H:%M:%SZ' system_clock_set_from_gps = False while not killer.kill_now: try: logging.debug('acquire_data process launched...') while not killer.kill_now: #---------verifying that the Pico UPS is running properly ------------- UPS_Pico_run_now = pi.i2c_read_word_data( i2c_handle_69, 0x0e) #rolling vale, should change after each read. if (UPS_Pico_run_now == UPS_Pico_run_prior): UPS_Pico_run_read_errors += 1 if (UPS_Pico_run_read_errors >= UPS_PICO_RUN_MAX_READ_ERRORS): raise TimeoutError( 'The UPS Pico_run register (UPS Pico module status register 69 0x0E) has returend the same value (' + str(UPS_Pico_run_now) + ') ' + str(UPS_Pico_run_read_errors) + ' times, which most likely indicates that the UPS is not running (either shutdown or locked up). The number of successful reads prior to error: ' + str(UPS_Pico_run_read_count)) else: UPS_Pico_run_read_errors = 0 UPS_Pico_run_read_count += 1 UPS_Pico_run_prior = UPS_Pico_run_now time.sleep(reporting_interval) #---------begin data colection ------------- timestamp = gps_sensor.timestamp timestamp_str = timestamp.strftime(time_format_str) pressure_str = analog_sensors.getUpdate() oil_str = oil_sensor.getUpdate() rpm_str = rpm_sensor.getUpdate() #---------concatenating, checking minimum number of fields and date validity ------------- # all of the fields have leading commas. data_str = timestamp_str + gps_sensor.data_str( ) + pressure_str + oil_str + rpm_str if (len(data_str.split(',')) < 10): logging.debug( 'Invalid data length, < 10 after split on comma, ACTUAL DATA: ' + data) continue if (timestamp.year < 2016): logging.debug( 'Discarding sample with an invalid timestamp: ' + timestamp_str) continue else: if not system_clock_set_from_gps: #only done once at startup logging.debug('Setting date according to GPS') os.system('sudo date -s ' + timestamp_str) system_clock_set_from_gps = True filename = gps_sensor.timestamp.strftime( "%Y-%m-%d_%H.csv") #change log file every hour main_queue.put(('new data', filename, data_str)) # print(data_str) led_state = 1 - led_state pi.i2c_write_byte_data( i2c_handle_6b, 0x0D, led_state ) #toggle the red LED to show data is being processed except Exception as ex: logging.error('acquire_data: ' + traceback.format_exc()) main_queue.put(('quit', )) finally: logging.debug('DAQ thread caught stop signal.') try: pi.i2c_write_byte_data(i2c_handle_6b, 0x0D, 0) #turn off the red LED r.cancel() #stop the call backs p.cancel() except: pass main_queue.put(('DAQ thread stopped', )) logging.debug('DAQ thread is exiting.')
if __name__ == "__main__": #debug test routine if called standalone (not from a parent program) import time import pigpio import read_PWM import threading PWM_GPIO = 24 #pin 18 RUN_TIME = 60.0 print("PWM GPIO= " + str(PWM_GPIO) + ", RUN_TIME= " + str(RUN_TIME)) pi = pigpio.pi() pi.set_mode(PWM_GPIO, pigpio.INPUT) p = read_PWM.reader(pi, PWM_GPIO) start = time.time() while (time.time() - start) < RUN_TIME: time.sleep(5) f = p.frequency() pw = p.pulse_width() dc = p.duty_cycle() print(p.getUpdate_debug()) p.cancel() pi.stop()
def acquire_data(main_queue, killer, pi, i2c_handle_6b, i2c_handle_69, i2c_handle_ADS7828, reporting_interval): #new_data_event): #target data string: #timestamp ,lat, ,lon ,el ,press , temp , level, qual,rpm #2016-03-16T16:15:04Z,32.758,-97.448,199.700,32.808,63.722,26.887,2.144,0.000 PWM_GPIO = 24 # to pin 18 on pi, mistakenly labeled "5" on rev c of the interface PCB RPM_GPIO = 17 # to pin 11 on the pi. #for reference, the ups uses gpio 27 (pin 13) to shutdown the pi (FSSD) and 22 (pin 15) is use for pulse train output to the ups (watchdog), 4 is used by the GPS PPS output, STACounter enable on 25 (pin 22) led_state = 0 analog_sensors = ADS7828.ADS7828(pi, i2c_handle_ADS7828) oil_sensor = read_PWM.reader(pi, PWM_GPIO) rpm_sensor = read_RPM.reader(pi, RPM_GPIO) UPS_Pico_run_prior = 0 UPS_Pico_run_temp = 0 UPS_Pico_run_read_count = 0 UPS_Pico_run_read_errors = 0 UPS_PICO_RUN_MAX_READ_ERRORS = 5 if(reporting_interval == 0): reporting_interval = 5 gps_sensor = GPS_AdafruitSensor(interface='/dev/ttyAMA0') time_format_str = '%Y-%m-%dT%H:%M:%SZ' system_clock_set_from_gps = False while not killer.kill_now: try: logging.debug('acquire_data process launched...') while not killer.kill_now: #---------verifying that the Pico UPS is running properly ------------- UPS_Pico_run_now = pi.i2c_read_word_data(i2c_handle_69, 0x0e) #rolling vale, should change after each read. if(UPS_Pico_run_now == UPS_Pico_run_prior): UPS_Pico_run_read_errors += 1 if(UPS_Pico_run_read_errors >= UPS_PICO_RUN_MAX_READ_ERRORS): raise TimeoutError('The UPS Pico_run register (UPS Pico module status register 69 0x0E) has returend the same value (' + str(UPS_Pico_run_now) + ') ' + str(UPS_Pico_run_read_errors) + ' times, which most likely indicates that the UPS is not running (either shutdown or locked up). The number of successful reads prior to error: ' + str(UPS_Pico_run_read_count)) else: UPS_Pico_run_read_errors = 0 UPS_Pico_run_read_count += 1 UPS_Pico_run_prior = UPS_Pico_run_now time.sleep(reporting_interval) #---------begin data colection ------------- timestamp = gps_sensor.timestamp timestamp_str = timestamp.strftime(time_format_str) pressure_str = analog_sensors.getUpdate() oil_str = oil_sensor.getUpdate() rpm_str = rpm_sensor.getUpdate() #---------concatenating, checking minimum number of fields and date validity ------------- # all of the fields have leading commas. data_str = timestamp_str + gps_sensor.data_str() + pressure_str + oil_str + rpm_str if(len(data_str.split(',')) < 10): logging.debug('Invalid data length, < 10 after split on comma, ACTUAL DATA: ' + data) continue if(timestamp.year < 2016): logging.debug('Discarding sample with an invalid timestamp: ' + timestamp_str) continue else: if not system_clock_set_from_gps: #only done once at startup logging.debug('Setting date according to GPS') os.system('sudo date -s ' + timestamp_str) system_clock_set_from_gps = True filename = gps_sensor.timestamp.strftime("%Y-%m-%d_%H.csv") #change log file every hour main_queue.put(('new data', filename, data_str)) # print(data_str) led_state = 1 - led_state pi.i2c_write_byte_data(i2c_handle_6b, 0x0D, led_state) #toggle the red LED to show data is being processed except Exception as ex: logging.error('acquire_data: ' + traceback.format_exc()) main_queue.put(('quit',)) finally: logging.debug('DAQ thread caught stop signal.') try: pi.i2c_write_byte_data(i2c_handle_6b, 0x0D, 0) #turn off the red LED r.cancel() #stop the call backs p.cancel() except: pass main_queue.put(('DAQ thread stopped',)) logging.debug('DAQ thread is exiting.')
import pigpio import read_PWM ## input pins ## PWM_GPIO1 = 4 PWM_GPIO2 = 5 ################ RUN_TIME = 60.0 SAMPLE_TIME = 1.0 pi = pigpio.pi() for g in GPIO: pi.set_mode(g, pigpio.OUTPUT) p1 = read_PWM.reader(pi, PWM_GPIO1) p2 = read_PWM.reader(pi, PWM_GPIO2) start = time.time() while (time.time() - start) < RUN_TIME: time.sleep(SAMPLE_TIME) ## signal information from input pins ## ## first pin (4) ## f1 = p1.frequency() pw1 = p1.pulse_width() dc1 = p1.duty_cycle() ## second pin (5) ## f2 = p2.frequency()
from flask import Flask, request, jsonify, render_template, Response from flask_restful import Resource, Api import json import serial from threading import Thread import binascii import time import pigpio import read_PWM # GPIO pin to read the frequency from fuel sensor FUELSENSOR_GPIO = 4 pi = pigpio.pi() fuelsensor_input = read_PWM.reader(pi, FUELSENSOR_GPIO) # Length of low pass filter frequencies_length = 100 # Circular buffer to store previous n frequencies frequencies = [] # Index to keep track of the oldest frequency to be replaced frequencies_index = 0 port = serial.Serial("/dev/serial0", baudrate=19200, timeout=3.0) # Starting value for the engine data engine_data = {'FUEL': 0, 'PWR': 0, 'RPM': 0, 'MANP': 0, 'FUELP': 0, 'OILP': 0, 'OILT': 0, 'CHT1': 0, 'CHT2': 0, 'CHT3': 0, 'CHT4': 0, 'CHT5': 0, 'CHT6': 0, 'EGT1': 0, 'EGT2': 0, 'EGT3': 0, 'EGT4': 0, 'EGT5': 0, 'EGT6': 0, 'ERRORS':[] } # All the possible faults faults = { 0: "Manifold Pressure Sensor Fault", 1: "Fuel Pressure Sensor Fault",
TRIAL_NUM = 0 for file in os.listdir('.'): if 'drive_trial_' in file: TRIAL_NUM += 1 # Setup the camera camera = PiCamera() camera.resolution = (FRAME_W, FRAME_H) camera.framerate = FRAME_RATE rawCapture = PiRGBArray(camera, size=(FRAME_W, FRAME_H)) time.sleep(0.1) # Setup PWM reader pi = pigpio.pi() steer_pwm = read_PWM.reader(pi, STEER_GPIO) # Start recording trial = 'drive_trial_' + str(TRIAL_NUM) print trial os.system('mkdir ' + trial) os.system('mkdir ' + trial + '/image') os.system('mkdir ' + trial + '/steer') counter = 0 for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): # Grab the raw NumPy array representing the image image = cv2.flip(frame.array, 0) #cv2.imshow("Frame", image)