-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_tcounts_curves.cpp
146 lines (116 loc) · 3.51 KB
/
gen_tcounts_curves.cpp
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
//
//
//
#include "gen_tcounts_curves.h"
#include "defs.h"
unsigned int gen_timercounts(unsigned int *curve_table, float target_speed, float freq, float steps_per_unit, float jerk, float acc, unsigned long cpucycles, bool scurve)
{
if (scurve == true) {
Serial.print("Calculating new S_curve table for new speed (can take a while)...\n\n");
float dcount_min=1.0/(float)steps_per_unit*(float)freq/target_speed;
unsigned long dcount = constrain((unsigned long)dcount_min/10,1,50);
float cycles_half = sqrt((target_speed)/(float)jerk)*(float)freq;
float j;
float int_jerk = 0;
float acc;
float int_acc = 0;
float int_speed = 0;
float speed;
float pos;
unsigned int n_step = 0;
unsigned long temptable;
float dtime=(float)dcount/(float)freq;
float dx=1.0/(float)steps_per_unit;
unsigned long prec_step = 0;
j = (float)jerk;
for (unsigned long i=1; i<=((unsigned long)cycles_half); i=i+dcount) {
int_jerk=int_jerk+j;
acc=int_jerk*dtime;
int_acc=int_acc+acc;
speed=int_acc*dtime;
int_speed=int_speed+speed;
pos=int_speed*dtime;
if (pos>(((float)n_step+1.0)*dx)) {
n_step++;
temptable = constrain(i-prec_step-(unsigned long)cpucycles,0,65535);
curve_table[n_step-1]= (unsigned int) temptable;
prec_step = i;
//#ifdef DEBUG
// Serial.print("n_step: ");
// Serial.print(n_step);
// Serial.print(" cpu_count: ");
// Serial.print(temptable);
// Serial.print(".\n");
//#endif
}
}
j = -(float)jerk;
for (unsigned long i=(unsigned long)cycles_half+1; i<=(2*(unsigned long)cycles_half); i=i+dcount) {
int_jerk=int_jerk+j;
acc=int_jerk*dtime;
int_acc=int_acc+acc;
speed=int_acc*dtime;
int_speed=int_speed+speed;
pos=int_speed*dtime;
if (pos>(((float)n_step+1.0)*dx)) {
n_step++;
temptable = constrain(i-prec_step-(unsigned long)cpucycles,0,65535);
curve_table[n_step-1]= (unsigned int) temptable;
prec_step = i;
//#ifdef DEBUG
// Serial.print("n_step: ");
// Serial.print(n_step);
// Serial.print(" cpu_count: ");
// Serial.print(temptable);
// Serial.print(".\n");
//#endif
}
}
if (n_step == 0) {
n_step = 1;
curve_table[0] = constrain((unsigned int) dcount_min,5,65535);
}
Serial.print("Done!\n\n");
return n_step;
}
else {
Serial.print("Calculating new table for new speed (can take a while)...\n\n");
float dcount_min=1.0/(float)steps_per_unit*(float)freq/target_speed;
unsigned long dcount = constrain((unsigned long)dcount_min/10,1,50);
float cycles = target_speed/(float)acc*(float)freq;
float int_acc = 0;
float int_speed = 0;
float speed;
float pos;
unsigned int n_step = 0;
unsigned long temptable;
float dtime=(float)dcount/(float)freq;
float dx=1.0/(float)steps_per_unit;
unsigned long prec_step = 0;
for (unsigned long i=1; i<=((unsigned long)cycles); i=i+dcount) {
int_acc=int_acc+(float)acc;
speed=int_acc*dtime;
int_speed=int_speed+speed;
pos=int_speed*dtime;
if (pos>(((float)n_step+1.0)*dx)) {
n_step++;
temptable = constrain(i-prec_step-(unsigned long)cpucycles,0,65535);
curve_table[n_step-1]= (unsigned int) temptable;
prec_step = i;
//#ifdef DEBUG
// Serial.print("n_step: ");
// Serial.print(n_step);
// Serial.print(" cpu_count: ");
// Serial.print(temptable);
// Serial.print(".\n");
//#endif
}
}
if (n_step == 0) {
n_step = 1;
curve_table[0] = constrain((unsigned int) dcount_min,5,65535);
}
Serial.print("Done!\n\n");
return n_step;
}
}