Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/50 hall speed sensor #118

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/KBoxHardware/src/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,7 @@ static const float analog_max_voltage = 3.0 / (10000/(10000+56000.0));
static const pin_t supply_analog = A14;

/* SDCard Interface */
static const pin_t sdcard_cs = 15;
static const pin_t sdcard_cs = 15;

/* Analog Sensor Interface */
static const pin_t paddle_wheel_input = 25;
44 changes: 29 additions & 15 deletions src/common/signalk/SKNMEA2000Converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,36 @@
#include "SKUnits.h"

void SKNMEA2000Converter::convert(const SKUpdate& update, SKNMEA2000Output& out) {
// Trigger a call of visitSKElectricalBatteriesVoltage for every key with that path
// (there can be more than one and we do not know how they are called)
_currentOutput = &out;
update.accept(*this, SKPathElectricalBatteriesVoltage);
// Please keep the PGNs in order in this function!

if (update.hasEnvironmentOutsidePressure()) {
// PGN 127250: Magnetic Heading
if (update.hasNavigationHeadingMagnetic()) {
tN2kMsg msg;
// PGN 130310 seems to be better supported
SetN2kOutsideEnvironmentalParameters(msg, 0, N2kDoubleNA, N2kDoubleNA, update.getEnvironmentOutsidePressure());
// PGN 130314 is more specific to pressure but not supported by Raymarine i70
//SetN2kPressure(*msg, /* sid */ 0, /* source */ 0, N2kps_Atmospheric, v.getNumberValue());
SetN2kMagneticHeading(msg, 0, update.getNavigationHeadingMagnetic());
out.write(msg);
}

// PGN: 127257: Attitude
if (update.hasNavigationAttitude()) {
tN2kMsg msg;
SKTypeAttitude attitude = update.getNavigationAttitude();
SetN2kAttitude(msg, 0, attitude.yaw, attitude.pitch, attitude.roll);
out.write(msg);
}

// PGN 127508: Battery Staus
// Trigger a call of visitSKElectricalBatteriesVoltage for every key with that path
// (there can be more than one and we do not know how they are called)
_currentOutput = &out;
update.accept(*this, SKPathElectricalBatteriesVoltage);

// PGN 128259: Boat Speed
if (update.hasNavigationSpeedThroughWater()) {
tN2kMsg msg;
SetN2kBoatSpeed(msg, 0, update.getNavigationSpeedThroughWater(), N2kDoubleNA, N2kSWRT_Paddle_wheel);
out.write(msg);
}

// PGN 129026: Fast COG/SOG
if (update.hasNavigationSpeedOverGround() && update.hasNavigationCourseOverGroundTrue()) {
tN2kMsg msg;
Expand All @@ -61,12 +70,7 @@ void SKNMEA2000Converter::convert(const SKUpdate& update, SKNMEA2000Output& out)
out.write(msg);
}

if (update.hasNavigationHeadingMagnetic()) {
tN2kMsg msg;
SetN2kMagneticHeading(msg, 0, update.getNavigationHeadingMagnetic());
out.write(msg);
}

// PGN 130306: Wind speed
if (update.hasEnvironmentWindAngleApparent() && update.hasEnvironmentWindSpeedApparent()) {
generateWind(out, update.getEnvironmentWindAngleApparent(), update.getEnvironmentWindSpeedApparent(), N2kWind_Apparent);
}
Expand All @@ -86,6 +90,16 @@ void SKNMEA2000Converter::convert(const SKUpdate& update, SKNMEA2000Output& out)
if (update.hasEnvironmentWindDirectionTrue() && update.hasEnvironmentWindSpeedOverGround()) {
generateWind(out, update.getEnvironmentWindDirectionTrue(), update.getEnvironmentWindSpeedOverGround(), N2kWind_True_North);
}

// PGN: 130310: OutsideEnvironmentalParameters
if (update.hasEnvironmentOutsidePressure()) {
tN2kMsg msg;
// PGN 130310 seems to be better supported
SetN2kOutsideEnvironmentalParameters(msg, 0, N2kDoubleNA, N2kDoubleNA, update.getEnvironmentOutsidePressure());
// PGN 130314 is more specific to pressure but not supported by Raymarine i70
//SetN2kPressure(*msg, /* sid */ 0, /* source */ 0, N2kps_Atmospheric, v.getNumberValue());
out.write(msg);
}
}

void SKNMEA2000Converter::visitSKElectricalBatteriesVoltage(const SKUpdate& u, const SKPath &p, const SKValue &v) {
Expand Down
2 changes: 2 additions & 0 deletions src/host/config/KBoxConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "IMUConfig.h"
#include "BarometerConfig.h"
#include "WiFiConfig.h"
#include "PaddleWheelConfig.h"

/**
* A KBox configuration in memory
Expand All @@ -46,4 +47,5 @@ struct KBoxConfig {
IMUConfig imuConfig;
BarometerConfig barometerConfig;
WiFiConfig wifiConfig;
PaddleWheelConfig paddleWheelConfig;
};
16 changes: 16 additions & 0 deletions src/host/config/KBoxConfigParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ void KBoxConfigParser::defaultConfig(KBoxConfig &config) {
config.barometerConfig.frequency = 1;

config.wifiConfig.enabled = true;

config.paddleWheelConfig.enabled = false;
config.paddleWheelConfig.frequency = 10;
config.paddleWheelConfig.pulsesPerNauticalMile = 20000;
}

void KBoxConfigParser::parseKBoxConfig(const JsonObject &json, KBoxConfig &config) {
Expand All @@ -74,6 +78,7 @@ void KBoxConfigParser::parseKBoxConfig(const JsonObject &json, KBoxConfig &confi
parseBarometerConfig(json["barometer"], config.barometerConfig);
parseWiFiConfig(json["wifi"], config.wifiConfig);
parseNMEA2000Config(json["nmea2000"], config.nmea2000Config);
parsePaddleWheelConfig(json["analogSensor"], config.paddleWheelConfig);
}

void KBoxConfigParser::parseIMUConfig(const JsonObject &json, IMUConfig &config) {
Expand Down Expand Up @@ -116,6 +121,17 @@ void KBoxConfigParser::parseWiFiConfig(const JsonObject &json, WiFiConfig &confi
parseNMEAConverterConfig(json["nmeaConverter"], config.nmeaConverter);
}

void KBoxConfigParser::parsePaddleWheelConfig(const JsonObject &json,
PaddleWheelConfig &config) {
if (json == JsonObject::invalid()) {
return;
}

READ_BOOL_VALUE(enabled);
READ_INT_VALUE(frequency);
READ_INT_VALUE_WRANGE(pulsesPerNauticalMile, 1, 100000);
}

void KBoxConfigParser::parseNMEAConverterConfig(const JsonObject &json, SKNMEAConverterConfig &config) {
if (json == JsonObject::invalid()) {
return;
Expand Down
5 changes: 4 additions & 1 deletion src/host/config/KBoxConfigParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,8 @@ class KBoxConfigParser {
void parseSerialConfig(const JsonObject &object, SerialConfig &config);
void parseNMEA2000Config(const JsonObject &object, NMEA2000Config &config);
void parseWiFiConfig(const JsonObject &json, WiFiConfig &config);
void parseNMEAConverterConfig(const JsonObject &json, SKNMEAConverterConfig &config);
void parseNMEAConverterConfig(const JsonObject &json,
SKNMEAConverterConfig &config);
void parsePaddleWheelConfig(const JsonObject &json,
PaddleWheelConfig &config);
};
40 changes: 40 additions & 0 deletions src/host/config/PaddleWheelConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
__ __ ______ ______ __ __
/\ \/ / /\ == \ /\ __ \ /\_\_\_\
\ \ _"-. \ \ __< \ \ \/\ \ \/_/\_\/_
\ \_\ \_\ \ \_____\ \ \_____\ /\_\/\_\
\/_/\/_/ \/_____/ \/_____/ \/_/\/_/

The MIT License

Copyright (c) 2017 Thomas Sarlandie [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#pragma once


struct PaddleWheelConfig {
bool enabled;
int frequency;
int pulsesPerNauticalMile;
};


10 changes: 10 additions & 0 deletions src/host/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "host/services/SDCardTask.h"
#include "host/services/USBService.h"
#include "host/services/WiFiService.h"
#include "host/services/PaddleWheelService.h"

static const char *configFilename = "kbox-config.json";

Expand Down Expand Up @@ -115,6 +116,9 @@ void setup() {
reader2->connectTo(*sdcardTask);
n2kService->connectTo(*sdcardTask);

PaddleWheelService* analogSensorService =
new PaddleWheelService(config.paddleWheelConfig, skHub);

// Add all the tasks
taskManager.addTask(&mfd);
taskManager.addTask(new IntervalTask(new RunningLightService(), 250));
Expand All @@ -125,6 +129,12 @@ void setup() {
if (config.barometerConfig.enabled) {
taskManager.addTask(new IntervalTask(baroService, 1000 / config.barometerConfig.frequency));
}
if (config.paddleWheelConfig.enabled) {
taskManager.addTask(
new IntervalTask(analogSensorService,
1000 / config.paddleWheelConfig.frequency));

}
taskManager.addTask(n2kService);
taskManager.addTask(reader1);
taskManager.addTask(reader2);
Expand Down
109 changes: 109 additions & 0 deletions src/host/services/PaddleWheelService.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
__ __ ______ ______ __ __
/\ \/ / /\ == \ /\ __ \ /\_\_\_\
\ \ _"-. \ \ __< \ \ \/\ \ \/_/\_\/_
\ \_\ \_\ \ \_____\ \ \_____\ /\_\/\_\
\/_/\/_/ \/_____/ \/_____/ \/_/\/_/

The MIT License

Copyright (c) 2017 Thomas Sarlandie [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "PaddleWheelService.h"

#include <KBoxHardware.h>
#include <common/signalk/SKUpdateStatic.h>
#include "common/signalk/SKUnits.h"

/*
* Standard paddle wheels are designed to generate 20000 pulses per mile.
*
* At 1knot, that is 5.5 pulses per second which is not enough to provide an
* accurate speed measurement by counting pulses per second.
*
* Instead we average the duration of pulses between updates.
*/
static volatile uint32_t s_sumPulsesDurationUS;
static volatile uint32_t s_countPulses;

/**
* Interrupt handler called every time a new pulse is detected.
*/
static void paddleWheelPulse() {
static uint32_t lastPulse = UINT32_MAX;

uint32_t now = micros();
// This handles the initial start and the overflow case. We just ignore the
// pulse in this case.
if (now > lastPulse) {
uint32_t pulseDuration = now - lastPulse;
s_sumPulsesDurationUS += pulseDuration;
s_countPulses++;
}
lastPulse = micros();
}

PaddleWheelService::PaddleWheelService(const PaddleWheelConfig& config,
SKHub& hub) :
Task("PaddleWheel"), _config(config), _hub(hub) {

}

void PaddleWheelService::setup() {
Task::setup();

s_sumPulsesDurationUS = 0;
s_countPulses = 0;
pinMode(paddle_wheel_input, INPUT_PULLDOWN);
attachInterrupt(digitalPinToInterrupt(paddle_wheel_input), paddleWheelPulse, RISING);
}


void PaddleWheelService::loop() {
// Disable interrupt to make a copy of the variable that might be
// manipulated by the interrupt handler while we process the loop.
cli();
uint32_t sumPulsesDurationUS = s_sumPulsesDurationUS;
uint32_t countPulses = s_countPulses;
// Reset the variables to 0 now that we have read them.
s_sumPulsesDurationUS = 0;
s_countPulses = 0;
sei();

double pulsePeriodUS = sumPulsesDurationUS / countPulses;
double boatSpeed = 0;

if (pulsePeriodUS > 0) {
// calculate speed in mile / second
boatSpeed = 1.0 /
(_config.pulsesPerNauticalMile * pulsePeriodUS * 1e-6);
// convert to mile / hour
boatSpeed *= 3600;
}

DEBUG("Paddle wheel period: %.2f us boat speed: %.2f kt - SumPeriod: %i Count: %i",
pulsePeriodUS, boatSpeed, sumPulsesDurationUS, countPulses);
SKUpdateStatic<1> update;
// TODO: Set the source properly once the WiFi PR is merged (it includes sources for sensors).
update.setNavigationSpeedThroughWater(SKKnotToMs(boatSpeed));
_hub.publish(update);
}
50 changes: 50 additions & 0 deletions src/host/services/PaddleWheelService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
__ __ ______ ______ __ __
/\ \/ / /\ == \ /\ __ \ /\_\_\_\
\ \ _"-. \ \ __< \ \ \/\ \ \/_/\_\/_
\ \_\ \_\ \ \_____\ \ \_____\ /\_\/\_\
\/_/\/_/ \/_____/ \/_____/ \/_/\/_/

The MIT License

Copyright (c) 2017 Thomas Sarlandie [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#pragma once

#include "common/os/Task.h"
#include "common/signalk/SKHub.h"
#include "host/config/PaddleWheelConfig.h"

class PaddleWheelService : public Task {
private:
const PaddleWheelConfig& _config;
SKHub& _hub;

public:
PaddleWheelService(const PaddleWheelConfig& config, SKHub& hub);

PaddleWheelService(const char *taskName);

void setup() override;

void loop() override;
};
Loading