sacada 3.21 KB
Newer Older
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
1 2 3 4 5
#!/usr/bin/env python3

from time import sleep
import click
from tqdm import tqdm
6
from thermocouples_reference import thermocouples
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
7 8
from sacada.SACADA import SACADA

9 10
typeR = thermocouples['R']

Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
11
@click.group()
12
@click.option('--port', default="/dev/ttyACM0", help='serial port location eg.: /dev/ttyACM0 (default)')
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
@click.pass_context
def cli(ctx, port):
    ctx.obj['s'] = SACADA(port)

@cli.command()
@click.pass_context
def show(ctx):
    click.echo(ctx.obj['s'].identify())

@cli.command()
@click.argument('channel')
@click.pass_context
def read(ctx, channel):
    click.echo(ctx.obj['s'].readVoltage(channel))

@cli.command()
@click.argument('channel')
@click.option('--interval', default=1000, help='interval bewteen readings (ms)')
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
@click.option('--save', default='', help='file to save readings')
@click.pass_context
def monitor(ctx, channel, interval, save):
    #with tqdm(total=256, desc="Channel {}".format(channel), bar_format="{desc}|{bar}|{n_fmt}mV") as bar:
    #while True:
    #    bar.n = int(ctx.obj['s'].readVoltage(channel) * 1000)
    #        bar.refresh()
    #        sleep(interval/1000.0)
    if not save:
        while True:
            click.echo(ctx.obj['s'].readVoltage(channel))
            sleep(interval/1000.0)
    else:
        f = open(save, 'w')
        try:
            while True:
                voltage = ctx.obj['s'].readVoltage(channel)
                click.echo(voltage)
                f.write(str(voltage) + '\n')
                sleep(interval/1000.0)
        except KeyboardInterrupt:
            f.close()

@cli.command()
@click.option('--interval', default=1000, help='interval bewteen readings (ms)')
@click.option('--save', default='', help='file to save readings')
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
57
@click.pass_context
58 59 60 61 62 63 64
def lapma(ctx, interval, save):
    #with tqdm(total=256, desc="Channel {}".format(channel), bar_format="{desc}|{bar}|{n_fmt}mV") as bar:
    #while True:
    #    bar.n = int(ctx.obj['s'].readVoltage(channel) * 1000)
    #        bar.refresh()
    #        sleep(interval/1000.0)
    if not save:
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
65
        while True:
66
            click.echo(ctx.obj['s'].readVoltage(channel))
Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
67
            sleep(interval/1000.0)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    else:
        f = open(save, 'w')
        try:
            while True:
                voltage = ctx.obj['s'].readVoltage("A3")
                current = ctx.obj['s'].readVoltage("A4")
                thermocouple = ctx.obj['s'].readVoltage("TC")
                try:
                    temperature = typeR.inverse_CmV((thermocouple + 0.001453125034) * -1000, Tref=25)
                except ValueError:
                    temperature = "Error"
                click.echo("Voltage: {}".format(voltage))
                click.echo("Current: {}".format(current))
                click.echo("Power: {}".format(voltage * current))
                click.echo("TC (mV): {}".format(thermocouple))
                click.echo("TC (celsius): {}".format(temperature))
                click.echo("-----------------")
                f.write(str(voltage) + '\t')
                f.write(str(current) + '\t')
                f.write(str(thermocouple) + '\t')
                f.write(str(temperature) + '\n')
                sleep(interval/1000.0)
        except KeyboardInterrupt:
            f.close()

Pedro Henrique Kopper's avatar
Pedro Henrique Kopper committed
93 94 95

if __name__ == '__main__':
    cli(obj={})