-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmachine.c
170 lines (161 loc) · 5.17 KB
/
machine.c
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
164
165
166
167
168
169
170
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include "machine.h"
void backlash_calc_backlash( cmdline_t *current_line, cmdline_t *machine, float backlash_x, float backlash_y, float backlash_z, float delay_x, float delay_y, float delay_z ) {
/*
* check if feedrate has change
*/
if ( current_line->F.argument_bool ) {
machine->F.argument = current_line->F.argument;
}
/*
* calculate the x achses delay distance from delay_x and current feedrate
*/
float delay_x_distance = ( machine->F.argument / 60 ) * delay_x;
/*
* calculate the x achses delay distance from delay_x and current feedrate
*/
float delay_y_distance = ( machine->F.argument / 60 ) * delay_y;
/*
* calculate the x achses delay distance from delay_x and current feedrate
*/
float delay_z_distance = ( machine->F.argument / 60 ) * delay_z;
/*
* update x coordinate if moved
*/
if ( current_line->X.argument_bool ) {
float tmp_X = current_line->X.argument;
if ( current_line->X.argument > machine->X.argument ) {
current_line->X.argument = current_line->X.argument + backlash_x + delay_x_distance;
}
else if ( current_line->X.argument < machine->X.argument ) {
current_line->X.argument = current_line->X.argument - backlash_x - delay_x_distance;
}
machine->X.argument = tmp_X;
}
/*
* update y coordinate if moved
*/
if ( current_line->Y.argument_bool ) {
float tmp_Y = current_line->Y.argument;
if ( current_line->Y.argument > machine->Y.argument ) {
current_line->Y.argument = current_line->Y.argument + backlash_y + delay_y_distance;
}
else if ( current_line->Y.argument < machine->Y.argument ) {
current_line->Y.argument = current_line->Y.argument - backlash_y - delay_y_distance;
}
machine->Y.argument = tmp_Y;
}
/*
* update z coordinate if moved
*/
if ( current_line->Z.argument_bool ) {
float tmp_Z = current_line->Y.argument;
if ( current_line->Z.argument > machine->Z.argument ) {
current_line->Z.argument = current_line->Z.argument + backlash_z + delay_z_distance;
}
else if ( current_line->Z.argument < machine->Z.argument ) {
current_line->Z.argument = current_line->Z.argument - backlash_z - delay_z_distance;
}
machine->Z.argument = tmp_Z;
}
}
void backlash_reset_line( cmdline_t *line ) {
line->F.argument = 0;
line->F.argument_bool = false;
line->G.argument = 0;
line->G.argument_bool = false;
line->M.argument = 0;
line->M.argument_bool = false;
line->P.argument = 0;
line->P.argument_bool = false;
line->S.argument = 0;
line->S.argument_bool = false;
line->X.argument = 0;
line->X.argument_bool = false;
line->Y.argument = 0;
line->Y.argument_bool = false;
line->Z.argument = 0;
line->Z.argument_bool = false;
}
void backlash_pharse_line( char *input, cmdline_t *line ) {
char *ret;
backlash_reset_line( line );
if ( ( ret = strstr( input, ";") ) ) {
printf("%s", input );
return;
}
if ( ( ret = strstr( input, "G") ) ) {
*ret++;
line->G.argument = atof( ret );
line->G.argument_bool = true;
}
if ( ( ret = strstr( input, "M") ) ) {
*ret++;
line->M.argument = atof( ret );
line->M.argument_bool = true;
}
if ( ( ret = strstr( input, "X") ) ) {
*ret++;
line->X.argument = atof( ret );
line->X.argument_bool = true;
}
if ( ( ret = strstr( input, "Y") ) ) {
*ret++;
line->Y.argument = atof( ret );
line->Y.argument_bool = true;
}
if ( ( ret = strstr( input, "Z") ) ) {
*ret++;
line->Z.argument = atof( ret );
line->Z.argument_bool = true;
}
if ( ( ret = strstr( input, "S") ) ) {
*ret++;
line->S.argument = atof( ret );
line->S.argument_bool = true;
}
if ( ( ret = strstr( input, "F") ) ) {
*ret++;
line->F.argument = atof( ret );
line->F.argument_bool = true;
}
if ( ( ret = strstr( input, "P") ) ) {
*ret++;
line->P.argument = atof( ret );
line->P.argument_bool = true;
}
}
void backlash_print_line( cmdline_t *line ) {
if ( line->G.argument_bool ) {
printf("G%.0f ", line->G.argument );
}
if ( line->M.argument_bool ) {
printf("M%.0f ", line->M.argument );
}
if ( line->X.argument_bool ) {
printf("X%.3f ", line->X.argument );
}
if ( line->Y.argument_bool ) {
printf("Y%.3f ", line->Y.argument );
}
if ( line->Z.argument_bool ) {
printf("Z%.3f ", line->Z.argument );
}
if ( line->P.argument_bool ) {
printf("P%.0f ", line->P.argument );
}
if ( line->S.argument_bool ) {
printf("S%.3f ", line->S.argument );
}
if ( line->F.argument_bool ) {
printf("F%.0f ", line->F.argument );
}
printf("\n");
}
void backlash_cpy_cmdline( cmdline_t *dest, cmdline_t *src ) {
memcpy( dest, src, sizeof( cmdline_t ) );
}