mirror of
https://gitlab.com/lander-team/static-load-test-code.git
synced 2025-08-02 03:21:25 +00:00
working load cell readout
This commit is contained in:
63
src/main.cpp
63
src/main.cpp
@@ -1,7 +1,7 @@
|
||||
#include <Arduino.h>
|
||||
#include "HX711.h"
|
||||
#include "LoadCells.h"
|
||||
#include "SD.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
LoadCells State;
|
||||
|
||||
@@ -24,11 +24,6 @@ const int pin_lc1 = 15;
|
||||
const int pin_lc2 = 19;
|
||||
const int pin_lc3 = 20;
|
||||
|
||||
double lc0Offset = 0.0;
|
||||
double lc1Offset = 0.0;
|
||||
double lc2Offset = 0.0;
|
||||
double lc3Offset = 0.0;
|
||||
|
||||
const int chipSelect = BUILTIN_SDCARD;
|
||||
File dataFile;
|
||||
|
||||
@@ -52,9 +47,13 @@ void setup() {
|
||||
lc3.set_scale();
|
||||
|
||||
lc0.tare(50);
|
||||
Serial.println(".");
|
||||
lc1.tare(50);
|
||||
Serial.println(".");
|
||||
lc2.tare(50);
|
||||
Serial.println(".");
|
||||
lc3.tare(50);
|
||||
Serial.println("Load Cells Tared");
|
||||
|
||||
// Attach ISRs to load cell data pins to read data when available
|
||||
attachInterrupt(digitalPinToInterrupt(pin_lc0), read_lc0, LOW);
|
||||
@@ -65,32 +64,6 @@ void setup() {
|
||||
Serial.println("Load Cells Initialized");
|
||||
Serial.println("Calibrating");
|
||||
|
||||
// Load Cell Tare
|
||||
double lc0Tot = 0.0;
|
||||
double lc1Tot = 0.0;
|
||||
double lc2Tot = 0.0;
|
||||
double lc3Tot = 0.0;
|
||||
|
||||
for (int i = 0; i < 50; i++) {
|
||||
lc0Tot += State.lc0;
|
||||
lc1Tot += State.lc1;
|
||||
lc2Tot += State.lc2;
|
||||
lc3Tot += State.lc3;
|
||||
delay(50);
|
||||
}
|
||||
|
||||
lc0Offset = lc0Tot / 50.0;
|
||||
lc1Offset = lc1Tot / 50.0;
|
||||
lc2Offset = lc2Tot / 50.0;
|
||||
lc3Offset = lc3Tot / 50.0;
|
||||
|
||||
Serial.println(lc0Tot);
|
||||
Serial.println(lc1Tot);
|
||||
Serial.println(lc2Tot);
|
||||
Serial.println(lc3Tot);
|
||||
|
||||
Serial.println("Load Cells Tared");
|
||||
|
||||
// Initializes State variables
|
||||
State.lc0 = lc0.get_value();
|
||||
State.lc1 = lc1.get_value();
|
||||
@@ -100,14 +73,18 @@ void setup() {
|
||||
|
||||
void loop() {
|
||||
// put your main code here, to run repeatedly:
|
||||
write2CSV(State);
|
||||
delay(20);
|
||||
// write2CSV(State);
|
||||
// delay(20);
|
||||
Serial.println(String(State.lc0) + ",\t" + String(State.lc1) + ",\t" +
|
||||
String(State.lc2) + ",\t" + String(State.lc3));
|
||||
Serial.println("~+~+~+~+~+~+~+~+~+~+~+~");
|
||||
// delay(1000);
|
||||
}
|
||||
|
||||
void read_lc0() { State.lc0 = lc0.get_value() - lc0Offset; }
|
||||
void read_lc1() { State.lc1 = lc1.get_value() - lc1Offset; }
|
||||
void read_lc2() { State.lc2 = lc2.get_value() - lc2Offset; }
|
||||
void read_lc3() { State.lc3 = lc3.get_value() - lc3Offset; }
|
||||
void read_lc0() { State.lc0 = lc0.get_value(); }
|
||||
void read_lc1() { State.lc1 = lc1.get_value(); }
|
||||
void read_lc2() { State.lc2 = lc2.get_value(); }
|
||||
void read_lc3() { State.lc3 = lc3.get_value(); }
|
||||
|
||||
void write2CSV(LoadCells &State) {
|
||||
|
||||
@@ -122,7 +99,7 @@ void write2CSV(LoadCells &State) {
|
||||
dataFile.close();
|
||||
}
|
||||
|
||||
void initFile(){
|
||||
void initFile() {
|
||||
Serial.print("Initializing SD card...");
|
||||
// see if the card is present and can be initialized:
|
||||
if (!SD.begin(chipSelect)) {
|
||||
@@ -133,9 +110,9 @@ void initFile(){
|
||||
int i = 1;
|
||||
const char *fileName;
|
||||
|
||||
if (SD.exists("simOut.csv")) {
|
||||
if (SD.exists("loadCell.csv")) {
|
||||
while (i > 0.0) {
|
||||
fileName = ("simOut_" + String(i) + ".csv").c_str();
|
||||
fileName = ("loadCell_" + String(i) + ".csv").c_str();
|
||||
|
||||
if (!SD.exists(fileName)) {
|
||||
// Open simOut_i.csv
|
||||
@@ -165,7 +142,5 @@ void initFile(){
|
||||
Serial.println("Error opening output file. \n\nABORTING SIMULATION");
|
||||
}
|
||||
}
|
||||
dataFile.println(
|
||||
"lc0,lc1,lc2,lc3");
|
||||
dataFile.println("lc0,lc1,lc2,lc3");
|
||||
}
|
||||
|
Reference in New Issue
Block a user