Skip to content

Commit

Permalink
Switched to task scheduler library for faster interrupt processing
Browse files Browse the repository at this point in the history
  • Loading branch information
pyr0ball committed Jul 6, 2019
1 parent 2b5d7e5 commit 0b1dbeb
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 286 deletions.
67 changes: 67 additions & 0 deletions firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/LightChrono.cpp
@@ -0,0 +1,67 @@
/*
* Lightweight chronometer class.
* Simple chronometer/stopwatch class that counts the time passed since started.
*
* (c) 2015 Sofian Audry :: info(@)sofianaudry(.)com
* (c) 2015 Thomas O Fredericks :: tof(@)t-o-f(.)info
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "LightChrono.h"

LightChrono::LightChrono()
{
restart();
}

void LightChrono::start() { restart(); }

void LightChrono::restart()
{
_startTime = millis();
}

LightChrono::chrono_t LightChrono::elapsed() const {
return (millis() - _startTime);
}

bool LightChrono::hasPassed(LightChrono::chrono_t timeout) const
{
return (elapsed() >= timeout);
}

bool LightChrono::hasPassed(LightChrono::chrono_t timeout, bool restartIfPassed) {
if (hasPassed(timeout)) {
if (restartIfPassed)
restart();
return true;
}
else {
return false;
}
}


72 changes: 72 additions & 0 deletions firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/LightChrono.h
@@ -0,0 +1,72 @@
/*
* Lightweight chronometer class.
* Simple chronometer/stopwatch class that counts the time passed since started.
*
* (c) 2015 Sofian Audry :: info(@)sofianaudry(.)com
* (c) 2015 Thomas O Fredericks :: tof(@)t-o-f(.)info
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef LIGHT_CHRONO_H_
#define LIGHT_CHRONO_H_

/*
* Example code:
*
* LightChrono myLightChrono; // chronometer automatically starts at creation
* // ...
* myLightChrono.restart(); // you can start (restart) it later
* while (!myLightChrono.hasPassed(2000)) // a 2000 ms loop
* Serial.println(myLightChrono.elapsed()); // current time
* // do something
* // ...
*/
class LightChrono
{
public:
#if defined(ARDUINO_ARC32_TOOLS)
typedef uint64_t chrono_t;
#else
typedef unsigned long chrono_t;
#endif

private:
chrono_t _startTime; // keeps track of start time (in milliseconds)

public:
/// Constructor.
LightChrono();

// Starts/restarts the chronometer.
void start();
void restart();

/// Returns the elapsed time since start (in milliseconds).
chrono_t elapsed() const;

/// Returns true iff elapsed time has passed given timeout.
bool hasPassed(chrono_t timeout) const;
bool hasPassed(chrono_t timeout, bool restartIfPassed);
};

#endif


Expand Up @@ -5,7 +5,7 @@
The sense pin is tied to an interrupt, which is pulled high by internal pullup resistor.
When the piezo touches the bed, the amplification circuit will draw the interrupt pin low
and the atmega will output a pulse based on the programmed trigger duration
* PD2 INT0 (Piezo In 'D2')
* D7 PCINT23 (Trigger OUT 'D7')
* PC0 ADC0 (Voltage Reference Check 'A0')
Expand Down Expand Up @@ -38,7 +38,7 @@ To change sensor input pullup vRef low threshold: VADJ [float value]
To change comparator trigger high threshold: VCOMP [float value]
These commands should be wrapped in this format:
These commands should be wrapped in this format:
<CMD, INT>
Examples:
Expand Down Expand Up @@ -73,6 +73,7 @@ int Hyst = 20; // Hysteresis value for ADC measurements
//#define VERBOSE true

// Headers, variables, and functions
#include "LightChrono.h"
#include "pP_pins.h"
#include "pP_volatile.h"
#include "pP_function.h"
Expand Down Expand Up @@ -104,51 +105,51 @@ void setup() {
/*------------------------------------------------*/

void loop() {

// Blink LED's on init
if (BlinkCount > 0) {
BlinkState = !BlinkState;
digitalWrite(ERR_LED, BlinkState);
digitalWrite(TRG_OUT, BlinkState);
delay(LOOP_DUR);
--BlinkCount;
}

// Get Serial Input
serialInput();

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

// Set the amplification gain factor
adjustGain();

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

// Voltage Follower adjustment
if (VLast > Hyst || VLast < -Hyst) {
adjustFollow();
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();

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

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

// Voltage Comparator adjustment
if (VLast > Hyst || VLast < -Hyst) {
adjustComp();
}

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

// Check for error state
checkError();

// Reply with status
serialReply();

// Sets trigger output state to false after completing loop
digitalWrite(TRG_OUT, HIGH);
sensorHReading = 0;
}

// Voltage Comparator adjustment
if (VLast > Hyst || VLast < -Hyst) {
adjustComp();
}

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

// Check for error state
checkError();

// Reply with status
serialReply();

// Sets trigger output state to false after completing loop
delay(LOOP_DUR);
digitalWrite(TRG_OUT, HIGH);
sensorHReading = 0;
}
21 changes: 8 additions & 13 deletions firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/pP_function.h
Expand Up @@ -34,23 +34,23 @@ long readVcc() {
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
#endif

delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA,ADSC)); // measuring

uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both

long result = (high<<8) | low;

result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
return result; // Vcc in millivolts
}

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

void adjustVin() {
VOld = Vin;
Vin = readVcc(), DEC;
Expand Down Expand Up @@ -81,16 +81,16 @@ void adjustComp() {

analogWrite(VCOMP_PWM, ADJ_COMP);
}

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

void calibrateAlert() {
VLast = VOld - Vin;
if (VLast > Hyst || VLast < -Hyst ) {
ERR_STATE = 1;
}
}

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

void adjustGain() {
Expand Down Expand Up @@ -129,7 +129,7 @@ void adjustGain() {
ERR_STATE = 0;
}
}

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

void checkError () {
Expand All @@ -142,8 +142,3 @@ void checkError () {
digitalWrite(ERR_LED, BlinkState);
}
}

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


// #endif

0 comments on commit 0b1dbeb

Please sign in to comment.