Skip to content

Commit 59cf0ce

Browse files
committed
plasmac: fix arc voltage ring buffer wrap off-by-one errors
Ensure buffer indices wrap at BUFFERSIZE - 1 and prevent invalid reads at index -1.
1 parent bedd956 commit 59cf0ce

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

src/hal/components/plasmac.comp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ A plasma cutting table control component for use with the LinuxCNC 2.10.
77

88
=== VERSION
99

10-
011
10+
012
1111

1212
=== SUMMARY
1313

@@ -442,7 +442,7 @@ float read_arc_voltage_buffer(int caller, int size, int index, float target, flo
442442
}
443443
sum += arc_voltage_buffer[index];
444444
if(index == 0){
445-
index = BUFFERSIZE;
445+
index = BUFFERSIZE - 1;
446446
}else{
447447
index--;
448448
}
@@ -824,7 +824,7 @@ FUNCTION(_) {
824824
torch_time += fperiod;
825825
/* add the current arc voltage to the arc voltage buffer */
826826
arc_voltage_buffer[arc_voltage_buffer_index] = arc_voltage_out;
827-
if (arc_voltage_buffer_index == BUFFERSIZE) {
827+
if (arc_voltage_buffer_index == BUFFERSIZE - 1) {
828828
arc_voltage_buffer_index = 0;
829829
}else{
830830
arc_voltage_buffer_index++;
@@ -845,13 +845,13 @@ FUNCTION(_) {
845845
if(!arc_ok_out){
846846
if(arc_voltage_out >= arc_ok_low &&
847847
arc_voltage_out <= arc_ok_high &&
848-
!read_arc_voltage_buffer(READ_OK, ok_sampler_samples, arc_voltage_buffer_index - 1, arc_voltage_out, ok_sample_threshold)){
848+
!read_arc_voltage_buffer(READ_OK, ok_sampler_samples, (arc_voltage_buffer_index == 0) ? BUFFERSIZE - 1 : arc_voltage_buffer_index - 1, arc_voltage_out, ok_sample_threshold)){
849849
arc_ok_out = TRUE;
850850
arc_lost_timer = arc_lost_delay;
851851
}
852852
/* stable voltages required to maintain arc_ok */
853853
}else if((arc_voltage_out < arc_ok_low || arc_voltage_out > arc_ok_high) &&
854-
read_arc_voltage_buffer(READ_OK, ok_sampler_samples, arc_voltage_buffer_index - 1, target_volts, ok_sample_threshold) >= ok_sampler_samples){
854+
read_arc_voltage_buffer(READ_OK, ok_sampler_samples, (arc_voltage_buffer_index == 0) ? BUFFERSIZE - 1 : arc_voltage_buffer_index - 1, target_volts, ok_sample_threshold) >= ok_sampler_samples){
855855
if(arc_lost_timer <= 0){
856856
arc_ok_out = FALSE;
857857
}else{
@@ -1727,7 +1727,7 @@ FUNCTION(_) {
17271727
if((current_velocity * 60 > requested_feed_rate * 0.999) || laser_mode){
17281728
if(thc_auto && use_auto_volts){
17291729
/* test the arc voltage buffer for a stable voltage */
1730-
double target = read_arc_voltage_buffer(READ_THC, thc_sampler_samples, arc_voltage_buffer_index - 1, arc_voltage_out, thc_sample_threshold);
1730+
double target = read_arc_voltage_buffer(READ_THC, thc_sampler_samples, (arc_voltage_buffer_index == 0) ? BUFFERSIZE - 1 : arc_voltage_buffer_index - 1, arc_voltage_out, thc_sample_threshold);
17311731
if(target){
17321732
// should we use average voltage or current voltage???
17331733
// target_volts = arc_voltage_out; // current voltage

0 commit comments

Comments
 (0)