Skip to content

Commit

Permalink
fix #26 DC measurements (#28)
Browse files Browse the repository at this point in the history
* fix #26 midPoint data type
* fix incrMidpoint
  • Loading branch information
RobTillaart authored Nov 21, 2022
1 parent e9d55d0 commit 891cd90
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 53 deletions.
3 changes: 2 additions & 1 deletion .arduino-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ compile:
- esp32
- esp8266
# - mega2560
- rpipico
- rpipico

47 changes: 4 additions & 43 deletions ACS712.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,9 @@
//
// FILE: ACS712.cpp
// AUTHOR: Rob Tillaart, Pete Thompson
// VERSION: 0.3.1
// VERSION: 0.3.2
// DATE: 2020-08-02
// PURPOSE: ACS712 library - current measurement
//
// HISTORY:
// 0.1.0 2020-03-17 initial version
// 0.1.1 2020-03-18 first release version
// 0.1.2 2020-03-21 automatic form factor test
// 0.1.3 2020-05-27 fix library.json
// 0.1.4 2020-08-02 Allow for faster processors
//
// 0.2.0 2020-08-02 Add autoMidPoint
// 0.2.1 2020-12-06 Add Arduino-CI + readme + unit test + refactor
// 0.2.2 2021-06-23 support for more frequencies.
// 0.2.3 2021-10-15 changed frequencies to float, for optimal tuning.
// updated build CI, readme.md
// 0.2.4 2021-11-22 add experimental detectFrequency()
// 0.2.5 2021-12-03 add timeout to detectFrequency()
// 0.2.6 2021-12-09 update readme.md + license
// 0.2.7 2022-08-10 change mVperAmp to float
// add ACS712_FF_SAWTOOTH
// update readme.md + unit test + minor edits
// 0.2.8 2022-08-19 prepare for 0.3.0
// Fix #21 FormFactor
// add mA_AC_sampling() as method to determine
// current when FormFactor is unknown.
// added float _AmperePerStep cached value.
// added getAmperePerStep();
// moved several functions to .cpp
// improve documentation
//
// 0.3.0 2022-09-01 return midPoint value in MP functions.
// float return type for mA() functions
// add float mA_peak2peak(freq, cycles)
// add debug getMinimum(), getmaximum();
// update Readme.md
// 0.3.1 2022-09-xx add float mVNoiseLevel(frequency, cycles)
// add void suppressNoise(bool flag)
// experimental suppression by averaging two samples.
// update readme.md
// improve midPoint functions
// add resetMidPoint()
// add RP2040 pico in build-ci


#include "ACS712.h"
Expand Down Expand Up @@ -184,6 +144,7 @@ float ACS712::mA_AC_sampling(float frequency, uint16_t cycles)
}
float current = value - _midPoint;
sumSquared += (current * current);
// not adding noise squared might be more correct for small currents.
// if (abs(current) > noiseLevel)
// {
// sumSquared += (current * current);
Expand Down Expand Up @@ -223,7 +184,7 @@ float ACS712::mA_DC(uint16_t cycles)
// CALIBRATION MIDPOINT
uint16_t ACS712::setMidPoint(uint16_t midPoint)
{
if (midPoint <= _maxADC) _midPoint = midPoint;
if (midPoint <= _maxADC) _midPoint = (int) midPoint;
return _midPoint;
};

Expand All @@ -236,7 +197,7 @@ uint16_t ACS712::getMidPoint()

uint16_t ACS712::incMidPoint()
{
if (_midPoint < _maxADC) _midPoint += 1;
if (_midPoint < (int)(_maxADC)) _midPoint += 1;
return _midPoint;
};

Expand Down
6 changes: 3 additions & 3 deletions ACS712.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: ACS712.h
// AUTHOR: Rob Tillaart, Pete Thompson
// VERSION: 0.3.1
// VERSION: 0.3.2
// DATE: 2020-08-02
// PURPOSE: ACS712 library - current measurement
//
Expand All @@ -12,7 +12,7 @@

#include "Arduino.h"

#define ACS712_LIB_VERSION (F("0.3.1"))
#define ACS712_LIB_VERSION (F("0.3.2"))


// ACS712_FF_SINUS == 1.0/sqrt(2) == 0.5 * sqrt(2)
Expand Down Expand Up @@ -111,7 +111,7 @@ class ACS712
float _formFactor; // peak2peak -> RMS
float _mVperAmpere;
float _mAPerStep;
uint16_t _midPoint;
int _midPoint;
uint8_t _noisemV;
float _microsAdjust = 1.0; // 0.9986
bool _suppresNoise = false;
Expand Down
88 changes: 88 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Change Log AD520X

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.3.2] - 2022-11-18
- fix #26 revert data type \_midPoint to int
- Add CHANGELOG.md

## [0.3.1 2022-09-xx
- add float mVNoiseLevel(frequency, cycles)
- add void suppressNoise(bool flag) - experimental suppression by averaging two samples.
- update readme.md
- improve midPoint functions
- add resetMidPoint()
- add RP2040 pico in build-ci

## [0.3.0] - 2022-09-01
- return midPoint value in MP functions.
- float return type for mA() functions
- add float mA_peak2peak(freq, cycles)
- add debug getMinimum(), getmaximum();
- update Readme.md

----

## [0.2.8] - 2022-08-19 prepare for 0.3.0
- Fix #21 FormFactor
- add mA_AC_sampling() as method to determine
- current when FormFactor is unknown.
- added float _AmperePerStep cached value.
- added getAmperePerStep();
- moved several functions to .cpp
- improve documentation

## [0.2.7] - 2022-08-10
- change mVperAmp to float
- add ACS712_FF_SAWTOOTH
- update readme.md + unit test + minor edits

## [0.2.6] - 2021-12-09
- update readme.md
- update license

## [0.2.5] - 2021-12-03
- add timeout to detectFrequency()

## [0.2.4] - 2021-11-22
- add experimental detectFrequency()

## [0.2.3] - 2021-10-15
- change frequencies to float, for optimal tuning.
- update build CI
- update readme.md

## [0.2.2] - 2021-06-23
- support for more frequencies

## [0.2.1] - 2020-12-06
- Add Arduino-CI + unit test
- update readme
- refactor

## [0.2.0] - 2020-08-02
- Add autoMidPoint()

----

## [0.1.4] - 2020-08-02
- Allow for faster processors

## [0.1.3] - 2020-05-27
- fix library.json

## [0.1.2] - 2020-03-21
- automatic form factor test

## [0.1.1] - 2020-03-18
- first release version

## [0.1.0] - 2020-03-17
- initial version



8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ The midpoint is the (raw) zero-reference for all current measurements.
It is defined in steps of the ADC and is typical around half the **maxADC** value defined
in the constructor. So for a 10 bit ADC a number between 500..525 is most likely.

Since 0.3.0 all midpoint functions return actual midPoint.
Since 0.3.0 all midpoint functions return the actual midPoint.

- **uint16_t setMidPoint(uint16_t midPoint)** sets midpoint for the ADC conversion.
Parameter must be between 0 and maxADC, otherwise midpoint is not changed.
Parameter must be between 0 and maxADC/2, otherwise midpoint is not changed.
- **uint16_t autoMidPoint(float frequency = 50, uint16_t cycles = 1)** Auto midPoint,
assuming zero DC current or any AC current.
The function takes the average of many measurements during one or more full cycles.
Expand All @@ -176,7 +176,7 @@ One can use the two debug functions.
and take the average of these two values. In code:

```cpp
uint16_t midpnt = ACS.setMidPoint((ACS.getMinimum(20) + ACS.getMaximum(20)) / 2);
uint16_t midpoint = ACS.setMidPoint(ACS.getMinimum(20)/2 + ACS.getMaximum(20)/ 2);
```
See - ACS712_20_AC_midPoint_compare.ino

Expand Down Expand Up @@ -343,7 +343,7 @@ The examples show the basic working of the functions.
#### Should - 0.3.x

- investigate noise suppression #21 (0.3.1 and later)
- external history file = changelog.md
- add external history file = changelog.md


#### Could
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/ACS712.git"
},
"version": "0.3.1",
"version": "0.3.2",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ACS712
version=0.3.1
version=0.3.2
author=Rob Tillaart <[email protected]>, Pete Thompson <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=ACS712 library for Arduino.
Expand Down
4 changes: 4 additions & 0 deletions test/unit_test_001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ unittest(test_midPoint)
amp = ACS.getMidPoint();
assertEqual(1000, amp);

ACS.decMidPoint();
amp = ACS.getMidPoint();
assertEqual(999, amp);

ACS.resetMidPoint();
amp = ACS.getMidPoint();
assertEqual(511, amp);
Expand Down

0 comments on commit 891cd90

Please sign in to comment.