RF-Positioning-System V0.1
by Max Heinekamp
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1
11
12// Include required libraries
13#include <Arduino.h>
14#include "config.h"
15#include "Vrekrer_scpi_parser.h"
16#include "scpi.h"
17#include "flash.h"
18#include "esp_system.h"
19
20//Sored in RTC memory to persist across soft resets, but not power cycles. Used to determine if the system has just booted or if it has been reset after boot.
21RTC_DATA_ATTR bool hasResetAfterBoot = false;
22
23//Sored in RTC memory to persist across soft resets, but not power cycles. Counts boots since the
24//device was last fully power-cycled, so repeated involuntary resets (e.g. brownouts, watchdog
25//triggers) during otherwise-normal operation are visible via DIAGnostic:SYStem?.
26RTC_DATA_ATTR uint32_t bootCount = 0;
27
28
29//Create Flash object
30FLASH flash_memory;
31
32// Cretate global pointers for SCPI and Stepper objects
33SCPI* scpi_instrument = nullptr;
34STEPPER* stepper1 = nullptr;
35STEPPER* stepper2 = nullptr;
36
37const char* resetReasonToString(esp_reset_reason_t reason) {
38 switch (reason) {
39 case ESP_RST_UNKNOWN: return "UNKNOWN";
40 case ESP_RST_POWERON: return "POWERON";
41 case ESP_RST_EXT: return "EXT";
42 case ESP_RST_SW: return "SW";
43 case ESP_RST_PANIC: return "PANIC";
44 case ESP_RST_INT_WDT: return "INT_WDT";
45 case ESP_RST_TASK_WDT: return "TASK_WDT";
46 case ESP_RST_WDT: return "WDT";
47 case ESP_RST_DEEPSLEEP: return "DEEPSLEEP";
48 case ESP_RST_BROWNOUT: return "BROWNOUT";
49 case ESP_RST_SDIO: return "SDIO";
50 default: return "UNMAPPED";
51 }
52}
53
54
55void setup() {
56 bootCount++;
57
58 pinMode(POWER_GOOD_PIN, INPUT); // Not currently used
59
60 flash_memory.initFlashMemory();
61
62 scpi_instrument = new SCPI();
63
64 //Retrieve ethernet settings from flash memory
65 int ethernet_reset_pin = flash_memory.preferences.getInt("ETHERNET-RESET-PIN", ETHERNET_RESET_PIN);
66
67 //Wait for power to stabilize and then reset ethernet module
68 delay(500);
69 pinMode(ethernet_reset_pin, OUTPUT);
70 digitalWrite(ethernet_reset_pin, LOW);
71 delay(100);
72 digitalWrite(ethernet_reset_pin, HIGH);
73 delay(500);
74
75 // Retrieve stepper settings from flash memory
76 int address1 = flash_memory.preferences.getInt("STEPPER1-ADDRESS", STEPPER1_ADDRESS);
77 int address2 = flash_memory.preferences.getInt("STEPPER2-ADDRESS", STEPPER2_ADDRESS);
78 int step_pin1 = flash_memory.preferences.getInt("MOTOR1-STEP-PIN", MOTOR1_STEP_PIN);
79 int enable_pin1 = flash_memory.preferences.getInt("MOTOR1-ENABLE-PIN", MOTOR1_ENABLE_PIN);
80 int step_pin2 = flash_memory.preferences.getInt("MOTOR2-STEP-PIN", MOTOR2_STEP_PIN);
81 int enable_pin2 = flash_memory.preferences.getInt("MOTOR2-ENABLE-PIN", MOTOR2_ENABLE_PIN);
82
83 // Initialize stepper objects with the retrieved settings
84 stepper1 = new STEPPER(address1, step_pin1, enable_pin1);
85 stepper2 = new STEPPER(address2, step_pin2, enable_pin2);
86
87 // Start serial communication for USB interface
88 Serial.begin(9600);
89 // Start serial interface for stepper motor driver
90 Serial1.begin(9600);
91 //Setup SCPI communication
92 scpi_instrument->setup();
93
94 //Setup Stepper motor
95 stepper1->setup();
96 stepper2->setup();
97
98 esp_reset_reason_t resetReason = esp_reset_reason();
99 Serial.println("Test123");
100 delay(500);
101 Serial.println("Test1234");
102 delay(500);
103 Serial.printf("Reset reason: %d\n", esp_reset_reason());
104
105 if ((resetReason == ESP_RST_POWERON || resetReason == ESP_RST_BROWNOUT || resetReason == ESP_RST_UNKNOWN || resetReason == ESP_RST_EXT) && hasResetAfterBoot == false) {
106 Serial.println("Cold/unstable boot detected, scheduling hardware reset...");
107 Serial.flush(); // Ensure message is sent before reset
108
109 hasResetAfterBoot = true;
110
111 delay(5000); // wait for power to stabilize
112
113 esp_restart();
114
115 // // Perform hardware reset by pulling EN pin low via GPIO
116 // pinMode(RESET_CONTROL_PIN, OUTPUT);
117 // digitalWrite(RESET_CONTROL_PIN, LOW);
118 // delay(500); // Hold low long enough for reset
119 // digitalWrite(RESET_CONTROL_PIN, HIGH); // Release (safety, though we're gone by now)
120
121 }
122}
123
124void loop() {
125 //Check Serial for incoming SCPI commands
126 scpi_instrument->processSerial();
127 //Check Ethernet for incoming SCPI commands
128 scpi_instrument->processEthernet();
129
130}
131
132/*
133import pyvisa
134rm = pyvisa.ResourceManager('@py')
135EthInstrument = rm.open_resource('TCPIP0::192.168.0.13::5025::SOCKET')
136EthInstrument.write_termination = '\n'
137EthInstrument.read_termination = '\n'
138EthInstrument.query("*IDN?")
139*/
This class handles access to the flash memory of the ESP32. It is also used to initialize the flash m...
Definition flash.h:19
This class handles the SCPI communication with the instrument. It is responsible for parsing SCPI com...
Definition scpi.h:26
The STEPPER class is used to manage stepper motor instances. It provides methods to control the motor...
Definition stepper.h:20
Configuration file for the entire project.
Handles the flash memory for the ESP32.
This file contains the functions and classes related to SCPI communication.