Skip to content

Commit

Permalink
Digital: use cli to control LED's or with python
Browse files Browse the repository at this point in the history
  • Loading branch information
epccs committed Feb 26, 2018
1 parent eb4c495 commit 56493e5
Show file tree
Hide file tree
Showing 14 changed files with 989 additions and 41 deletions.
2 changes: 1 addition & 1 deletion AmpHr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 2,7 @@

## Overview

ADC6 is connected to a high side current sense. The [Adc] firmware is included to provide the readings. This interactive command line program demonstrates how the ATmega328p can be used to estimate the discharge.
Accumulate ADC readings at timed intervals from a resistor sensor. This is an interactive command line program that demonstrates how the ATmega328p can be used to keep track of charge (or discharge).

[Adc]: ../Adc

Expand Down
70 changes: 44 additions & 26 deletions AmpHr/chrg_accum.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 27,37 @@ For a copy of the GNU General Public License use
#include "../Adc/references.h"
#include "chrg_accum.h"

// ADC6 is a high side current sense of the input current
// converted to current with analogRead(PWR_I)*(5.0/1024.0)/(0.068*50.0))
// the fine accumulator is for the adc reading * millis counts (e.g. it is in mSec and needs scaled)

static unsigned long chrgTmrStartOfAccum;
static unsigned long chrgTmrStarted;
static unsigned long chrgTmrStartOfAccum[ADC_CHANNELS];
static unsigned long chrgTmrStarted[ADC_CHANNELS];
#define CHRG_ACCUMULATION_DELAY_MILSEC 20UL

static unsigned long chrg_accum;
static unsigned long chrg_accum_fine;
static unsigned long chrg_accum[ADC_CHANNELS];
static unsigned long chrg_accum_fine[ADC_CHANNELS];

#define SERIAL_PRINT_DELAY_MILSEC 60000UL
static unsigned long serial_print_started_at;

// scale accumulated adc*time to mAHr
static float adc_accum2mAHr[ADC_CHANNELS] = {
[0] = ((1/1.0E6)/1024.0)/(0.018*50.0)/3.6, // 0.018 ohm sense resistor with gain of 50
[1] = ((1/1.0E6)/1024.0)/(100.0)/3.6, // 100 ohm resistor, e.g. for a 4 to 20mA sensor
[2] = 0,
[3] = 0,
[4] = 0, //used for SDA of I2C
[5] = 0, //used for SCL of I2C
[6] = ((1/1.0E6)/1024.0)/(0.068*50.0)/3.6, // PWR_I is 0.068 ohm sense resistor with gain of 50
[7] = 0 // PWR_V
};

// Accumulated Charge in mAHr
float ChargeAccum(void)
float ChargeAccum(uint8_t channel)
{
return chrg_accum * ((ref_extern_avcc_uV/1.0E6)/1024.0)/(0.068*50.0)/3.6;
float temp = 0.0;
if (channel < ADC_CHANNELS)
{
temp = adc_accum2mAHr[channel] * ref_extern_avcc_uV * chrg_accum[channel];
}
return temp;
}

void Charge(void)
Expand All @@ -65,7 78,7 @@ void Charge(void)
}
else if ( (command_done == 11) )
{
printf_P(PSTR("\"%1.2f\","),ChargeAccum() );
printf_P(PSTR("\"%1.2f\","),ChargeAccum(PWR_I) );
command_done = 16;
}
else if ( (command_done == 16) )
Expand All @@ -75,7 88,7 @@ void Charge(void)
}
else if ( (command_done == 17) )
{
printf_P(PSTR("\"%1.2f\""),((chrgTmrStarted - chrgTmrStartOfAccum)/1000.0));
printf_P(PSTR("\"%1.2f\""),((chrgTmrStarted[PWR_I] - chrgTmrStartOfAccum[PWR_I])/1000.0));
printf_P(PSTR("}\r\n"));
command_done = 18;
}
Expand All @@ -89,36 102,41 @@ void Charge(void)
}
}

/* check power accumulation
/* update charge accumulation
charge accumulation is an integer value of the ADC*(millis()/1000) and will need scaled
*/
void CheckChrgAccumulation(void)
void CheckChrgAccumulation(uint8_t channel)
{
unsigned long kRuntime= millis() - chrgTmrStarted;
unsigned long kRuntime= millis() - chrgTmrStarted[channel];
if ((kRuntime) > ((unsigned long)CHRG_ACCUMULATION_DELAY_MILSEC))
{
chrg_accum_fine = (analogRead(PWR_I) * CHRG_ACCUMULATION_DELAY_MILSEC);
chrgTmrStarted = CHRG_ACCUMULATION_DELAY_MILSEC;
chrg_accum_fine[channel] = (analogRead(channel) * CHRG_ACCUMULATION_DELAY_MILSEC);
chrgTmrStarted[channel] = CHRG_ACCUMULATION_DELAY_MILSEC;
}
else
{
// check if fine accumulator has enough to add it to the Sec based accumulater
if (chrg_accum_fine > 1000)
if (chrg_accum_fine[channel] > 10000)
{
chrg_accum[channel] =10;
chrg_accum_fine[channel] -= 10000;
}
else if (chrg_accum_fine[channel] > 1000)
{
chrg_accum;
chrg_accum_fine -= 1000;
chrg_accum[channel];
chrg_accum_fine[channel] -= 1000;
}
}
}

/* The charge and discharge accumulation values need to be zeroed at the start of the day
/* For when the charge accumulation values need to be zeroed
*/
uint8_t init_ChargAccumulation(void)
uint8_t init_ChargAccumulation(uint8_t channel)
{
chrg_accum = 0;
chrg_accum_fine = 0;
chrgTmrStarted = millis();
chrgTmrStartOfAccum = chrgTmrStarted;
chrg_accum[channel] = 0;
chrg_accum_fine[channel] = 0;
chrgTmrStarted[channel] = millis();
chrgTmrStartOfAccum[channel] = chrgTmrStarted[channel];

// laod reference calibration from eeprom
if ( ! LoadAnalogRefFromEEPROM() )
Expand Down
6 changes: 3 additions & 3 deletions AmpHr/chrg_accum.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 3,9 @@

extern void Charge(void);

extern void CheckChrgAccumulation(void);
extern uint8_t init_ChargAccumulation(void);
extern void CheckChrgAccumulation(uint8_t);
extern uint8_t init_ChargAccumulation(uint8_t);

extern float ChargeAccum(void);
extern float ChargeAccum(uint8_t);

#endif // ChrgAccum_H
4 changes: 2 additions & 2 deletions AmpHr/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 82,7 @@ void callback_for_day_attach(void)
void callback_for_night_attach(void)
{
// setup AmpHr accumulators and load Adc calibration reference
if (!init_ChargAccumulation()) // ../AmpHr/power_storage.c
if (!init_ChargAccumulation(PWR_I)) // ../AmpHr/power_storage.c
{
blink_delay = BLINK_DELAY/4;
}
Expand Down Expand Up @@ -216,7 216,7 @@ int main(void)
adc_burst();

// check how much current went through high side sensor
CheckChrgAccumulation();
CheckChrgAccumulation(PWR_I);

// check if character is available to assemble a command, e.g. non-blocking
if ( (!command_done) && uart0_available() ) // command_done is an extern from parse.h
Expand Down
197 changes: 197 additions & 0 deletions Digital/Blink.py
Original file line number Diff line number Diff line change
@@ -0,0 1,197 @@
#!/usr/bin/env python3
# Line ending is the gift from hell, on Linux lines end with LF, on Windows most editors add a CR-LF pair
# but that will confuse bash and/or the env function and thus most liky look for "python\r" and fail.
# this python file also needs to be set as an executable with chmod x load_profile.py

# I am using python3 so make sure the package is installed for it.
# sudo apt-get update
# sudo apt-get install python3-serial
import json
import serial
from time import sleep

# address on the RPU_BUS to interact with
rpu_addr = '1'

# When the host serial port is open it will cause nDTR or nRTS to be set active wHIGH will tell the
# RPUadpt to send a bootload address and
# reset a device that will run a bootloader for a few seconds (set the timeout longer if the bootloader needs more time).
sio = serial.Serial("/dev/ttyUSB0",38400, timeout=3)

rpubus_echo = sio.readline().strip()

# status for user to see
print("init: " rpubus_echo[-10:].decode("utf-8"))

# checking if an output ? command was printed
if ( rpubus_echo[-7:] == b'?'):
# stop the command that may be running after a reset
sio.write(("\n").encode('ascii'))
sleep(0.2)
command = "/" rpu_addr
sio.write((command "\n").encode('ascii'))
rpubus_echo = sio.readline().strip()
print("echo: " rpubus_echo.decode("utf-8"))
while (not (rpubus_echo == (command).encode('ascii') ) ) :
sleep(0.2)
print("miss catch of command prompt, trying again")
sio.write((command "\n").encode('ascii'))
rpubus_echo = sio.readline().strip()
print("echo: " rpubus_echo.decode("utf-8"))
print("ready for next command")
else:
sio.write(("\n").encode('ascii'))
sleep(0.2)
command = "/" rpu_addr
sio.write((command "\n").encode('ascii'))
rpubus_echo = sio.readline().strip()
print("echo: " rpubus_echo.decode("utf-8"))
while (not (rpubus_echo == (command).encode('ascii') ) ) :
sleep(0.2)
print("miss catch of command prompt, trying again")
sio.write((command "\n").encode('ascii'))
rpubus_echo = sio.readline().strip()
print("echo: " rpubus_echo.decode("utf-8"))
print("ready for next command")

# set the the LED control pins in OUTPUT mode
command = "/" rpu_addr "/pMod 3,OUTPUT"
sio.write((command "\n").encode('ascii'))
sleep(0.2)
rpubus_echo = sio.readline().strip()
print("cmd echo: " rpubus_echo.decode("utf-8"))
rpubus_echo = sio.readline().strip()
print("json echo: " rpubus_echo.decode("utf-8"))
command = "/" rpu_addr "/pMod 5,OUTPUT"
sio.write((command "\n").encode('ascii'))
sleep(0.2)
rpubus_echo = sio.readline().strip()
print("cmd echo: " rpubus_echo.decode("utf-8"))
rpubus_echo = sio.readline().strip()
print("json echo: " rpubus_echo.decode("utf-8"))
command = "/" rpu_addr "/pMod 6,OUTPUT"
sio.write((command "\n").encode('ascii'))
sleep(0.2)
rpubus_echo = sio.readline().strip()
print("cmd echo: " rpubus_echo.decode("utf-8"))
rpubus_echo = sio.readline().strip()
print("json echo: " rpubus_echo.decode("utf-8"))
command = "/" rpu_addr "/pMod 9,OUTPUT"
sio.write((command "\n").encode('ascii'))
sleep(0.2)
rpubus_echo = sio.readline().strip()
print("cmd echo: " rpubus_echo.decode("utf-8"))
rpubus_echo = sio.readline().strip()
print("json echo: " rpubus_echo.decode("utf-8"))
command = "/" rpu_addr "/pMod 10,OUTPUT"
sio.write((command "\n").encode('ascii'))
sleep(0.2)
rpubus_echo = sio.readline().strip()
print("cmd echo: " rpubus_echo.decode("utf-8"))
rpubus_echo = sio.readline().strip()
print("json echo: " rpubus_echo.decode("utf-8"))
command = "/" rpu_addr "/pMod 11,OUTPUT"
sio.write((command "\n").encode('ascii'))
sleep(0.2)
rpubus_echo = sio.readline().strip()
print("cmd echo: " rpubus_echo.decode("utf-8"))
rpubus_echo = sio.readline().strip()
print("json echo: " rpubus_echo.decode("utf-8"))

# Turn on each LED for a few seconds
command = "/" rpu_addr "/dWrt 3,HIGH"
sio.write((command "\n").encode('ascii'))
sleep(0.2)
rpubus_echo = sio.readline().strip()
print("cmd echo: " rpubus_echo.decode("utf-8"))
rpubus_echo = sio.readline().strip()
print("json echo: " rpubus_echo.decode("utf-8"))
sleep(5)
command = "/" rpu_addr "/dWrt 3,LOW"
sio.write((command "\n").encode('ascii'))
sleep(0.2)
rpubus_echo = sio.readline().strip()
print("cmd echo: " rpubus_echo.decode("utf-8"))
rpubus_echo = sio.readline().strip()
print("json echo: " rpubus_echo.decode("utf-8"))


command = "/" rpu_addr "/dWrt 5,HIGH"
sio.write((command "\n").encode('ascii'))
sleep(0.2)
rpubus_echo = sio.readline().strip()
print("cmd echo: " rpubus_echo.decode("utf-8"))
rpubus_echo = sio.readline().strip()
print("json echo: " rpubus_echo.decode("utf-8"))
sleep(5)
command = "/" rpu_addr "/dWrt 5,LOW"
sio.write((command "\n").encode('ascii'))
sleep(0.2)
rpubus_echo = sio.readline().strip()
print("cmd echo: " rpubus_echo.decode("utf-8"))
rpubus_echo = sio.readline().strip()
print("json echo: " rpubus_echo.decode("utf-8"))

command = "/" rpu_addr "/dWrt 6,HIGH"
sio.write((command "\n").encode('ascii'))
sleep(0.2)
rpubus_echo = sio.readline().strip()
print("cmd echo: " rpubus_echo.decode("utf-8"))
rpubus_echo = sio.readline().strip()
print("json echo: " rpubus_echo.decode("utf-8"))
sleep(5)
command = "/" rpu_addr "/dWrt 6,LOW"
sio.write((command "\n").encode('ascii'))
sleep(0.2)
rpubus_echo = sio.readline().strip()
print("cmd echo: " rpubus_echo.decode("utf-8"))
rpubus_echo = sio.readline().strip()
print("json echo: " rpubus_echo.decode("utf-8"))

command = "/" rpu_addr "/dWrt 9,HIGH"
sio.write((command "\n").encode('ascii'))
sleep(0.2)
rpubus_echo = sio.readline().strip()
print("cmd echo: " rpubus_echo.decode("utf-8"))
rpubus_echo = sio.readline().strip()
print("json echo: " rpubus_echo.decode("utf-8"))
sleep(5)
command = "/" rpu_addr "/dWrt 9,LOW"
sio.write((command "\n").encode('ascii'))
sleep(0.2)
rpubus_echo = sio.readline().strip()
print("cmd echo: " rpubus_echo.decode("utf-8"))
rpubus_echo = sio.readline().strip()
print("json echo: " rpubus_echo.decode("utf-8"))

command = "/" rpu_addr "/dWrt 10,HIGH"
sio.write((command "\n").encode('ascii'))
sleep(0.2)
rpubus_echo = sio.readline().strip()
print("cmd echo: " rpubus_echo.decode("utf-8"))
rpubus_echo = sio.readline().strip()
print("json echo: " rpubus_echo.decode("utf-8"))
sleep(5)
command = "/" rpu_addr "/dWrt 10,LOW"
sio.write((command "\n").encode('ascii'))
sleep(0.2)
rpubus_echo = sio.readline().strip()
print("cmd echo: " rpubus_echo.decode("utf-8"))
rpubus_echo = sio.readline().strip()
print("json echo: " rpubus_echo.decode("utf-8"))

command = "/" rpu_addr "/dWrt 11,HIGH"
sio.write((command "\n").encode('ascii'))
sleep(0.2)
rpubus_echo = sio.readline().strip()
print("cmd echo: " rpubus_echo.decode("utf-8"))
rpubus_echo = sio.readline().strip()
print("json echo: " rpubus_echo.decode("utf-8"))
sleep(5)
command = "/" rpu_addr "/dWrt 11,LOW"
sio.write((command "\n").encode('ascii'))
sleep(0.2)
rpubus_echo = sio.readline().strip()
print("cmd echo: " rpubus_echo.decode("utf-8"))
rpubus_echo = sio.readline().strip()
print("json echo: " rpubus_echo.decode("utf-8"))
Loading

0 comments on commit 56493e5

Please sign in to comment.