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 delay(3000);
46
47 msg::Log::get().sinks.add_Serial();
48 msg::Log::get().sinks.add(&msg::StartupLog::get());
49
50 bus::init();
51
52 LOG_ALWAYS(dist::ident());
53 LOGV("Flash image (%d bytes) sha256 sum: %s\n", loader::flashimage::len(),
54 loader::flashimage::sha256sum().to_string().c_str());
55
57
58 // Initialize carrier board
59 // TODO, _ERROR_OUT_ shall not be used, see #116
60 LOG(ANABRID_DEBUG_INIT, "Detecting and Initializing carrier board...");
61 auto &carrier_ = carrier::Carrier::get();
62 auto carrier_init_result = carrier_.init();
63 if (!carrier_init_result) {
64 LOG_ERROR("Error initializing carrier board.");
65 leds::indicate_error();
66 }
67
68 // all LEDs on: Both visual self-test and showing that now ethernet search takes place.
69 leds::set(0xFF);
70
71 LOG(ANABRID_DEBUG_INIT, "Starting up Ethernet...");
72 net::register_settings();
73 auto &netconf = net::StartupConfig::get();
74 int net_error = netconf.begin_ip();
75 if (!net_error && netconf.enable_mdns) {
76 LOG_ALWAYS("Starting MDNS");
77 netconf.begin_mdns();
78 }
79 if (!net_error && netconf.enable_jsonl)
80 msg::ApplicationServer::get().begin();
81 //if (!net_error && netconf.enable_webserver)
82 // web::LucidacWebServer::get().begin();
83
84 // Initialize things related to runs
85 // TODO: I think mode control must be initialized somewhere here?
86 /*
87 if (!mode::FlexIOControl::init(mode::DEFAULT_IC_TIME, mode::DEFAULT_OP_TIME)) {
88 LOG_ERROR("Error initializing FlexIO mode control.");
89 leds::indicate_error();
90 }
91 */
92
93 auto &reg = msg::handlers::Registry::get();
94 reg.init(carrier_); // registers all commonly known messages
95
96 msg::Broker::get(); // init json lines protocol
97
98 //skip initialization if carrier init was not successful
99 if (!carrier_init_result) return;
100
101 // Initialize data acquisition before it's used later
102 daq::init();
103 if (!daq::calibrate(carrier_)) {
104 LOG_ALWAYS("Warning: Error during DAQ calibration. Machine will continue with reduced accuracy.");
105 }
106
107 // Reset overload line
108 carrier_.reset(entities::ResetAction::OVERLOAD_RESET);
109
110 // Set computer into IC mode at startup
111 mode::RealManualControl::to_ic();
112
113 // Run calibration
114 LOG(ANABRID_DEBUG_INIT, "Executing self-calibration, this may take a few moments...");
115 auto mblock_calibrate_result = carrier_.calibrate_m_blocks();
116 if (!mblock_calibrate_result) {
117 // Right now, self-calibration is still not well tested and the error handling
118 // suggest it fails despite it most likely does not. Therefore, do not make
119 // the output so harsh.
120 //
121 // LOG_ERROR("Error during self-calibration. Machine will continue with reduced accuracy.");
122 // leds::indicate_error();
123 //
124 // instead, do this:
125
126 LOG_ALWAYS(mblock_calibrate_result.err_value().c_str());
127 LOG_ALWAYS("Info: Startup self-calibration apparently did not succeed. The calibration scheme is by "
128 "default not used in this build anyway.");
129
130 // For a quick fix, reset machine here because calibration routines exit
131 // early which *do* result in a machine in an unusable state.
132 carrier_.reset(entities::ResetAction::CIRCUIT_RESET | entities::ResetAction::OVERLOAD_RESET);
133 (void)carrier_.write_to_hardware();
134 }
135
136 // Done.
137 LOG(ANABRID_DEBUG_INIT, "Initialization done.");
138
139 static_plugin::setup();
140
141 platform::CalibrationBase::begin();
142
143 // visual effect to show that booting has been done
144 leds::ease_out();
145}
146
147FASTRUN
148void loop() {
149 //auto &netconf = net::StartupConfig::get();
150
151 //if (netconf.enable_jsonl)
152 msg::ApplicationServer::get().loop();
153
154 //if (Serial.available()) {
155 // auto input_stream = std::make_shared<transport::DelimitedInputStream<Stream*>>(&Serial);
156 // auto output_stream = std::make_shared<transport::DelimitedMessageOutputStream<Stream*>>(&Serial);
157 // transport::Transport transport(std::move(input_stream), output_stream, std::move(output_stream));
158 // msg::Broker::get().process(transport);
159 //}
160
161 run::RunManager::get().try_run_next(carrier::Carrier::get());
162 static_plugin::loop();
163}
void setup()
FASTRUN void loop()
void check_and_log_crash()
Logs out what has been captured by the Teensy CrashReport tooling, cf.