Skip to content

Commit 33af3c0

Browse files
committed
Add load cell
1 parent 8db7428 commit 33af3c0

File tree

10 files changed

+122
-1
lines changed

10 files changed

+122
-1
lines changed

src/IO/ArduinoProxy.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ void ArduinoProxy::handleArduinoMessage(const RocketryProto::ArduinoOut &arduino
126126
dcMotorState.maxlimitswitch(), std::chrono::steady_clock::now()};
127127
}
128128
break;
129+
case RocketryProto::ArduinoOut::kLoadCellState: {
130+
std::lock_guard<std::mutex> lockGuard(stateMutex);
131+
132+
const auto &state = arduinoOut.loadcellstate();
133+
loadCellState = {state.value(), std::chrono::steady_clock::now()};
134+
}
135+
break;
129136
case RocketryProto::ArduinoOut::DATA_NOT_SET:
130137
SPDLOG_LOGGER_WARN(logger, "Data field not set in Arduino message. ");
131138
break;
@@ -211,4 +218,23 @@ DCMotorState ArduinoProxy::getDCMotorState(int forwardPin, int reversePin)
211218
}
212219
}
213220

221+
int ArduinoProxy::getLoadCellState()
222+
{
223+
std::lock_guard<std::mutex> lockGuard(stateMutex);
224+
225+
auto state = loadCellState;
226+
auto now = std::chrono::steady_clock::now();
227+
228+
if (now - state.second >= pinStateTimeout)
229+
{
230+
loadCellState = {-1, std::chrono::steady_clock::now()};
231+
SPDLOG_ERROR("Arduino stopped reporting load cell state");
232+
return -1;
233+
}
234+
else
235+
{
236+
return state.first;
237+
}
238+
}
239+
214240
#endif

src/IO/ArduinoProxy.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ArduinoProxy : IO
2525
bool getDigitalState(int pin);
2626
int getServoState(int pin);
2727
DCMotorState getDCMotorState(int forwardPin, int reversePin);
28+
int getLoadCellState();
2829

2930
ArduinoProxy(ArduinoProxy const &) = delete;
3031
void operator=(ArduinoProxy const &) = delete;
@@ -33,6 +34,7 @@ class ArduinoProxy : IO
3334
std::map<unsigned int, std::pair<bool, std::chrono::time_point<std::chrono::steady_clock>>> digitalStates;
3435
std::map<unsigned int, std::pair<int, std::chrono::time_point<std::chrono::steady_clock>>> servoStates;
3536
std::map<std::pair<unsigned int, unsigned int>, DCMotorState> dcMotorStates;
37+
std::pair<int, std::chrono::time_point<std::chrono::steady_clock>> loadCellState;
3638
std::mutex stateMutex;
3739

3840
int fd = 0;

src/IO/SensorLogger.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ void SensorLogger::writeData(std::ofstream &fileStream, const StateData &current
334334
{
335335
fileStream << output.second << sep;
336336
}
337+
338+
339+
fileStream << currentState.gpioState.loadCellState << sep;
337340
#endif
338341

339342
#if USE_SBG == 1

src/IO/gpio/Gpio.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ GpioState Gpio::getCurrentState()
9292
state.dcMotorStateMap.insert({i.first, i.second.getCurrentState()});
9393
}
9494

95+
state.loadCellState = loadCell.getCurrentState();
96+
9597
return state;
9698
}
9799

src/IO/gpio/Gpio.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "IO/gpio/DCMotor.h"
88
#include "IO/gpio/DigitalOutput.h"
99
#include "IO/gpio/PwmOutput.h"
10+
#include "IO/gpio/LoadCell.h"
1011
#include "data/GpioData.h"
1112
#include <condition_variable>
1213
#include <fstream>
@@ -44,6 +45,7 @@ class Gpio : public IO
4445
std::map<std::string, DigitalOutput> digitalOutputsMap;
4546
std::map<std::string, PwmOutput> pwmOutputsMap;
4647
std::map<std::string, DCMotorOutput> dcMotorOutputsMap;
48+
LoadCell loadCell;
4749

4850
struct InitFlags
4951
{

src/IO/gpio/LoadCell.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "config.h"
2+
3+
#if USE_GPIO == 1
4+
5+
#include "LoadCell.h"
6+
#include <iostream>
7+
#include <spdlog/spdlog.h>
8+
#include <utility>
9+
#include <wiringSerial.h>
10+
11+
LoadCell::LoadCell()
12+
{
13+
createThread = false;
14+
logger = spdlog::default_logger();
15+
16+
SPDLOG_LOGGER_DEBUG(logger, "Created LoadCell");
17+
18+
#if USE_ARDUINO_PROXY == 1
19+
arduinoProxy = ArduinoProxy::getInstance();
20+
#endif
21+
}
22+
23+
int LoadCell::getCurrentState()
24+
{
25+
#if USE_ARDUINO_PROXY == 1
26+
try
27+
{
28+
return arduinoProxy->getLoadCellState();
29+
}
30+
catch (std::out_of_range &error)
31+
{
32+
return -1;
33+
}
34+
#else
35+
return -1;
36+
#endif
37+
}
38+
39+
void LoadCell::run()
40+
{
41+
}
42+
43+
bool LoadCell::isInitialized()
44+
{
45+
return true;
46+
}
47+
48+
#endif

src/IO/gpio/LoadCell.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
3+
#include "config.h"
4+
5+
#if USE_GPIO == 1
6+
7+
#include "Output.h"
8+
#include "arduino/DCMotorState.h"
9+
10+
#if USE_ARDUINO_PROXY
11+
#include "IO/ArduinoProxy.h"
12+
#endif
13+
14+
#include <spdlog/logger.h>
15+
#include <string>
16+
17+
class LoadCell : public IO
18+
{
19+
public:
20+
LoadCell();
21+
22+
int getCurrentState();
23+
void run() override;
24+
bool isInitialized() override;
25+
26+
private:
27+
std::shared_ptr<spdlog::logger> logger;
28+
29+
#if USE_ARDUINO_PROXY == 1
30+
ArduinoProxy *arduinoProxy;
31+
#endif
32+
};
33+
34+
#endif

src/data/GpioData.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ struct GpioState
1616
std::map<std::string, int> digitalStateMap;
1717
std::map<std::string, int> pwmStateMap;
1818
std::map<std::string, DCMotorState> dcMotorStateMap;
19+
int loadCellState;
1920
};

src/data/StateData.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ std::string StateData::convertToReducedString() const
126126
data += std::to_string(output.second.maxLimitSwitch);
127127
data += ",";
128128
}
129+
130+
data += std::to_string(gpioState.loadCellState);
131+
data += ",";
129132
#endif
130133

131134
#if USE_SENSOR_SUITE == 1

0 commit comments

Comments
 (0)