STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
main.cpp File Reference
#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"
Include dependency graph for main.cpp:

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

Enumeration Type Documentation

◆ BallValveState

enum class BallValveState : uint8_t
strong
Enumerator
WaitingForServer 
Active 

Definition at line 110 of file main.cpp.

Function Documentation

◆ findValve()

Valve * findValve ( uint8_t actuator_id)
static

Definition at line 143 of file main.cpp.

References valves.

Referenced by processActuatorCommands().

◆ flushLogs()

void flushLogs ( )
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().

◆ getBoardStateForHeartbeat()

daq::BoardState getBoardStateForHeartbeat ( )
static

Definition at line 199 of file main.cpp.

References daq::ACTIVE, Active, daq::SETUP, state, and WaitingForServer.

Referenced by sendBoardHeartbeatTo().

◆ initializeValves()

void initializeValves ( )
static

Definition at line 368 of file main.cpp.

References digitalWrite(), HIGH, INPUT, OUTPUT, pinMode(), and valves.

Referenced by setup().

◆ loop()

◆ otaStart()

void otaStart ( void * ,
uint32_t  )
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().

◆ pollNetwork()

◆ probeW5500Version()

void probeW5500Version ( )
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().

◆ processActuatorCommands()

void processActuatorCommands ( const std::vector< daq::ActuatorCommand > & commands)
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().

◆ processIncomingPacket()

◆ readPositionCounts()

uint32_t readPositionCounts ( const Valve & v)
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().

◆ sendBoardHeartbeatTo()

void sendBoardHeartbeatTo ( IPAddress dest_ip,
int dest_port )
static

◆ sendPositions()

◆ setAllValvesSafe()

void setAllValvesSafe ( const char * reason)
static

Definition at line 164 of file main.cpp.

References SAFE_STATE, setValve(), and valves.

Referenced by loop(), and otaStart().

◆ setState()

void setState ( BallValveState new_state)
static

Definition at line 131 of file main.cpp.

References HF_LOG, HF_LOGLN, Serial, state, and stateName().

Referenced by loop(), and processIncomingPacket().

◆ setup()

◆ setValve()

void setValve ( Valve & v,
uint8_t new_state,
const char * reason )
static

◆ stateName()

const char * stateName ( BallValveState s)
static

Definition at line 120 of file main.cpp.

References Active, and WaitingForServer.

Referenced by setState().

Variable Documentation

◆ board_id

uint8_t board_id = 0
static

Definition at line 88 of file main.cpp.

◆ dns

IPAddress dns(192, 168, 2, 1) ( 192 ,
168 ,
2 ,
1  )
static

◆ ETH_CS

int ETH_CS = 5
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().

◆ ETH_MISO

int ETH_MISO = 19
staticconstexpr

Definition at line 56 of file main.cpp.

Referenced by setup().

◆ ETH_MOSI

int ETH_MOSI = 23
staticconstexpr

Definition at line 54 of file main.cpp.

Referenced by setup().

◆ eth_present

bool eth_present = false
static

Definition at line 102 of file main.cpp.

Referenced by loop(), and setup().

◆ ETH_SCLK

int ETH_SCLK = 18
staticconstexpr

Definition at line 55 of file main.cpp.

Referenced by setup().

◆ g_verbose

bool g_verbose = true

Definition at line 105 of file main.cpp.

◆ gateway

IPAddress gateway(0, 0, 0, 0) ( 0 ,
0 ,
0 ,
0  )
static

◆ last_position_read_ms

unsigned long last_position_read_ms = 0
static

Definition at line 117 of file main.cpp.

Referenced by pollNetwork().

◆ last_server_heartbeat_ms

unsigned long last_server_heartbeat_ms = 0
static

Definition at line 116 of file main.cpp.

◆ lastHeartbeatMillis

unsigned long lastHeartbeatMillis = 0
static

Definition at line 118 of file main.cpp.

◆ mac

byte mac[6]
static

Definition at line 89 of file main.cpp.

◆ net

BoardNet::State net
static

Definition at line 98 of file main.cpp.

◆ NUM_VALVES

uint8_t NUM_VALVES = sizeof(valves) / sizeof(valves[0])
staticconstexpr

Definition at line 83 of file main.cpp.

Referenced by sendPositions().

◆ otaServer

◆ POSITION_SAMPLES

int POSITION_SAMPLES = 16
staticconstexpr

Definition at line 59 of file main.cpp.

Referenced by readPositionCounts().

◆ SAFE_STATE

uint8_t SAFE_STATE = 1
staticconstexpr

Definition at line 62 of file main.cpp.

Referenced by processActuatorCommands(), and setAllValvesSafe().

◆ serverIP

IPAddress serverIP(192, 168, 2, HOTFIRE_SERVER_IP_OCTET_4) ( 192 ,
168 ,
2 ,
HOTFIRE_SERVER_IP_OCTET_4  )
static

◆ serverPort

const int serverPort = HOTFIRE_SERVER_PORT
static

Definition at line 95 of file main.cpp.

◆ state

Definition at line 115 of file main.cpp.

◆ subnet

IPAddress subnet(255, 255, 255, 0) ( 255 ,
255 ,
255 ,
0  )
static

◆ udp

EthernetUDP udp
static

Definition at line 96 of file main.cpp.

◆ udpListenPort

const int udpListenPort = 5005
static

Definition at line 94 of file main.cpp.

◆ valves

Valve valves[]
static
Initial value:
= {
{1, 1, 25, 32, 0},
{2, 2, 14, 35, 0},
}

Definition at line 79 of file main.cpp.

Referenced by findValve(), initializeValves(), sendPositions(), setAllValvesSafe(), and setup().