def main(): parser = argparse.ArgumentParser( description= 'HAL component to read ADC values and convert to temperature') parser.add_argument('-n', '--name', help='HAL component name', required=True) parser.add_argument('-i', '--interval', help='Adc update interval', default=0.05) parser.add_argument( '-c', '--channels', help= 'Komma separated list of channels and thermistors to use e.g. 01:semitec_103GT_2,02:epcos_B57560G1104', required=True) parser.add_argument('-f', '--filter_size', help='Size of the low pass filter to use', default=10) parser.add_argument('-b', '--cape_board', help='Type of cape used', default='BeBoPr') parser.add_argument( '-r', '--r_pu', default=2000, type=float, help='Divider pull-up resistor value (default 2k Ohms)') args = parser.parse_args() updateInterval = float(args.interval) filterSize = int(args.filter_size) error = False watchdog = True # Create pins pins = [] if (args.channels != ""): channelsRaw = args.channels.split(',') for channel in channelsRaw: pinRaw = channel.split(':') if (len(pinRaw) != 2): print(("wrong input")) sys.exit(1) pin = Pin() pin.pin = int(pinRaw[0]) if ((pin.pin > 5) or (pin.pin < 0)): print(("Pin not available")) sys.exit(1) checkAdcInput(pin) if (pinRaw[1] != "none"): pin.r2temp = R2Temp(pinRaw[1]) pin.filterSize = filterSize pins.append(pin) # Initialize HAL h = hal.component(args.name) for pin in pins: pin.halRawPin = h.newpin( getHalName(pin) + ".raw", hal.HAL_FLOAT, hal.HAL_OUT) if (pin.r2temp is not None): pin.halValuePin = h.newpin( getHalName(pin) + ".value", hal.HAL_FLOAT, hal.HAL_OUT) halErrorPin = h.newpin("error", hal.HAL_BIT, hal.HAL_OUT) halNoErrorPin = h.newpin("no-error", hal.HAL_BIT, hal.HAL_OUT) halWatchdogPin = h.newpin("watchdog", hal.HAL_BIT, hal.HAL_OUT) h.ready() halErrorPin.value = error halNoErrorPin.value = not error halWatchdogPin.value = watchdog try: while (True): try: for pin in pins: f = open(pin.filename, 'r') value = float(f.readline()) pin.addSample(value) pin.halRawPin.value = pin.rawValue if (pin.r2temp is not None): pin.halValuePin.value = adc2Temp(pin) error = False except IOError: error = True halErrorPin.value = error halNoErrorPin.value = not error watchdog = not watchdog halWatchdogPin.value = watchdog time.sleep(updateInterval) except: print(("exiting HAL component " + args.name)) h.exit()
def main(): parser = argparse.ArgumentParser( description='HAL component to read LSM303 Accelerometer values') parser.add_argument('-n', '--name', help='HAL component name', default="gy86") parser.add_argument('-b', '--bus_id', help='I2C bus id', default=2) parser.add_argument('-i', '--interval', help='I2C update interval', default=0.05) parser.add_argument('-d', '--delay', help='Delay before the i2c should be updated', default=0.0) args = parser.parse_args() delayInterval = float(args.delay) h = hal.component(args.name) tempPin = h.newpin("temperature", hal.HAL_FLOAT, hal.HAL_OUT) pressPin = h.newpin("pressure", hal.HAL_FLOAT, hal.HAL_OUT) anglexPin = h.newpin("angle-x", hal.HAL_FLOAT, hal.HAL_OUT) angleyPin = h.newpin("angle-y", hal.HAL_FLOAT, hal.HAL_OUT) anglezPin = h.newpin("angle-z", hal.HAL_FLOAT, hal.HAL_OUT) magxPin = h.newpin("mag-x", hal.HAL_FLOAT, hal.HAL_OUT) magyPin = h.newpin("mag-y", hal.HAL_FLOAT, hal.HAL_OUT) magzPin = h.newpin("mag-z", hal.HAL_FLOAT, hal.HAL_OUT) accelxPin = h.newpin("accel-x", hal.HAL_FLOAT, hal.HAL_OUT) accelyPin = h.newpin("accel-y", hal.HAL_FLOAT, hal.HAL_OUT) accelzPin = h.newpin("accel-z", hal.HAL_FLOAT, hal.HAL_OUT) gyroxPin = h.newpin("gyro-x", hal.HAL_FLOAT, hal.HAL_OUT) gyroyPin = h.newpin("gyro-y", hal.HAL_FLOAT, hal.HAL_OUT) gyrozPin = h.newpin("gyro-z", hal.HAL_FLOAT, hal.HAL_OUT) h.ready() i2c_bus = int(args.bus_id) # /dev/i2c-2 mpu6050 = Adafruit_I2C(0x68, i2c_bus) # ADC 16bit hmc5883 = Adafruit_I2C(0x1e, i2c_bus) # ADC 12bit ms5611 = Adafruit_I2C(0x77, i2c_bus) # ADC 24bit # mpu6050 i2c master enable bit mpu6050.write8(0x6A, 0) # mpu6050 i2c bypass enable bit mpu6050.write8(0x37, 2) # mpu6050 turn off sleep mode mpu6050.write8(0x6B, 0) # hmc5883 continuous mode hmc5883.write8(0x02, 0x00) # ms5611 reset ms5611.writeList(0x1e, []) time.sleep(2 / 1000.0) # ms5611 prom t = ms5611.readList(0xa2, 2) c1 = t[0] * 256 + t[1] t = ms5611.readList(0xa4, 2) c2 = t[0] * 256 + t[1] t = ms5611.readList(0xa6, 2) c3 = t[0] * 256 + t[1] t = ms5611.readList(0xa8, 2) c4 = t[0] * 256 + t[1] t = ms5611.readList(0xaa, 2) c5 = t[0] * 256 + t[1] t = ms5611.readList(0xac, 2) c6 = t[0] * 256 + t[1] # c1=40127 # c2=36924 # c3=23317 # c4=23282 # c5=33464 # c6=28312 try: time.sleep(delayInterval) while True: # read mpu6050 accelerations in m/s^2 ax = (mpu6050.readS8(0x3b) * 256 + mpu6050.readU8(0x3c)) / 16384.0 ay = (mpu6050.readS8(0x3d) * 256 + mpu6050.readU8(0x3e)) / 16384.0 az = (mpu6050.readS8(0x3f) * 256 + mpu6050.readU8(0x40)) / 16384.0 # read mpu6050 temperature in C tt = (mpu6050.readS8(0x41) * 256 + mpu6050.readU8(0x42)) / 340.0 + 36.53 # read mpu6050 gyroscope in degree/s gx = (mpu6050.readS8(0x43) * 256 + mpu6050.readU8(0x44)) / 131.0 gy = (mpu6050.readS8(0x45) * 256 + mpu6050.readU8(0x46)) / 131.0 gz = (mpu6050.readS8(0x47) * 256 + mpu6050.readU8(0x48)) / 131.0 # wait hmc5883 to be ready while not hmc5883.readU8(0x09) & 0x01 == 1: pass # read hmc5883 magnetometer in gauss magx = (hmc5883.readS8(0x3) * 256 + hmc5883.readU8(0x4)) / 1090.0 magz = (hmc5883.readS8(0x5) * 256 + hmc5883.readU8(0x6)) / 1090.0 magy = (hmc5883.readS8(0x7) * 256 + hmc5883.readU8(0x8)) / 1090.0 z = math.degrees(math.atan2(magy, magx)) y = math.degrees(-math.atan2(ax, az)) x = math.degrees(math.atan2(ay, az)) ms5611.writeList(0x48, []) time.sleep(9 / 1000.0) adc = ms5611.readList(0x00, 3) d1 = adc[0] * 65536 + adc[1] * 256 + adc[2] # d1=9085466 ms5611.writeList(0x58, []) time.sleep(9 / 1000.0) adc = ms5611.readList(0x00, 3) d2 = adc[0] * 65536 + adc[1] * 256 + adc[2] # d2=8569150 dt = d2 - c5 * (1 << 8) temp = 2000 + dt * c6 / (1 << 23) off = c2 * (1 << 16) + c4 * dt / (1 << 7) sens = c1 * (1 << 15) + c3 * dt / (1 << 8) p = (d1 * sens / (1 << 21) - off) / (1 << 15) # print c1,c2,c3,c4,c5,c6,d1,d2,dt,temp,off,sens,p tempPin.value = temp / 100.0 pressPin.value = p / 100.0 anglexPin.value = x angleyPin.value = y anglezPin.value = z magxPin.value = magx magyPin.value = magy magzPin.value = magz accelxPin.value = ax accelyPin.value = ay accelzPin.value = az gyroxPin.value = gx gyroyPin.value = gy gyrozPin.value = gz except: print(("exiting HAL component " + args.name)) h.exit()
def main(): parser = argparse.ArgumentParser( description='HAL component to read LSM303 Accelerometer values') parser.add_argument('-n', '--name', help='HAL component name', required=True) parser.add_argument('-b', '--bus_id', help='I2C bus id', default=2) parser.add_argument('-a', '--address', help='I2C device address', default=0x20) parser.add_argument('-i', '--interval', help='I2C update interval', default=0.05) parser.add_argument( '-op', '--output_pins', help='Komma separated list of output pins e.g. A01,B02', default="") parser.add_argument('-ip', '--input_pins', help='Komma separated list of input pins e.g. A01,B02', default="") parser.add_argument('-d', '--delay', help='Delay before the i2c should be updated', default=0.0) args = parser.parse_args() updateInterval = float(args.interval) delayInterval = float(args.delay) error = True watchdog = True gpio = MCP23017(busId=int(args.bus_id), address=int(args.address)) # Parse arguments pins = [] if (args.output_pins != ""): outputPinsRaw = args.output_pins.split(',') for pinRaw in outputPinsRaw: pins.append(parseInputPin(pinRaw, MCP23017.DIR_OUT)) if (args.input_pins != ""): inputPinsRaw = args.input_pins.split(',') for pinRaw in inputPinsRaw: pins.append(parseInputPin(pinRaw, MCP23017.DIR_IN)) if (len(pins) == 0): print(("No pins specified")) sys.exit(1) # Initialize HAL h = hal.component(args.name) for pin in pins: if (pin.direction == MCP23017.DIR_IN): pin.halPin = h.newpin(getHalName(pin), hal.HAL_BIT, hal.HAL_OUT) else: pin.halPin = h.newpin(getHalName(pin), hal.HAL_BIT, hal.HAL_IN) pin.halPullupPin = h.newpin( getHalName(pin) + ".pullup", hal.HAL_BIT, hal.HAL_IN) pin.halInvertedPin = h.newpin( getHalName(pin) + ".invert", hal.HAL_BIT, hal.HAL_IN) halErrorPin = h.newpin("error", hal.HAL_BIT, hal.HAL_OUT) halNoErrorPin = h.newpin("no-error", hal.HAL_BIT, hal.HAL_OUT) halWatchdogPin = h.newpin("watchdog", hal.HAL_BIT, hal.HAL_OUT) h.ready() halErrorPin.value = error halNoErrorPin.value = not error halWatchdogPin.value = watchdog try: time.sleep(delayInterval) while (True): try: if (error): gpio.init() error = False gpio.read() # read for pin in pins: gpio.setDir(pin.port, pin.pin, pin.direction) if (pin.direction == MCP23017.DIR_IN): pin.halPin.value = gpio.getValue( pin.port, pin.pin) != pin.halInvertedPin.value else: gpio.setValue( pin.port, pin.pin, pin.halPin.value != pin.halInvertedPin.value) pullup = pin.halPullupPin.value if (pullup): gpio.setPullup(pin.port, pin.pin, MCP23017.PULLUP_EN) else: gpio.setPullup(pin.port, pin.pin, MCP23017.PULLUP_DIS) gpio.write() # write except IOError as e: error = True halErrorPin.value = error halNoErrorPin.value = not error watchdog = not watchdog halWatchdogPin.value = watchdog time.sleep(updateInterval) except: print(("exiting HAL component " + args.name)) h.exit()
def main(): parser = argparse.ArgumentParser( description='HAL component to read Temperature values over I2C') parser.add_argument('-n', '--name', help='HAL component name', required=True) parser.add_argument('-b', '--bus_id', help='I2C bus id', default=2) parser.add_argument('-a', '--address', help='I2C device address', default=0x20) parser.add_argument('-i', '--interval', help='I2C update interval', default=0.05) parser.add_argument( '-c', '--channels', help= 'Comma separated list of channels and thermistors to use e.g. 01:semitec_103GT_2,02:epcos_B57560G1104', required=True) parser.add_argument('-f', '--filter_size', help='Size of the low pass filter to use', default=10) parser.add_argument('-d', '--delay', help='Delay before the i2c should be updated', default=0.0) args = parser.parse_args() updateInterval = float(args.interval) delayInterval = float(args.delay) filterSize = int(args.filter_size) error = True watchdog = True adc = ADS7828(busId=int(args.bus_id), address=int(args.address)) # Create pins pins = [] if (args.channels != ""): channelsRaw = args.channels.split(',') for channel in channelsRaw: pinRaw = channel.split(':') if (len(pinRaw) != 2): print(("wrong input")) sys.exit(1) pin = Pin() pin.pin = int(pinRaw[0]) if ((pin.pin > 7) or (pin.pin < 0)): print(("Pin not available")) sys.exit(1) if (pinRaw[1] != "none"): pin.r2temp = R2Temp(pinRaw[1]) pin.filterSize = filterSize pins.append(pin) # Initialize HAL h = hal.component(args.name) for pin in pins: pin.halRawPin = h.newpin( getHalName(pin) + ".raw", hal.HAL_FLOAT, hal.HAL_OUT) if (pin.r2temp is not None): pin.halValuePin = h.newpin( getHalName(pin) + ".value", hal.HAL_FLOAT, hal.HAL_OUT) halErrorPin = h.newpin("error", hal.HAL_BIT, hal.HAL_OUT) halNoErrorPin = h.newpin("no-error", hal.HAL_BIT, hal.HAL_OUT) halWatchdogPin = h.newpin("watchdog", hal.HAL_BIT, hal.HAL_OUT) h.ready() halErrorPin.value = error halNoErrorPin.value = not error halWatchdogPin.value = watchdog try: time.sleep(delayInterval) while (True): try: for pin in pins: value = float(adc.readChannel(pin.pin)) pin.addSample(value) pin.halRawPin.value = pin.rawValue if (pin.r2temp is not None): pin.halValuePin.value = adc2Temp(pin) error = False except IOError as e: error = True halErrorPin.value = error halNoErrorPin.value = not error watchdog = not watchdog halWatchdogPin.value = watchdog time.sleep(updateInterval) except: print(("exiting HAL component " + args.name)) h.exit()
#!/usr/bin/env python3 import machinekit.hal.pyhal as hal import os h = hal.component("x") try: pins = dict(HAL_S32=h.newpin("HAL_S32", hal.HAL_S32, hal.HAL_OUT), HAL_U32=h.newpin("HAL_U32", hal.HAL_U32, hal.HAL_OUT), HAL_S64=h.newpin("HAL_S64", hal.HAL_S64, hal.HAL_OUT), HAL_U64=h.newpin("HAL_U64", hal.HAL_U64, hal.HAL_OUT), HAL_FLOAT=h.newpin("HAL_FLOAT", hal.HAL_FLOAT, hal.HAL_OUT)) param = h.newparam("param", hal.HAL_BIT, hal.HAL_RW) h.ready() ########## Set and read pin values; access pins via hal.compenent.__getitem__() def try_set(p, v): try: h[p] = v hp = h[p] print("set {} {} {}".format(p, v, "ok" if hp == v else repr(hp))) except Exception as e: print("set {} {} {}: {}".format(p, v, type(e).__name__, e)) print("S32 pass") # pass try_set("HAL_S32", -1) try_set("HAL_S32", 0) try_set("HAL_S32", 1) try_set("HAL_S32", 0x7fffffff) # INT32_MAX try_set("HAL_S32", -0x80000000) # INT32_MIN try_set("HAL_S32", 1e5) # float