1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#ifdef __PLAT_LINUX__
//#define GPIO_LOGGING // Full GPIO and Positional Logging
#include "../../inc/MarlinConfig.h"
#include "../shared/Delay.h"
#include "hardware/IOLoggerCSV.h"
#include "hardware/Heater.h"
#include "hardware/LinearAxis.h"
#include <stdio.h>
#include <stdarg.h>
#include <thread>
#include <iostream>
#include <fstream>
extern void setup();
extern void loop();
// simple stdout / stdin implementation for fake serial port
void write_serial_thread() {
for (;;) {
for (std::size_t i = usb_serial.transmit_buffer.available(); i > 0; i--) {
fputc(usb_serial.transmit_buffer.read(), stdout);
}
std::this_thread::yield();
}
}
void read_serial_thread() {
char buffer[255] = {};
for (;;) {
std::size_t len = _MIN(usb_serial.receive_buffer.free(), 254U);
if (fgets(buffer, len, stdin))
for (std::size_t i = 0; i < strlen(buffer); i++)
usb_serial.receive_buffer.write(buffer[i]);
std::this_thread::yield();
}
}
void simulation_loop() {
Heater hotend(HEATER_0_PIN, TEMP_0_PIN);
Heater bed(HEATER_BED_PIN, TEMP_BED_PIN);
LinearAxis x_axis(X_ENABLE_PIN, X_DIR_PIN, X_STEP_PIN, X_MIN_PIN, X_MAX_PIN);
LinearAxis y_axis(Y_ENABLE_PIN, Y_DIR_PIN, Y_STEP_PIN, Y_MIN_PIN, Y_MAX_PIN);
LinearAxis z_axis(Z_ENABLE_PIN, Z_DIR_PIN, Z_STEP_PIN, Z_MIN_PIN, Z_MAX_PIN);
LinearAxis extruder0(E0_ENABLE_PIN, E0_DIR_PIN, E0_STEP_PIN, P_NC, P_NC);
#ifdef GPIO_LOGGING
IOLoggerCSV logger("all_gpio_log.csv");
Gpio::attachLogger(&logger);
std::ofstream position_log;
position_log.open("axis_position_log.csv");
int32_t x,y,z;
#endif
for (;;) {
hotend.update();
bed.update();
x_axis.update();
y_axis.update();
z_axis.update();
extruder0.update();
#ifdef GPIO_LOGGING
if (x_axis.position != x || y_axis.position != y || z_axis.position != z) {
uint64_t update = _MAX(x_axis.last_update, y_axis.last_update, z_axis.last_update);
position_log << update << ", " << x_axis.position << ", " << y_axis.position << ", " << z_axis.position << std::endl;
position_log.flush();
x = x_axis.position;
y = y_axis.position;
z = z_axis.position;
}
// flush the logger
logger.flush();
#endif
std::this_thread::yield();
}
}
int main() {
std::thread write_serial (write_serial_thread);
std::thread read_serial (read_serial_thread);
#ifdef MYSERIAL0
MYSERIAL0.begin(BAUDRATE);
SERIAL_ECHOLNPGM("x86_64 Initialized");
SERIAL_FLUSHTX();
#endif
Clock::setFrequency(F_CPU);
Clock::setTimeMultiplier(1.0); // some testing at 10x
HAL_timer_init();
std::thread simulation (simulation_loop);
DELAY_US(10000);
setup();
for (;;) {
loop();
std::this_thread::yield();
}
simulation.join();
write_serial.join();
read_serial.join();
}
#endif // __PLAT_LINUX__
|