REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
hybrid_controller.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 anabrid GmbH
2// Contact: https://www.anabrid.com/licensing/
3// SPDX-License-Identifier: MIT OR GPL-2.0-or-later
4
13
14#include "protocol/protocol_oob.h"
15#include "redac/calibration.h"
16#include "run/run_manager.h"
17
18#include <Arduino.h>
19
20#include <build/distributor.h>
21#include <daq/daq.h>
22#include <lucidac/front_panel_signaling.h>
23#include <lucidac/lucidac.h>
24#include <mode/mode.h>
25#include <net/auth.h>
26#include <net/settings.h>
27#include <plugin/static.h>
28#include <protocol/handler.h>
29#include <protocol/jsonl_logging.h>
30#include <protocol/jsonl_server.h>
31#include <protocol/protocol.h>
32#include <protocol/registry.h>
33#include <redac/redac.h>
34#include <utils/crash_report.h>
35#include <utils/hashflash.h>
36#include <utils/logging.h>
37
38void setup() {
39 // Initialize serial communication
40 Serial.begin(0);
41 // while (!Serial && millis() < 4000) {
42 // Wait for Serial, but not forever
43 //}
44
45 msg::Log::get().sinks.add_Serial();
46 msg::Log::get().sinks.add(&msg::StartupLog::get());
47
48 bus::init();
49
50 LOG_ALWAYS(dist::ident());
51 LOGV("Flash image (%d bytes) sha256 sum: %s\n", loader::flashimage::len(),
52 loader::flashimage::sha256sum().to_string().c_str());
53
55
56 // Initialize carrier board
57 // TODO, _ERROR_OUT_ shall not be used, see #116
58 LOG(ANABRID_DEBUG_INIT, "Detecting and Initializing carrier board...");
59 auto &carrier_ = carrier::Carrier::get();
60 if (!carrier_.init()) {
61 LOG_ERROR("Error initializing carrier board.");
62 leds::indicate_error();
63 }
64
65 // all LEDs on: Both visual self-test and showing that now ethernet search takes place.
66 leds::set(0xFF);
67
68 LOG(ANABRID_DEBUG_INIT, "Starting up Ethernet...");
69 net::register_settings();
70 auto &netconf = net::StartupConfig::get();
71 int net_error = netconf.begin_ip();
72 if (!net_error && netconf.enable_mdns) {
73 LOG_ALWAYS("mdns");
74 netconf.begin_mdns();
75 }
76 if (!net_error && netconf.enable_jsonl)
77 msg::ApplicationServer::get().begin();
78 //if (!net_error && netconf.enable_webserver)
79 // web::LucidacWebServer::get().begin();
80
81 // Initialize things related to runs
82 // TODO: I think mode control must be initialized somewhere here?
83 /*
84 if (!mode::FlexIOControl::init(mode::DEFAULT_IC_TIME, mode::DEFAULT_OP_TIME)) {
85 LOG_ERROR("Error initializing FlexIO mode control.");
86 leds::indicate_error();
87 }
88 */
89
90 auto &reg = msg::handlers::Registry::get();
91 reg.init(carrier_); // registers all commonly known messages
92
93 msg::Broker::get(); // init json lines protocol
94
95 // Initialize data acquisition before it's used later
96 daq::init();
97 if (!daq::calibrate(carrier_)) {
98 LOG_ALWAYS("Warning: Error during DAQ calibration. Machine will continue with reduced accuracy.");
99 }
100
101 // Reset overload line
102 carrier_.reset(entities::ResetAction::OVERLOAD_RESET);
103
104 // Set computer into IC mode at startup
105 mode::RealManualControl::to_ic();
106
107 // Run calibration
108 LOG(ANABRID_DEBUG_INIT, "Executing self-calibration, this may take a few moments...");
109 if (!carrier_.calibrate_m_blocks()) {
110 // Right now, self-calibration is still not well tested and the error handling
111 // suggest it fails despite it most likely does not. Therefore, do not make
112 // the output so harsh.
113 //
114 // LOG_ERROR("Error during self-calibration. Machine will continue with reduced accuracy.");
115 // leds::indicate_error();
116 //
117 // instead, do this:
118
119 LOG_ALWAYS("Info: Startup self-calibration apparently did not succeed. The calibration scheme is by "
120 "default not used in this build anyway.");
121
122 // For a quick fix, reset machine here because calibration routines exit
123 // early which *do* result in a machine in an unusable state.
124 carrier_.reset(entities::ResetAction::CIRCUIT_RESET | entities::ResetAction::OVERLOAD_RESET);
125 (void)carrier_.write_to_hardware();
126 }
127
128 // Done.
129 LOG(ANABRID_DEBUG_INIT, "Initialization done.");
130
131 static_plugin::setup();
132
133 platform::CalibrationBase::begin();
134
135 // visual effect to show that booting has been done
136 leds::ease_out();
137}
138
139void loop() {
140
141 auto &netconf = net::StartupConfig::get();
142
143 if (netconf.enable_jsonl)
144 msg::ApplicationServer::get().loop();
145
146 if (Serial.available()) {
147 auto input_stream = std::make_shared<transport::DelimitedInputStream<Stream*>>(&Serial);
148 auto output_stream = std::make_shared<transport::DelimitedMessageOutputStream<Stream*>>(&Serial);
149 transport::Transport transport(std::move(input_stream), output_stream, std::move(output_stream));
150 msg::Broker::get().process(transport);
151 }
152
153 run::RunManager::get().try_run_next(carrier::Carrier::get());
154 static_plugin::loop();
155}
void setup()
void loop()
void check_and_log_crash()
Logs out what has been captured by the Teensy CrashReport tooling, cf.