Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Saving settings to EEPROM and other IMO sensible changes
  • Loading branch information
loredan committed Sep 25, 2019
1 parent b35e116 commit 6b9ea8e
Show file tree
Hide file tree
Showing 9 changed files with 430 additions and 249 deletions.
11 changes: 11 additions & 0 deletions firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/platformio.ini
Expand Up @@ -12,4 +12,15 @@
platform = atmelavr
board = ATmega88P
framework = arduino
upload_protocol = stk500v1
; each flag in a new line
upload_flags =
-P$UPLOAD_PORT
-b$UPLOAD_SPEED

; edit these lines
upload_port = COM4
upload_speed = 19200

board_build.f_cpu = 1000000L

Expand Up @@ -34,7 +34,7 @@
To change trigger active duration: TRG_D [integer for milliseconds]
To change gain factor: GAIN_F [integer for gain state - see note*]
To change ADC hysteresis value: HYST [integer]
To change sensor input pullup vRef low threshold: VADJ [float value]
To change sensor input pullup vRef low threshold: VFOL [float value]
To change comparator trigger high threshold: VCOMP [float value]
Expand All @@ -43,7 +43,7 @@ These commands should be wrapped in this format:
Examples:
<GAIN_F, 3> <~ set gain factor to index 3 (6x)
<VADJ, 2350> <~ set the vref floor to 2.35V
<VFOL, 2350> <~ set the vref floor to 2.35V
*Note for Gain Factor:
The gain STATE is representative of these values:
Expand All @@ -54,14 +54,7 @@ The gain STATE is representative of these values:
4 = 11x
*/

/*------------------------------------------------------------*/

// Debug output toggle. Uncomment to enable
#define DEBUG true

/* Debug output verbose mode will continuously output sensor readings
rather than waiting for user input */
//#define VERBOSE true
#include <Arduino.h>

// Headers, variables, and functions
#include "LightChrono.h"
Expand All @@ -74,68 +67,82 @@ The gain STATE is representative of these values:
// i2c input toggle. Uncomment to enable
//#define I2C_INPUT true

void setup() {
pinMode(TRG_OUT, OUTPUT); // declare the Trigger as as OUTPUT
void setup()
{
pinMode(TRG_OUT, OUTPUT); // declare the Trigger as as OUTPUT
pinMode(ERR_LED, OUTPUT);
pinMode(Z_TRG, INPUT_PULLUP); // declare z-sense input with pullup
pinMode(Z_TRG, INPUT_PULLUP); // declare z-sense input with pullup
pinMode(V_FOLLOW_PIN, INPUT);
pinMode(VCOMP_SENSE_PIN, INPUT);
pinMode(GADJ_R0, INPUT); // declare input to set high impedance
pinMode(GADJ_R1, INPUT); // declare input to set high impedance
pinMode(GADJ_R2, INPUT); // declare input to set high impedance
pinMode(GADJ_R3, INPUT); // declare input to set high impedance
pinMode(GADJ_R0, INPUT); // declare input to set high impedance
pinMode(GADJ_R1, INPUT); // declare input to set high impedance
pinMode(GADJ_R2, INPUT); // declare input to set high impedance
pinMode(GADJ_R3, INPUT); // declare input to set high impedance
Serial.begin(9600);

attachInterrupt(digitalPinToInterrupt(Z_TRG), pulse, FALLING);

Serial.println("Initializing Pyr0-Piezo Sensor...");
}

/*------------------------------------------------*/
restoreConfig();

void loop() {
if (mainLoop.hasPassed(LOOP_DUR)) {
adjustGain();
}

void loop()
{
if (mainLoop.hasPassed(LOOP_DUR))
{
mainLoop.restart();
// Blink LED's on init
if (BlinkCount > 0) {
BlinkState = !BlinkState;
digitalWrite(ERR_LED, BlinkState);
digitalWrite(TRG_OUT, BlinkState);
--BlinkCount;
}

// Get Serial Input
serialInput();

// Set any new parameters from serial input
updateParams();

// Set the amplification gain factor
adjustGain();
if (serialIncoming)
{
updateParams();
}

// Check voltage of first and second stages and compare against thresholds
adjustVin();
readVin();
VComp = analogRead(VCOMP_SENSE_PIN);
VAdj = analogRead(V_FOLLOW_PIN);
VFol = analogRead(V_FOLLOW_PIN);

// Voltage Follower adjustment
if (VLast > Hyst || VLast < -Hyst) {
VLast = VOld - Vin;
if (VLast > Hyst || VLast < -Hyst)
{
// Voltage Follower adjustment
adjustFollow();
}

// Voltage Comparator adjustment
if (VLast > Hyst || VLast < -Hyst) {
// Voltage Comparator adjustment
adjustComp();
// Alert the user that auto-calibration is ongoing
ERR_STATE = 1;
}
else
{
ERR_STATE = 0;
}

// Alert the user that auto-calibration is ongoing
calibrateAlert();

// Blink LED's on init
if (BlinkCount > 0)
{
BlinkState = !BlinkState;
digitalWrite(ERR_LED, BlinkState);
digitalWrite(TRG_OUT, BlinkState);
--BlinkCount;
}
else
// Check for error state
checkError();
{
checkError();
}

// Reply with status
serialReply();
// Print state if debug is on
if (Debug > 0)
{
serialPrintState();
}

// Sets trigger output state to false after completing loop
//digitalWrite(TRG_OUT, HIGH);
Expand Down
99 changes: 99 additions & 0 deletions firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_config.cpp
@@ -0,0 +1,99 @@
#include "pP_config.h"
#include <EEPROM.h>

int GAIN_FACTOR = GAIN_FACTOR_DEFAULT; // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x
int followerThrs = FOLLOWER_THRESHOLD_DEFAULT;
int compThrs = COMP_THRESHOLD_DEFAULT;
int LOOP_DUR = LOOP_DUR_DEFAULT; // duration of time between ADC checks and other loop functions
int TRG_DUR = TRG_DUR_DEFAULT; // duration of the Z-axis pulse sent, in ms
int Hyst = HYST_DEFAULT; // Hysteresis value for ADC measurements
int Debug = 0;
long voltMeterConstant = 1125300L; // For fine tuning input voltage sense
// byte pP_i2c_address = 0xa0; // I2C Bus Address

void resetEEPROM()
{
resetConfig();
EEPROM.put(GAIN_FACTOR_ADDRESS, GAIN_FACTOR);
EEPROM.put(FOLLOWER_THRESHOLD_ADDRESS, followerThrs);
EEPROM.put(COMP_THRESHOLD_ADDRESS, compThrs);
EEPROM.put(LOOP_DUR_ADDRESS, LOOP_DUR);
EEPROM.put(TRG_DUR_ADDRESS, TRG_DUR);
EEPROM.put(HYST_ADDRESS, Hyst);
}

// Restore config from EEPROM, otherwise reset config and write to EEPROM
void restoreConfig()
{
int temp;

EEPROM.get(GAIN_FACTOR_ADDRESS, temp);
if (temp < 0 || temp > 4)
{
resetEEPROM();
}
else
{
GAIN_FACTOR = temp;
}

EEPROM.get(FOLLOWER_THRESHOLD_ADDRESS, temp);
if (temp < 0 || temp > 5000)
{
resetEEPROM();
}
else
{
followerThrs = temp;
}

EEPROM.get(COMP_THRESHOLD_ADDRESS, temp);
if (temp < 0 || temp > 5000)
{
resetEEPROM();
}
else
{
compThrs = temp;
}

EEPROM.get(LOOP_DUR_ADDRESS, temp);
if (temp < 0 && temp > 1000)
{
resetEEPROM();
}
else
{
LOOP_DUR = temp;
}

EEPROM.get(TRG_DUR_ADDRESS, temp);
if (temp < 0 || temp > 1000)
{
resetEEPROM();
}
else
{
TRG_DUR = temp;
}

EEPROM.get(HYST_ADDRESS, temp);
if (temp < 0 || temp > 1000)
{
resetEEPROM();
}
else
{
Hyst = temp;
}
}

void resetConfig()
{
GAIN_FACTOR = GAIN_FACTOR_DEFAULT;
followerThrs = FOLLOWER_THRESHOLD_DEFAULT;
compThrs = COMP_THRESHOLD_DEFAULT;
LOOP_DUR = LOOP_DUR_DEFAULT;
TRG_DUR = TRG_DUR_DEFAULT;
Hyst = HYST_DEFAULT;
}
54 changes: 38 additions & 16 deletions firmware/AVR-Source/Pyr0_Piezo_Sensor_v2/src/pP_config.h
@@ -1,41 +1,63 @@
#ifndef PP_CONFIG_H
#define PP_CONFIG_H

// Configurable settings:

#define GAIN_FACTOR_DEFAULT 2
#define GAIN_FACTOR_ADDRESS 0
#if !(defined(GAIN_FACTOR))
int GAIN_FACTOR = 2; // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x
extern int GAIN_FACTOR; // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x
#endif

#ifndef senseThrs
#define senseThrs 1450
#define FOLLOWER_THRESHOLD_DEFAULT 1450
#define FOLLOWER_THRESHOLD_ADDRESS 4
#if !(defined(followerThrs))
extern int followerThrs;
#endif

#ifndef compThrs
#define compThrs 2850
#define COMP_THRESHOLD_DEFAULT 2850
#define COMP_THRESHOLD_ADDRESS 8
#if !(defined(compThrs))
extern int compThrs;
#endif

#ifndef InitCount
#define InitCount 6 // Number of times to blink the LED on start
#define InitCount 6 // Number of times to blink the LED on start
#endif

#define LOOP_DUR_DEFAULT 50
#define LOOP_DUR_ADDRESS 12
#if !(defined(LOOP_DUR))
int LOOP_DUR = 50; // duration of time between ADC checks and other loop functions
extern int LOOP_DUR; // duration of time between ADC checks and other loop functions
#endif

#define TRG_DUR_DEFAULT 20
#define TRG_DUR_ADDRESS 16
#if !(defined(TRG_DUR))
int TRG_DUR = 20; // duration of the Z-axis pulse sent, in ms
extern int TRG_DUR; // duration of the Z-axis pulse sent, in ms
#endif

#define HYST_DEFAULT 20
#define HYST_ADDRESS 20
#if !(defined(Hyst))
int Hyst = 20; // Hysteresis value for ADC measurements
extern int Hyst; // Hysteresis value for ADC measurements
#endif

#if !(defined(voldMeterConstant))
long voltMeterConstant = 1125300L; // For fine tuning input voltage sense
#if !(defined(Debug))
extern int Debug;
#endif

#ifdef I2C_INPUT
#if !(defined(pP_i2c_address))
byte pP_i2c_address = 0xa0; // I2C Bus Address
#endif
#if !(defined(voldMeterConstant))
extern long voltMeterConstant; // For fine tuning input voltage sense
#endif


// #ifdef I2C_INPUT
// #if !(defined(pP_i2c_address))
// extern byte pP_i2c_address = 0xa0; // I2C Bus Address
// #endif
// #endif

void restoreConfig();
void resetConfig();

#endif

0 comments on commit 6b9ea8e

Please sign in to comment.