-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
165 lines (133 loc) · 5.52 KB
/
main.c
File metadata and controls
165 lines (133 loc) · 5.52 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h> // For sleep/usleep (remove for embedded)
#include "motor_control.h"
#include "sensor.h"
#include "measurement.h"
#include "display.h"
#include "system_config.h"
// System state structure
typedef struct {
motor_control_t motor;
sensor_t sensor;
measurement_t measurement;
display_t display;
uint32_t system_time; // System time in clock cycles
bool system_running;
bool scanning_complete;
bool measurement_ready;
} system_state_t;
// Initialize system
void system_init(system_state_t *sys) {
if (sys == NULL) return;
// Initialize all subsystems
motor_init(&sys->motor);
sensor_init(&sys->sensor);
measurement_init(&sys->measurement, CALIBRATION_FACTOR);
display_init(&sys->display);
// Initialize system state
sys->system_time = 0;
sys->system_running = true;
sys->scanning_complete = false;
sys->measurement_ready = false;
// Start motor in forward direction
motor_set_direction(&sys->motor, MOTOR_FORWARD);
motor_start(&sys->motor);
}
// Main system loop
void system_main_loop(system_state_t *sys) {
if (sys == NULL) return;
bool object_detected_prev = false;
while (sys->system_running) {
// Update system time (in real system, this would be from hardware timer)
sys->system_time += MS_TO_CLOCK_CYCLES(UPDATE_INTERVAL_MS);
// Update motor
motor_update(&sys->motor);
uint32_t current_position = motor_get_position(&sys->motor);
// Update sensor
sensor_update(&sys->sensor, sys->system_time);
bool object_detected = sensor_is_object_detected(&sys->sensor);
// Handle measurement logic
if (object_detected && !object_detected_prev) {
// Object just detected - start measurement
measurement_start(&sys->measurement, current_position);
printf("Object detected at position: %u steps\n", current_position);
} else if (object_detected && object_detected_prev) {
// Object still detected - update measurement
measurement_update(&sys->measurement, current_position, true);
} else if (!object_detected && object_detected_prev) {
// Object just lost - stop measurement
measurement_stop(&sys->measurement, current_position);
sys->measurement_ready = true;
float length = measurement_get_length_mm(&sys->measurement);
printf("Measurement complete: %.2f mm\n", length);
}
object_detected_prev = object_detected;
// Update display
float current_measurement = 0.0f;
if (sys->measurement_ready) {
current_measurement = measurement_get_length_mm(&sys->measurement);
} else if (sys->measurement.measurement_active) {
current_measurement = measurement_get_length_mm(&sys->measurement);
}
display_update(&sys->display, current_measurement, object_detected);
// Check if scanning is complete (motor returned to home)
if (sys->motor.state == MOTOR_STOP &&
sys->motor.position == MOTOR_HOME_POSITION &&
!object_detected) {
if (sys->measurement_ready && !sys->scanning_complete) {
sys->scanning_complete = true;
printf("Scanning complete. Final measurement: %.2f mm\n",
measurement_get_length_mm(&sys->measurement));
// Keep displaying result for a few seconds
// Then reset for next measurement
// In real system, this could be triggered by a button press
}
}
// Check if motor reached end and needs to return
if (sys->motor.state == MOTOR_BACKWARD &&
sys->motor.position == MOTOR_HOME_POSITION) {
// Scanning cycle complete, ready for next measurement
if (sys->scanning_complete) {
// Reset for next measurement
measurement_reset(&sys->measurement);
sensor_reset(&sys->sensor);
sys->measurement_ready = false;
sys->scanning_complete = false;
// Start new scan
motor_set_direction(&sys->motor, MOTOR_FORWARD);
motor_start(&sys->motor);
}
}
// Simulate delay (in real system, this would be handled by timer interrupts)
// For embedded systems, remove this and use interrupt-driven timing
#ifdef SIMULATION
usleep(UPDATE_INTERVAL_MS * 1000); // Convert ms to microseconds
#endif
}
}
// Main entry point
int main(void) {
system_state_t system;
printf("Object Measurement System Initializing...\n");
printf("Calibration factor: %.2f steps/mm\n", CALIBRATION_FACTOR);
// Initialize system
system_init(&system);
printf("System started. Beginning measurement cycle...\n");
printf("Motor moving forward. Waiting for object detection...\n");
// Run main loop
system_main_loop(&system);
return 0;
}
// Interrupt handlers (for embedded implementation)
// These would be called by FPGA interrupt controller
void timer_interrupt_handler(void) {
// Update system timers
// Trigger periodic updates
}
void sensor_interrupt_handler(void) {
// Handle sensor state changes
// Update sensor reading immediately
}