|
STAR
Cal STAR — firmware, DAQ server, and engine design
|
#include "main.h"#include <Arduino.h>#include <Ethernet.h>#include <EthernetUdp.h>#include <SPI.h>#include <STAR_EthernetOTA.h>#include <daq-protocol.h>#include <esp_mac.h>#include <cstring>#include <vector>#include "actuator_config.h"#include "board_net.h"#include "firmware_hash.h"#include "hotfire_log.h"Go to the source code of this file.
Classes | |
| struct | Valve |
Enumerations | |
| enum class | BallValveState : uint8_t { WaitingForServer = 1 , Active = 2 } |
Functions | |
| static const char * | stateName (BallValveState s) |
| static void | setState (BallValveState new_state) |
| static Valve * | findValve (uint8_t actuator_id) |
| static void | setValve (Valve &v, uint8_t new_state, const char *reason) |
| static void | setAllValvesSafe (const char *reason) |
| static void | processActuatorCommands (const std::vector< daq::ActuatorCommand > &commands) |
| static uint32_t | readPositionCounts (const Valve &v) |
| Raw 12-bit ADC counts, averaged over POSITION_SAMPLES. | |
| static daq::BoardState | getBoardStateForHeartbeat () |
| static void | sendBoardHeartbeatTo (IPAddress dest_ip, int dest_port) |
| static void | sendPositions () |
| static void | flushLogs () |
| static void | processIncomingPacket (const uint8_t *buffer, size_t len, IPAddress remoteIP) |
| static void | otaStart (void *, uint32_t) |
| An OTA blocks the loop until the board reboots, so the heartbeat watchdog cannot run: put every valve in SAFE_STATE first, then freeze our DHCP address for the duration of the flash. | |
| static void | probeW5500Version () |
| Read the W5500's VERSIONR (common register 0x0039, always 0x04) straight over SPI and say what the answer implies, so a dead link can be traced to a wire without a logic analyser. | |
| static void | initializeValves () |
| void | setup () |
| static void | pollNetwork () |
| void | loop () |
Variables | |
| static constexpr int | ETH_CS = 5 |
| Ball Valve Hotfire — motorized ball valve prototype board. | |
| static constexpr int | ETH_MOSI = 23 |
| static constexpr int | ETH_SCLK = 18 |
| static constexpr int | ETH_MISO = 19 |
| static constexpr int | POSITION_SAMPLES = 16 |
| static constexpr uint8_t | SAFE_STATE = 1 |
| static Valve | valves [] |
| static constexpr uint8_t | NUM_VALVES = sizeof(valves) / sizeof(valves[0]) |
| static uint8_t | board_id = 0 |
| static byte | mac [6] |
| static IPAddress | gateway (0, 0, 0, 0) |
| static IPAddress | subnet (255, 255, 255, 0) |
| static IPAddress | dns (192, 168, 2, 1) |
| static IPAddress | serverIP (192, 168, 2, HOTFIRE_SERVER_IP_OCTET_4) |
| static const int | udpListenPort = 5005 |
| static const int | serverPort = HOTFIRE_SERVER_PORT |
| static EthernetUDP | udp |
| static StarOTA::Server | otaServer (HOTFIRE_OTA_PORT) |
| static BoardNet::State | net |
| static bool | eth_present = false |
| bool | g_verbose = true |
| static BallValveState | state = BallValveState::WaitingForServer |
| static unsigned long | last_server_heartbeat_ms = 0 |
| static unsigned long | last_position_read_ms = 0 |
| static unsigned long | lastHeartbeatMillis = 0 |
|
strong |
|
static |
Definition at line 143 of file main.cpp.
References valves.
Referenced by processActuatorCommands().
|
static |
Definition at line 253 of file main.cpp.
References daq::create_log_packet(), g_logbuf, LOG_BUF_SIZE, millis(), serverIP, serverPort, and udp.
Referenced by pollNetwork(), and setup().
|
static |
Definition at line 199 of file main.cpp.
References daq::ACTIVE, Active, daq::SETUP, state, and WaitingForServer.
Referenced by sendBoardHeartbeatTo().
|
static |
| void loop | ( | ) |
Definition at line 522 of file main.cpp.
References Active, delay(), eth_present, HEARTBEAT_TIMEOUT_MS, last_server_heartbeat_ms, LOOP_DELAY_MS, millis(), pollNetwork(), setAllValvesSafe(), setState(), state, and WaitingForServer.
|
static |
An OTA blocks the loop until the board reboots, so the heartbeat watchdog cannot run: put every valve in SAFE_STATE first, then freeze our DHCP address for the duration of the flash.
Definition at line 330 of file main.cpp.
References BoardNet::beginOtaHold(), net, and setAllValvesSafe().
Referenced by setup().
|
static |
Definition at line 456 of file main.cpp.
References ADC_READ_INTERVAL_MS, BOARD_HEARTBEAT_INTERVAL_MS, board_id, BoardNet::boardId(), dns, Ethernet, flushLogs(), g_logbuf, gateway, HF_LOG, HF_LOGLN_, HOTFIRE_SERVER_IP_OCTET_4, HOTFIRE_SERVER_PORT, IPAddress, last_position_read_ms, lastHeartbeatMillis, BoardNet::maintainLease(), MAX_PACKET_SIZE, millis(), net, otaServer, packetBuffer, StarOTA::printTestMessage(), processIncomingPacket(), BoardNet::retryDhcp(), sendBoardHeartbeatTo(), sendPositions(), Serial, serverIP, serverPort, BoardNet::serverWentSilent(), and udp.
Referenced by loop().
|
static |
Read the W5500's VERSIONR (common register 0x0039, always 0x04) straight over SPI and say what the answer implies, so a dead link can be traced to a wire without a logic analyser.
Definition at line 340 of file main.cpp.
References digitalWrite(), ETH_CS, HEX, HIGH, LOW, and Serial.
Referenced by setup().
|
static |
Definition at line 169 of file main.cpp.
References Active, Valve::actuator_id, commands, findValve(), HF_LOG, HF_LOGLN, SAFE_STATE, setValve(), and state.
Referenced by processIncomingPacket().
|
static |
Definition at line 271 of file main.cpp.
References Active, daq::ACTUATOR_COMMAND, daq::ACTUATOR_CONFIG, commands, g_log_stream_level, g_verbose, last_server_heartbeat_ms, millis(), net, BoardNet::onServerPacket(), daq::parse_actuator_command_packet(), daq::parse_actuator_config_packet(), daq::parse_server_heartbeat_packet(), processActuatorCommands(), daq::SERVER_HEARTBEAT, serverIP, and setState().
Referenced by pollNetwork().
|
static |
Raw 12-bit ADC counts, averaged over POSITION_SAMPLES.
Definition at line 189 of file main.cpp.
References analogRead(), Valve::position_pin, and POSITION_SAMPLES.
Referenced by sendPositions().
|
static |
Definition at line 209 of file main.cpp.
References board_id, daq::create_board_heartbeat_packet(), FirmwareHash::get(), getBoardStateForHeartbeat(), HF_VERBOSE, HF_VERBOSELN, MAX_PACKET_SIZE, millis(), packetBuffer, daq::SAFE, and udp.
Referenced by pollNetwork().
|
static |
Definition at line 231 of file main.cpp.
References chunk, chunks, daq::create_sensor_data_packet(), HF_VERBOSE, HF_VERBOSELN, MAX_PACKET_SIZE, millis(), NUM_VALVES, packetBuffer, readPositionCounts(), serverIP, serverPort, udp, and valves.
Referenced by pollNetwork().
|
static |
Definition at line 164 of file main.cpp.
References SAFE_STATE, setValve(), and valves.
Referenced by loop(), and otaStart().
|
static |
Definition at line 131 of file main.cpp.
References HF_LOG, HF_LOGLN, Serial, state, and stateName().
Referenced by loop(), and processIncomingPacket().
| void setup | ( | void | ) |
Definition at line 380 of file main.cpp.
References BoardNet::begin(), BOARD_ID, board_id, BoardNet::boardId(), BoardNet::configure(), delay(), dns, ETH_CS, ETH_MISO, ETH_MOSI, eth_present, ETH_SCLK, Ethernet, ETHERNET_BEGIN_DELAY_MS, ETHERNET_INIT_DELAY_MS, ETHERNET_SPI_DELAY_MS, flushLogs(), gateway, HF_LOG, HF_LOGLN_, initializeValves(), IPAddress, mac, net, otaServer, otaStart(), FirmwareHash::print(), BoardNet::printMac(), probeW5500Version(), Serial, SERIAL_MONITOR_READY_DELAY_MS, BoardNet::sourceName(), subnet, udp, udpListenPort, and valves.
|
static |
Definition at line 150 of file main.cpp.
References Valve::actuator_id, digitalWrite(), Valve::enable_pin, HF_LOG, HF_LOGLN, HIGH, LOW, and Valve::state.
Referenced by processActuatorCommands(), and setAllValvesSafe().
|
static |
Definition at line 120 of file main.cpp.
References Active, and WaitingForServer.
Referenced by setState().
|
static |
|
staticconstexpr |
Ball Valve Hotfire — motorized ball valve prototype board.
Each valve has an enable line and a position readback:
enable A pull-up holds the 4-20 mA driver's input at 5 V; an NMOS on the enable pin pulls it to 0 V. ACTUATOR_COMMAND state 1 leaves the NMOS off (pin LOW, 5 V to the driver: 20 mA, valve open); state 0 turns it on (pin HIGH, line pulled low: valve closed). position The valve's 4-20 mA position signal across a 100 ohm shunt (0.4-2.0 V) on an ADC1 pin, streamed to the server as SENSOR_DATA: raw 12-bit ADC counts, which is what daq_bridge and calibration_service read for an ACTUATOR board (V = counts / 4095 * 3.3; mA = V * 1000 / 100).
State machine: WaitingForServer -> Active while SERVER_HEARTBEATs are current, back to WaitingForServer with every valve in SAFE_STATE (1, open) when they stop for HEARTBEAT_TIMEOUT_MS, and when an OTA starts. A command to SAFE_STATE is always obeyed; leaving it needs Active. Losing the ESP32's 3.3 V fails the same way: the NMOS gate drops and 5 V reaches the driver. Boot is 0 (closed), from the first instruction of setup(), as the gate pull-up holds it before the firmware runs. Position is streamed in both states.
Prototype: pins are hardcoded below; two valves, actuator/sensor ids 1 and 2. The server's [boards.ball_valve_board] lists them as active_connectors [1, 2]. Networking, heartbeats, logs, firmware hash and Ethernet OTA match the hotfire boards (Actuator_Hotfire, common/).
Definition at line 53 of file main.cpp.
Referenced by probeW5500Version(), and setup().
|
static |
|
static |
|
static |
Definition at line 117 of file main.cpp.
Referenced by pollNetwork().
|
static |
Definition at line 83 of file main.cpp.
Referenced by sendPositions().
|
static |
|
staticconstexpr |
Definition at line 59 of file main.cpp.
Referenced by readPositionCounts().
|
staticconstexpr |
Definition at line 62 of file main.cpp.
Referenced by processActuatorCommands(), and setAllValvesSafe().
|
static |
|
static |
|
static |
|
static |
|
static |
Definition at line 79 of file main.cpp.
Referenced by findValve(), initializeValves(), sendPositions(), setAllValvesSafe(), and setup().