Skyward boardcore
Loading...
Searching...
No Matches
ND015D.cpp
Go to the documentation of this file.
1/* Copyright (c) 2025 Skyward Experimental Rocketry
2 * Author: Pietro Bortolus
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 "ND015D.h"
24
26#include <utils/Constants.h>
27
28#include <cmath>
29#include <string>
30
31namespace Boardcore
32{
33const char ND015D::MODEL_NAME[] = "ND015D";
34
36{
37 SPIBusConfig spiConfig{};
38 spiConfig.mode = SPI::Mode::MODE_1;
39 spiConfig.clockDivider = SPI::ClockDivider::DIV_256;
40
41 // Datasheet specifies 100us, but 50us is enough from testing
42 spiConfig.csSetupTimeUs = 50;
43
44 // The datasheet specifies a minimum CS hold time of 100us
45 // If this is ever an issue, implement it by waiting at the next sample
46 // instead of setting it in SPIBusConfig
47
48 return spiConfig;
49}
50
51ND015D::ND015D(SPIBusInterface& bus, miosix::GpioPin cs, SPIBusConfig spiConfig,
53 NotchEnable ntc, uint8_t odr)
54 : slave(bus, cs, spiConfig), range(rangeToPressure(fsr)),
55 sensorSettings{fsr, iow, bwl, ntc, odr}
56{
57}
58
60{
61 // setting the sensor settings to the correct values
62 SPITransaction spi(slave);
63 uint16_t spiDataOut;
64
65 memcpy(&spiDataOut, &sensorSettings, sizeof(spiDataOut));
66 spi.transfer16(spiDataOut);
67
68 return true;
69}
70
71bool ND015D::selfTest() { return true; }
72
74{
75 ND015DDataExtended extendedData{};
76 uint8_t* data = reinterpret_cast<uint8_t*>(&extendedData);
77
78 // setting the first 2 bytes of the data to the correct sensor settings
79 memcpy(&extendedData, &sensorSettings, sizeof(sensorSettings));
80
81 SPITransaction spi(slave);
82 spi.transfer(data, sizeof(extendedData));
83
84 // this part checks if the model number returned by the sensor matches the
85 // correct model number
86 bool compareResult =
87 (memcmp(&extendedData.model, &MODEL_NAME, sizeof(MODEL_NAME) - 1) == 0);
88
89 if (!compareResult)
90 {
91 auto model =
92 std::string(extendedData.model, sizeof(extendedData.model));
93 // Replace all \0 with '.' for printing
94 std::replace(model.begin(), model.end(), '\0', '.');
95
96 LOG_ERR(logger,
97 "Sensor model mismatch: received {}, expected {} (. = \\0)",
98 model, MODEL_NAME);
99 return false;
100 }
101 return true;
102}
103
105{
106 sensorSettings.odr = odr;
107
108 SPITransaction spi(slave);
109 uint16_t spiDataOut;
110
111 memcpy(&spiDataOut, &sensorSettings, sizeof(spiDataOut));
112 spi.transfer16(spiDataOut);
113}
114
116{
117 sensorSettings.fsr = fsr;
118
119 range = rangeToPressure(fsr);
120
121 SPITransaction spi(slave);
122 uint16_t spiDataOut;
123
124 memcpy(&spiDataOut, &sensorSettings, sizeof(spiDataOut));
125 spi.transfer16(spiDataOut);
126}
127
129{
130 switch (fsr)
131 {
133 return 1;
135 return 2;
137 return 4;
139 return 5;
141 return 10;
143 return 15;
144 default:
145 return 0;
146 }
147}
148
150{
151 sensorSettings.iow = iow;
152
153 SPITransaction spi(slave);
154 uint16_t spiDataOut;
155
156 memcpy(&spiDataOut, &sensorSettings, sizeof(spiDataOut));
157 spi.transfer16(spiDataOut);
158}
159
161{
162 sensorSettings.bwl = bwl;
163
164 SPITransaction spi(slave);
165 uint16_t spiDataOut;
166
167 memcpy(&spiDataOut, &sensorSettings, sizeof(spiDataOut));
168 spi.transfer16(spiDataOut);
169}
170
172{
173 sensorSettings.ntc = ntc;
174
175 SPITransaction spi(slave);
176 uint16_t spiDataOut;
177
178 memcpy(&spiDataOut, &sensorSettings, sizeof(spiDataOut));
179 spi.transfer16(spiDataOut);
180}
181
183{
184 ND015XData data;
185 uint16_t spiDataOut;
186 SPITransaction spi(slave);
187
188 memcpy(&spiDataOut, &sensorSettings, sizeof(spiDataOut));
189 uint16_t spiDataIn = spi.transfer16(spiDataOut);
190
191 data.pressure =
192 (static_cast<int16_t>(spiDataIn) / (0.9 * pow(2, 15)) * range) *
193 Constants::PSI_TO_PASCAL;
195
196 return data;
197}
198
199} // namespace Boardcore
#define LOG_ERR(logger,...)
void setIOWatchdog(IOWatchdogEnable iow)
Enables or disables the IO watchdog.
Definition ND015D.cpp:149
void setFullScaleRange(FullScaleRange fsr)
Sets the full-scale range for the sensor.
Definition ND015D.cpp:115
BWLimitFilter bwl
Definition ND015D.h:187
bool init() override
Initializes the sensor.
Definition ND015D.cpp:59
FullScaleRange fsr
Definition ND015D.h:185
void setBWLimitFilter(BWLimitFilter bwl)
Sets the bandwidth limit filter for the sensor.
Definition ND015D.cpp:160
void setNotch(NotchEnable ntc)
Enables or disables the notch filter.
Definition ND015D.cpp:171
static SPIBusConfig getDefaultSPIConfig()
Constructs the default config for the SPI bus.
Definition ND015D.cpp:35
static float rangeToPressure(FullScaleRange fsr)
Converts the FullScale value to its corresponding range.
Definition ND015D.cpp:128
IOWatchdogEnable iow
Definition ND015D.h:186
NotchEnable ntc
Definition ND015D.h:188
static const char MODEL_NAME[]
Definition ND015D.h:37
bool checkModelMatch()
Checks if the sensor model matches the expected model.
Definition ND015D.cpp:73
void setOutputDataRate(uint8_t odr)
function to set the output data rate
Definition ND015D.cpp:104
ND015XData sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
Definition ND015D.cpp:182
ND015D(SPIBusInterface &bus, miosix::GpioPin cs, SPIBusConfig spiConfig, FullScaleRange fsr=FullScaleRange::FS_2, IOWatchdogEnable iow=IOWatchdogEnable::DISABLED, BWLimitFilter bwl=BWLimitFilter::BWL_200, NotchEnable ntc=NotchEnable::ENABLED, uint8_t odr=0x1C)
Constructor for the ND015D sensor.
Definition ND015D.cpp:51
bool selfTest() override
Not implemented.
Definition ND015D.cpp:71
Interface for low level access of a SPI bus as a master.
Provides high-level access to the SPI Bus for a single transaction.
uint16_t transfer16(uint16_t data)
Full duplex transmission of one half word on the bus.
uint8_t transfer(uint8_t data)
Full duplex transmission of one byte on the bus.
@ 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::Mode mode
MSBit or LSBit first.