Skyward boardcore
Loading...
Searching...
No Matches
HX711.cpp
Go to the documentation of this file.
1/* Copyright (c) 2022 Skyward Experimental Rocketry
2 * Author: Alberto Nidasio
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#include "HX711.h"
24
26#include <interfaces-impl/gpio_impl.h>
27#include <miosix.h>
28
29namespace Boardcore
30{
31
32HX711::HX711(SPIBusInterface& bus, miosix::GpioPin sckPin, SPIBusConfig config,
33 unsigned char sckAlternateFunction)
34 : bus(bus), sckPin(sckPin), config(config),
35 sckAlternateFunction(sckAlternateFunction)
36{
37}
38
40{
41 SPIBusConfig spiConfig{};
43 spiConfig.mode = SPI::Mode::MODE_1;
44 return spiConfig;
45}
46
47bool HX711::init() { return true; }
48
49bool HX711::selfTest() { return true; }
50
52{
53 int32_t sample;
54
55 bus.configure(config);
56 sample = bus.read16() << 8;
57 sample |= bus.read();
58 sckPin.mode(miosix::Mode::OUTPUT);
59 sckPin.high();
60 miosix::delayUs(1);
61 sckPin.low();
62 sckPin.mode(miosix::Mode::ALTERNATE);
63 sckPin.alternateFunction(sckAlternateFunction);
64
65 if (sample & static_cast<int32_t>(0x800000))
66 sample |= static_cast<uint32_t>(0xFF) << 24;
67
68 if (sample == static_cast<int32_t>(0xFFFFFFFF))
69 return lastSample;
70
72 static_cast<float>(sample - offset) * scale};
73}
74
75void HX711::computeScale(float value, float sample)
76{
77 // Convert the sample in raw measurement with current scale factor
78 sample = sample / scale;
79
80 // Update the scale such that the sample corresponds to the given value
81 this->scale = value / sample;
82}
83
84void HX711::computeScale(float value) { computeScale(value, lastSample.load); }
85
86void HX711::setScale(float scale) { this->scale = scale; }
87
88float HX711::getScale() { return scale; }
89
90void HX711::setOffset(float offset) { this->offset = offset / scale; }
91
92void HX711::updateOffset(float offset) { this->offset += offset / scale; }
93
94float HX711::getOffset() { return offset; }
95
96} // namespace Boardcore
bool selfTest() override
Check if the sensor is working.
Definition HX711.cpp:49
float getScale()
Returns the current scale.
Definition HX711.cpp:88
static SPIBusConfig getDefaultSPIConfig()
Definition HX711.cpp:39
void setScale(float scale)
Simply changes the scale.
Definition HX711.cpp:86
void updateOffset(float offset)
Updates the offset by adding it to the current offset.
Definition HX711.cpp:92
HX711(SPIBusInterface &bus, miosix::GpioPin sckPin, SPIBusConfig config=getDefaultSPIConfig(), unsigned char sckAlternateFunction=5)
Definition HX711.cpp:32
HX711Data sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
Definition HX711.cpp:51
void computeScale(float value, float sample)
Calculates the scale value such that the load cell's output matches the given value.
Definition HX711.cpp:75
void setOffset(float offset)
Sets the offset to the given value.
Definition HX711.cpp:90
bool init() override
Initialize the sensor.
Definition HX711.cpp:47
float getOffset()
Return the current offset.
Definition HX711.cpp:94
Interface for low level access of a SPI bus as a master.
virtual uint16_t read16()=0
Reads 16 bits from the bus.
virtual uint8_t read()=0
Reads 8 bits from the bus.
virtual void configure(SPIBusConfig config)=0
Configures the bus with the provided configuration parameters.
@ MODE_1
CPOL = 1, CPHA = 0 -> Clock high when idle, sample on first edge.
uint64_t getTimestamp()
Returns the current timer value in microseconds.
This file includes all the types the logdecoder script will decode.
SPI Bus configuration for a specific slave.
SPI::ClockDivider clockDivider
< Peripheral clock division