Skyward boardcore
Loading...
Searching...
No Matches
ND015A.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 "ND015A.h"
24
26#include <utils/Constants.h>
27
28#include <cmath>
29#include <string>
30
31namespace Boardcore
32{
33const char ND015A::MODEL_NAME[] = "ND015A";
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
51ND015A::ND015A(SPIBusInterface& bus, miosix::GpioPin cs, SPIBusConfig spiConfig,
53 uint8_t odr)
54 : slave(bus, cs, spiConfig), sensorSettings{0x7, iow, bwl, ntc, odr}
55{
56}
57
59{
60 // setting the sensor settings to the correct values
61 SPITransaction spi(slave);
62 uint16_t spiDataOut;
63
64 memcpy(&spiDataOut, &sensorSettings, sizeof(spiDataOut));
65 spi.transfer16(spiDataOut);
66
67 return true;
68}
69
70bool ND015A::selfTest() { return true; }
71
73{
74 ND015ADataExtended extendedData{};
75 uint8_t* data = reinterpret_cast<uint8_t*>(&extendedData);
76
77 // setting the first 2 bytes of the data to the correct sensor settings
78 memcpy(&extendedData, &sensorSettings, sizeof(sensorSettings));
79
80 SPITransaction spi(slave);
81 spi.transfer(data, sizeof(extendedData));
82
83 // this part checks if the model number returned by the sensor matches the
84 // correct model number
85 bool compareResult =
86 (memcmp(&extendedData.model, &MODEL_NAME, sizeof(MODEL_NAME) - 1) == 0);
87
88 if (!compareResult)
89 {
90 auto model =
91 std::string(extendedData.model, sizeof(extendedData.model));
92 // Replace all \0 with '.' for printing
93 std::replace(model.begin(), model.end(), '\0', '.');
94
95 LOG_ERR(logger,
96 "Sensor model mismatch: received {}, expected {} (. = \\0)",
97 model, MODEL_NAME);
98 return false;
99 }
100 return true;
101}
102
104{
105 sensorSettings.odr = odr;
106
107 SPITransaction spi(slave);
108 uint16_t spiDataOut;
109
110 memcpy(&spiDataOut, &sensorSettings, sizeof(spiDataOut));
111 spi.transfer16(spiDataOut);
112}
113
115{
116 sensorSettings.iow = iow;
117
118 SPITransaction spi(slave);
119 uint16_t spiDataOut;
120
121 memcpy(&spiDataOut, &sensorSettings, sizeof(spiDataOut));
122 spi.transfer16(spiDataOut);
123}
124
126{
127 sensorSettings.bwl = bwl;
128
129 SPITransaction spi(slave);
130 uint16_t spiDataOut;
131
132 memcpy(&spiDataOut, &sensorSettings, sizeof(spiDataOut));
133 spi.transfer16(spiDataOut);
134}
135
137{
138 sensorSettings.ntc = ntc;
139
140 SPITransaction spi(slave);
141 uint16_t spiDataOut;
142
143 memcpy(&spiDataOut, &sensorSettings, sizeof(spiDataOut));
144 spi.transfer16(spiDataOut);
145}
146
148{
149 ND015XData data;
150 uint16_t spiDataOut;
151 SPITransaction spi(slave);
152
153 memcpy(&spiDataOut, &sensorSettings, sizeof(spiDataOut));
154 uint16_t spiDataIn = spi.transfer16(spiDataOut);
155
156 data.pressure =
157 ((spiDataIn - 0.05 * pow(2, 16)) / (0.9 * pow(2, 16)) * 15) *
158 Constants::PSI_TO_PASCAL;
160
161 return data;
162}
163
164} // namespace Boardcore
#define LOG_ERR(logger,...)
bool init() override
Initializes the sensor.
Definition ND015A.cpp:58
void setBWLimitFilter(BWLimitFilter bwl)
Sets the bandwidth limit filter for the sensor.
Definition ND015A.cpp:125
bool checkModelMatch()
Checks if the sensor model matches the expected model.
Definition ND015A.cpp:72
void setNotch(NotchEnable ntc)
Enables or disables the notch filter.
Definition ND015A.cpp:136
NotchEnable ntc
Definition ND015A.h:156
bool selfTest() override
Not implemented.
Definition ND015A.cpp:70
static const char MODEL_NAME[]
Definition ND015A.h:37
static SPIBusConfig getDefaultSPIConfig()
Constructs the default config for the SPI bus.
Definition ND015A.cpp:35
IOWatchdogEnable iow
Definition ND015A.h:154
ND015XData sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
Definition ND015A.cpp:147
void setIOWatchdog(IOWatchdogEnable iow)
function to enable the IO watchdog
Definition ND015A.cpp:114
BWLimitFilter bwl
Definition ND015A.h:155
void setOutputDataRate(uint8_t odr)
function to set the output data rate
Definition ND015A.cpp:103
ND015A(SPIBusInterface &bus, miosix::GpioPin cs, SPIBusConfig spiConfig, IOWatchdogEnable iow=IOWatchdogEnable::DISABLED, BWLimitFilter bwl=BWLimitFilter::BWL_200, NotchEnable ntc=NotchEnable::ENABLED, uint8_t odr=0x1C)
Constructor for the ND015A sensor.
Definition ND015A.cpp:51
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.