Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions v3/firmware/ACS764.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "ACS764.h"
#include <Wire.h>

void ACS764::init(byte address)
{
write_reg(address, ADDR_AVG_POINTS, AVG_POINTS & 0xFF);
write_reg(address, ADDR_GAIN_RANGE, GAIN_RANGE & 0x03);
write_reg(address, ADDR_FAULT_LVL, FAULT_LVL & 0x0F);
}

void ACS764::write_reg(byte address, byte reg_add, byte data)
{
for (byte attempts = 0; attempts < 10; attempts++) {
Wire.beginTransmission(address);
Wire.write(reg_add);
Wire.write(0x00);
Wire.write(0x00);
Wire.write(data);
if (Wire.endTransmission() == 0)
break;
}
}

unsigned int ACS764::getCurrent(byte addr)
{
byte csb, lsb;
byte attempts;
byte timeout;

for (attempts = 0; attempts < 10; attempts++) {

/* request data from sensor */
Wire.beginTransmission(addr);
delay(5);
Wire.write(ADDR_DATA);
delay(5);

/* retry on error */
if (Wire.endTransmission(0) != 0)
continue;

delay(5);

/* read data from sensor */
if (Wire.requestFrom(addr, 3) != 3)
continue;

delay(5);

for (timeout = 0; timeout < 100 && Wire.available() < 3; timeout++) {
delay(5);
}

/* retry on error */
if (timeout >= 100)
continue;

Wire.read();
csb = Wire.read() & 0x01;
lsb = Wire.read();

/* retry on error */
if (Wire.endTransmission(1) != 0)
continue;

delay(5);

/* return milliamps from raw sensor data. */
if (((csb << 8) | lsb) < MIN_STEP)
return 0;
else
return ((csb << 8) | lsb) * MILLIAMPS_PER_STEP;
}
return 0xFFFF;
}
26 changes: 26 additions & 0 deletions v3/firmware/ACS764.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef ACS764_H
#define ACS764_H

#include <Arduino.h>

class ACS764 {
public:
static const byte AVG_POINTS = 0x10; // 32 points
static const byte GAIN_RANGE = 0x02; // 15.66 mA per LSB
static const byte FAULT_LVL = 0x00;

void init(byte address);
void write_reg(byte address, byte reg_add, byte data);
unsigned int getCurrent(byte addr);
private:
//communication register
static const byte ADDR_DATA = 0x00;
static const byte ADDR_AVG_POINTS = 0x02;
static const byte ADDR_GAIN_RANGE = 0x04;
static const byte ADDR_FAULT_LVL = 0x06;

// mA per LSB
static const unsigned int MILLIAMPS_PER_STEP = 15.66;
static const unsigned int MIN_STEP = 15;
};
#endif
6 changes: 3 additions & 3 deletions v3/firmware/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ void Device::updateFault()
if (current == 0xFFFF)
return;

if (current < 120) {
if (current < 235) { // 15 LSB * 15.66 mA/LSB = 234.90 mA
newCurrentLevel = CURRENT_LOW;
} else if (current < 300) {
} else if (current < 600) {
newCurrentLevel = CURRENT_NORMAL;
} else if (current < 850) {
} else if (current < 1500) {
newCurrentLevel = CURRENT_STRESSED;
} else {
newCurrentLevel = CURRENT_HIGH;
Expand Down
61 changes: 10 additions & 51 deletions v3/firmware/Wagman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "HTU21D.h"
#include "MCP342X.h"
#include "MCP79412RTC.h"
#include "ACS764.h"
#include "Wagman.h"
#include "Record.h"

Expand Down Expand Up @@ -30,6 +31,7 @@ static const byte BOOT_SELECTOR_PINS[BOOT_SELECTOR_COUNT] = {1, A3};

static HTU21D htu21d;
static MCP342X mcp3428_1;
static ACS764 acs764;

static bool wireEnabled = true;

Expand Down Expand Up @@ -118,6 +120,13 @@ void init()
htu21d.begin();
delay(200);

acs764.init(SYSTEM_CURRENT_ADDRESS);
for (byte i = 0; i < PORT_COUNT; i++)
{
acs764.init(PORT_CURRENT_ADDRESS[i]);
}
delay(200);

noInterrupts();

// schedule timer 1 to check on heartbeat signal. this is to ensure that
Expand Down Expand Up @@ -264,57 +273,7 @@ bool validBootSelector(byte selector)

unsigned int getAddressCurrent(byte addr)
{
static const unsigned int MILLIAMPS_PER_STEP = 16;
byte csb, lsb;
byte attempts;
byte timeout;

if (!getWireEnabled())
return 0xFFFF;

for (attempts = 0; attempts < 10; attempts++) {

/* request data from sensor */
Wire.beginTransmission(addr);
delay(5);
Wire.write(0);
delay(5);

/* retry on error */
if (Wire.endTransmission(0) != 0)
continue;

delay(5);

/* read data from sensor */
if (Wire.requestFrom(addr, 3) != 3)
continue;

delay(5);

for (timeout = 0; timeout < 100 && Wire.available() < 3; timeout++) {
delay(5);
}

/* retry on error */
if (timeout >= 100)
continue;

Wire.read();
csb = Wire.read() & 0x01;
lsb = Wire.read();

/* retry on error */
if (Wire.endTransmission(1) != 0)
continue;

delay(5);

/* return milliamps from raw sensor data. */
return ((csb << 8) | lsb) * MILLIAMPS_PER_STEP;
}

return 0xFFFF; /* return error value */
return acs764.getCurrent(addr);
}

void getTime(time_t &time)
Expand Down