Merge pull request #37 from pyr0ball/loredans-additions

Loredans additional additions
This commit is contained in:
Alan Weinstock 2019-10-02 18:54:50 -07:00 committed by GitHub
commit 75d86cb836
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 76 additions and 64 deletions

View file

@ -22,5 +22,23 @@ upload_flags =
upload_port = COM4 upload_port = COM4
upload_speed = 19200 upload_speed = 19200
board_build.f_cpu = 1000000L board_build.f_cpu = 8000000L
board_fuses.lfuse = "0xE2"
board_fuses.hfuse = "0xDF"
board_fuses.efuse = "0xF9"
[env:ATmega328PB]
platform = atmelavr
board = ATmega328PB
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 = 8000000L

View file

@ -82,9 +82,6 @@ update the voltMeterConstant variable in pP_config.h with the correct value
------------------------------------------------------------*/ ------------------------------------------------------------*/
// Debug output toggle. Uncomment to enable
#define DEBUG true
/* Debug output verbose mode will continuously output sensor readings /* Debug output verbose mode will continuously output sensor readings
rather than waiting for user input */ rather than waiting for user input */
//#define VERBOSE true //#define VERBOSE true
@ -115,20 +112,6 @@ void setup() {
Serial.begin(9600); Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(Z_TRG), pulse, FALLING); attachInterrupt(digitalPinToInterrupt(Z_TRG), pulse, FALLING);
// Atmega's Secret Voltmeter setup:
// set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
delay(2); // Wait for vref to settle
Serial.println("Initializing Pyr0-Piezo Sensor..."); Serial.println("Initializing Pyr0-Piezo Sensor...");
@ -166,11 +149,10 @@ void loop() {
VComp = analogRead(VCOMP_SENSE_PIN); VComp = analogRead(VCOMP_SENSE_PIN);
VFol = analogRead(V_FOLLOW_PIN); VFol = analogRead(V_FOLLOW_PIN);
// Voltage Follower adjustment
VLast = VOld - Vin; VLast = VOld - Vin;
if (VLast > Hyst || VLast < -Hyst) { if (VLast > Hyst || VLast < -Hyst) {
// Voltage Follower adjustment
adjustFollow(); adjustFollow();
// Voltage Comparator adjustment // Voltage Comparator adjustment
adjustComp(); adjustComp();
// Alert the user that auto-calibration is ongoing // Alert the user that auto-calibration is ongoing

View file

@ -8,6 +8,8 @@ int LOOP_DUR = LOOP_DUR_DEFAULT; // duration of time between ADC checks and othe
int TRG_DUR = TRG_DUR_DEFAULT; // duration of the Z-axis pulse sent, in ms 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 Hyst = HYST_DEFAULT; // Hysteresis value for ADC measurements
int Debug = 0; int Debug = 0;
long voltMeterConstant = VM_CONST_DEFAULT;
uint8_t pP_i2c_address = 0xa0;
void resetEEPROM() { void resetEEPROM() {
resetConfig(); resetConfig();
@ -65,6 +67,14 @@ void restoreConfig() {
} else { } else {
Hyst = temp; Hyst = temp;
} }
long longTemp;
EEPROM.get(VM_CONST_DEFAULT, longTemp);
if (longTemp < 1000000L || longTemp > 1200000L) {
resetEEPROM();
} else {
voltMeterConstant = longTemp;
}
} }
void resetConfig() { void resetConfig() {
@ -74,4 +84,5 @@ void resetConfig() {
LOOP_DUR = LOOP_DUR_DEFAULT; LOOP_DUR = LOOP_DUR_DEFAULT;
TRG_DUR = TRG_DUR_DEFAULT; TRG_DUR = TRG_DUR_DEFAULT;
Hyst = HYST_DEFAULT; Hyst = HYST_DEFAULT;
voltMeterConstant = VM_CONST_DEFAULT;
} }

View file

@ -48,14 +48,15 @@
#endif #endif
#define VM_CONST_ADDRESS 28 #define VM_CONST_ADDRESS 28
#define VM_CONST_DEFAULT 1125300L
#if !(defined(voltMeterConstant)) #if !(defined(voltMeterConstant))
extern long voltMeterConstant = 1125300L; // For fine tuning input voltage sense extern long voltMeterConstant; // For fine tuning input voltage sense
#endif #endif
#ifdef I2C_INPUT #ifdef I2C_INPUT
#define I2C_SLAVE_ADDRESS 24 #define I2C_SLAVE_ADDRESS 24
#if !(defined(pP_i2c_address)) #if !(defined(pP_i2c_address))
extern byte pP_i2c_address = 0xa0; // I2C Bus Address extern uint8_t pP_i2c_address; // I2C Bus Address
#endif #endif
#endif #endif

View file

@ -17,9 +17,6 @@ void digitalWriteFast(uint8_t pin, uint8_t x) {
void pulse() { void pulse() {
digitalWriteFast(TRG_OUT, LOW); digitalWriteFast(TRG_OUT, LOW);
sensorHReading = 1; sensorHReading = 1;
#ifdef DEBUG
Serial.println("Trig!");
#endif
delay(TRG_DUR); delay(TRG_DUR);
digitalWriteFast(TRG_OUT, HIGH); digitalWriteFast(TRG_OUT, HIGH);
} }
@ -28,6 +25,21 @@ void pulse() {
long readVcc() { long readVcc() {
// Read 1.1V reference against AVcc // Read 1.1V reference against AVcc
// Atmega's Secret Voltmeter setup:
// set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
delay(2); // Wait for vref to settle
ADCSRA |= _BV(ADSC); // Start conversion ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA,ADSC)); // measuring while (bit_is_set(ADCSRA,ADSC)); // measuring

View file

@ -8,7 +8,7 @@ void parseData() {
strcpy(serialMessageIn, strtokIndx); // copy it to serialMessageIn strcpy(serialMessageIn, strtokIndx); // copy it to serialMessageIn
strtokIndx = strtok(NULL, " "); // this continues where the previous call left off strtokIndx = strtok(NULL, " "); // this continues where the previous call left off
serialInt = atoi(strtokIndx); // convert this part to an integer serialLong = atol(strtokIndx); // convert this part to an integer
} }
/*------------------------------------------------*/ /*------------------------------------------------*/
@ -18,7 +18,7 @@ void identifyMarkers() {
char x = Serial.read(); char x = Serial.read();
// char y = Wire.read(); // char y = Wire.read();
if (x == endMarker) { if (x == '\n' || x == '\r') {
serialIncoming = true; serialIncoming = true;
inputBuffer[bytesRecvd] = 0; inputBuffer[bytesRecvd] = 0;
parseData(); parseData();
@ -26,8 +26,7 @@ void identifyMarkers() {
} else { } else {
inputBuffer[bytesRecvd] = x; inputBuffer[bytesRecvd] = x;
bytesRecvd++; bytesRecvd++;
if (bytesRecvd == buffSize) if (bytesRecvd == buffSize) {
{
bytesRecvd = buffSize - 1; bytesRecvd = buffSize - 1;
} }
} }
@ -59,9 +58,8 @@ void identifyMarkers() {
void updateGainFactor() void updateGainFactor()
{ {
if (serialInt >= 0) if (serialLong >= 0) {
{ GAIN_FACTOR = serialLong;
GAIN_FACTOR = serialInt;
adjustGain(); adjustGain();
EEPROM.put(GAIN_FACTOR_ADDRESS, GAIN_FACTOR); EEPROM.put(GAIN_FACTOR_ADDRESS, GAIN_FACTOR);
} }
@ -70,9 +68,8 @@ void updateGainFactor()
/*------------------------------------------------*/ /*------------------------------------------------*/
void updateVFol() { void updateVFol() {
if (serialInt >= 0) if (serialLong >= 0) {
{ followerThrs = serialLong;
followerThrs = serialInt;
adjustFollow(); adjustFollow();
EEPROM.put(FOLLOWER_THRESHOLD_ADDRESS, followerThrs); EEPROM.put(FOLLOWER_THRESHOLD_ADDRESS, followerThrs);
} }
@ -80,9 +77,8 @@ void updateVFol() {
/*------------------------------------------------*/ /*------------------------------------------------*/
void updateVComp() { void updateVComp() {
if (serialInt >= 0) if (serialLong >= 0) {
{ compThrs = serialLong;
compThrs = serialInt;
adjustComp(); adjustComp();
EEPROM.put(COMP_THRESHOLD_ADDRESS, compThrs); EEPROM.put(COMP_THRESHOLD_ADDRESS, compThrs);
} }
@ -90,38 +86,33 @@ void updateVComp() {
/*------------------------------------------------*/ /*------------------------------------------------*/
void updateLoopDuration() void updateLoopDuration() {
{ if (serialLong >= 0) {
if (serialInt >= 0) LOOP_DUR = serialLong;
{
LOOP_DUR = serialInt;
EEPROM.put(LOOP_DUR_ADDRESS, LOOP_DUR); EEPROM.put(LOOP_DUR_ADDRESS, LOOP_DUR);
} }
} }
/*------------------------------------------------*/ /*------------------------------------------------*/
void updateTrigDuration() { void updateTrigDuration() {
if (serialInt >= 0) if (serialLong >= 0) {
{ TRG_DUR = serialLong;
TRG_DUR = serialInt;
EEPROM.put(TRG_DUR_ADDRESS, TRG_DUR); EEPROM.put(TRG_DUR_ADDRESS, TRG_DUR);
} }
} }
/*------------------------------------------------*/ /*------------------------------------------------*/
void updateHysteresis() { void updateHysteresis() {
if (serialInt >= 0) if (serialLong >= 0) {
{ Hyst = serialLong;
Hyst = serialInt;
EEPROM.put(HYST_ADDRESS, Hyst); EEPROM.put(HYST_ADDRESS, Hyst);
} }
} }
/*------------------------------------------------*/ /*------------------------------------------------*/
void updateConstant() { void updateConstant() {
if (serialInt >= 0) if (serialLong >= 0) {
{ voltMeterConstant = (long) serialLong;
voltMeterConstant = (long) serialInt;
EEPROM.put(VM_CONST_ADDRESS, voltMeterConstant); EEPROM.put(VM_CONST_ADDRESS, voltMeterConstant);
} }
} }
@ -129,21 +120,19 @@ void updateConstant() {
/*------------------------------------------------*/ /*------------------------------------------------*/
void updateDebug() { void updateDebug() {
if (serialInt > 0) { if (serialLong > 0) {
Debug = 1; Debug = 1;
} else if (serialInt == 0) { } else if (serialLong == 0) {
Debug = 0; Debug = 0;
} }
} }
/*------------------------------------------------*/ /*------------------------------------------------*/
void serialPrintConfig() void serialPrintConfig() {
{
Serial.print("GAIN_F "); Serial.print("GAIN_F ");
Serial.print(GAIN_FACTOR); Serial.print(GAIN_FACTOR);
switch (GAIN_FACTOR) switch (GAIN_FACTOR) {
{
case 0: case 0:
Serial.println(" 3x"); Serial.println(" 3x");
break; break;
@ -183,8 +172,7 @@ void serialPrintConfig()
Serial.println(voltMeterConstant); Serial.println(voltMeterConstant);
} }
void serialPrintState() void serialPrintState() {
{
Serial.print("{"); Serial.print("{");
Serial.print("\"Vcc\":"); Serial.print("\"Vcc\":");
@ -192,11 +180,11 @@ void serialPrintState()
Serial.print(","); Serial.print(",");
Serial.print("\"VComp\":"); Serial.print("\"VComp\":");
Serial.print(VComp); Serial.print((long) VComp * Vin / 1023);
Serial.print(","); Serial.print(",");
Serial.print("\"VFol\":"); Serial.print("\"VFol\":");
Serial.print(VFol); Serial.print((long) VFol * Vin / 1023);
Serial.print(","); Serial.print(",");
Serial.print("\"Err\":"); Serial.print("\"Err\":");
@ -217,7 +205,7 @@ void updateParams() {
updateVComp(); updateVComp();
} }
else if (strcmp(serialMessageIn, "LOOP_D") == 0) { else if (strcmp(serialMessageIn, "LOOP_D") == 0) {
updateTrigDuration(); updateLoopDuration();
} }
else if (strcmp(serialMessageIn, "TRG_D") == 0) { else if (strcmp(serialMessageIn, "TRG_D") == 0) {
updateTrigDuration(); updateTrigDuration();

View file

@ -33,7 +33,7 @@ char inputBuffer[buffSize];
byte bytesRecvd = 0; byte bytesRecvd = 0;
bool serialIncoming = false; bool serialIncoming = false;
char serialMessageIn[buffSize] = {0}; char serialMessageIn[buffSize] = {0};
int serialInt = 0; long serialLong = 0;
//#define LOW 0 //#define LOW 0
//#define HIGH 1 //#define HIGH 1