REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
mul_calibration.cpp
Go to the documentation of this file.
1#include "block/mul_calibration.h"
2
3#include <algorithm>
4#include <bitset>
5
6#include "carrier/carrier.h"
7#include "carrier/cluster.h"
8#include "block/mblock.h"
9
10#include <ArduinoEigenDense.h>
11
12using Eigen::MatrixXd;
13using Eigen::VectorXd;
14using Eigen::Matrix4d;
15using Eigen::Vector4d;
16using Eigen::Matrix;
17using Eigen::Vector;
18
19
20UnitResult setup_calibration(platform::Cluster* cluster, blocks::MBlock* mblock, blocks::UBlock::Transmission_Mode mode, float lhs, float rhs) {
21 auto offset = static_cast<uint32_t>(mblock->slot) * 8;
22 cluster->reset(entities::ResetAction::CIRCUIT_RESET);
23 for (auto idx = 0u; idx < 4; ++idx) {
24 auto lhs_lane = idx * 2;
25 auto rhs_lane = idx * 2 + 1;
26 cluster->add_constant(mode, lhs_lane, lhs, offset + lhs_lane);
27 cluster->add_constant(mode, rhs_lane, rhs, offset + rhs_lane);
28 }
29 TRY_TRUE(cluster->write_to_hardware());
30 delay(10);
31 return UnitResult::ok();
32}
33
34MulGainOffset vec_to_gain_offset(const Vector4d& x) {
35 MulGainOffset current;
36 auto gain = current.gain = x[0];
37 if (std::fabs(gain) < 1e-8)
38 gain = 1.0;
39 current.offset_x = x[1] / gain;
40 current.offset_y = x[2] / gain;
41 current.offset_z = x[3] - gain * current.offset_x * current.offset_y;
42 return current;
43}
44
45void prepare_calibration_data(VectorXd& x_vec, double x_min, double x_max, size_t x_samples,
46 VectorXd& y_vec, double y_min, double y_max, size_t y_samples) {
47 const size_t C = x_samples * y_samples;
48 x_vec.resize(C);
49 y_vec.resize(C);
50
51 const double x_step = (x_samples > 1) ? (x_max - x_min) / (x_samples - 1) : 0.0;
52 const double y_step = (y_samples > 1) ? (y_max - y_min) / (y_samples - 1) : 0.0;
53
54 size_t sample_idx = 0;
55 for (size_t ix = 0; ix < x_samples; ++ix) {
56 const double x = (x_samples > 1) ? x_min + ix * x_step : 0.5 * (x_min + x_max);
57 for (size_t iy = 0; iy < y_samples; ++iy) {
58 const double y = (y_samples > 1) ? y_min + iy * y_step : 0.5 * (y_min + y_max);
59 x_vec[sample_idx] = x;
60 y_vec[sample_idx] = y;
61 ++sample_idx;
62 }
63 }
64}
void prepare_calibration_data(VectorXd &x_vec, double x_min, double x_max, size_t x_samples, VectorXd &y_vec, double y_min, double y_max, size_t y_samples)
MulGainOffset vec_to_gain_offset(const Vector4d &x)
UnitResult setup_calibration(platform::Cluster *cluster, blocks::MBlock *mblock, blocks::UBlock::Transmission_Mode mode, float lhs, float rhs)
Definition mode.h:14