Skip to content

Commit 2ef6562

Browse files
committed
added pulse active state tracking to avoid sending pulses when stopping pulse output that was already stopped
1 parent bd2fb59 commit 2ef6562

File tree

9 files changed

+80
-15
lines changed

9 files changed

+80
-15
lines changed

adamController.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,21 +392,37 @@ void adamController::set_pulse_duty(int16_t output, float duty) {
392392

393393
void adamController::set_pulse_percent(int16_t output, int16_t duty_percent) {
394394
float duty = float(duty_percent) / 100.0;
395-
Serial.println(duty);
395+
//Serial.println(duty);
396396
adamController::set_pulse_duty(output, duty);
397397
}
398398

399399
void adamController::start_pulse_output(int16_t output) {
400400
adamController::write_holding_register(CH0_ABSOLUTE_PULSE + output * 2, 0);
401+
// adamController::printBin(pulseState);
402+
pulseState = pulseState | bitmask[output]; // track which pulses are set high
403+
// adamController::printBin(pulseState);
401404
}
402405

403406
// Cannot stop the pulse directly, but can write it to do 1 more absolute pulse
404407
void adamController::stop_pulse_output(int16_t output) {
405-
adamController::write_holding_register(CH0_ABSOLUTE_PULSE + output * 2, 1);
408+
// adamController::printBin(pulseState);
409+
if (pulseState & bitmask[output]) {
410+
// Serial.print("pulse output: ");
411+
// Serial.print(output);
412+
// Serial.println(" stopped");
413+
pulseState = pulseState & not_bitmask[output]; // remove high bit from disabled pulses
414+
adamController::write_holding_register(CH0_ABSOLUTE_PULSE + output * 2, 1);
415+
} else {
416+
// Serial.print("pulse output: ");
417+
// Serial.print(output);
418+
// Serial.println(" not active");
419+
}
420+
// adamController::printBin(pulseState);
406421
}
407422

408423

409424

425+
410426
int16_t adamController::set_DAC_analog_output(int outputNum, uint16_t outputVal) {
411427
int16_t response = -1;
412428

adamController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class adamController {
156156
char moduleName[32] = { "ADAM-xxxxA" };
157157
bool modbusConnected = false;
158158

159-
159+
uint8_t pulseState = 0b00000000;
160160

161161
uint8_t bitmask[8] = {
162162
0b00000001,

examples/adam6024_example/adamController.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,21 +392,37 @@ void adamController::set_pulse_duty(int16_t output, float duty) {
392392

393393
void adamController::set_pulse_percent(int16_t output, int16_t duty_percent) {
394394
float duty = float(duty_percent) / 100.0;
395-
Serial.println(duty);
395+
//Serial.println(duty);
396396
adamController::set_pulse_duty(output, duty);
397397
}
398398

399399
void adamController::start_pulse_output(int16_t output) {
400400
adamController::write_holding_register(CH0_ABSOLUTE_PULSE + output * 2, 0);
401+
// adamController::printBin(pulseState);
402+
pulseState = pulseState | bitmask[output]; // track which pulses are set high
403+
// adamController::printBin(pulseState);
401404
}
402405

403406
// Cannot stop the pulse directly, but can write it to do 1 more absolute pulse
404407
void adamController::stop_pulse_output(int16_t output) {
405-
adamController::write_holding_register(CH0_ABSOLUTE_PULSE + output * 2, 1);
408+
// adamController::printBin(pulseState);
409+
if (pulseState & bitmask[output]) {
410+
// Serial.print("pulse output: ");
411+
// Serial.print(output);
412+
// Serial.println(" stopped");
413+
pulseState = pulseState & not_bitmask[output]; // remove high bit from disabled pulses
414+
adamController::write_holding_register(CH0_ABSOLUTE_PULSE + output * 2, 1);
415+
} else {
416+
// Serial.print("pulse output: ");
417+
// Serial.print(output);
418+
// Serial.println(" not active");
419+
}
420+
// adamController::printBin(pulseState);
406421
}
407422

408423

409424

425+
410426
int16_t adamController::set_DAC_analog_output(int outputNum, uint16_t outputVal) {
411427
int16_t response = -1;
412428

examples/adam6024_example/adamController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class adamController {
156156
char moduleName[32] = { "ADAM-xxxxA" };
157157
bool modbusConnected = false;
158158

159-
159+
uint8_t pulseState = 0b00000000;
160160

161161
uint8_t bitmask[8] = {
162162
0b00000001,

examples/adam6052_example/adam6052_example.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This example shows:
1313
1414
Note: In order to use the PULSE settings (pulse high and pulse low registers), the output must be set to PULSE mode using the ADAM.NET utility. This will prevent the output from being used
1515
as a typical DO output, and you must use the stop_pulse(uint16_t outputNum); method to stop this output (note, the stop_pulse() function will trigger one additional pulse before stopping due to limitations in the
16-
MODBUS command set)
16+
MODBUS command set) -> Now added feature to prevent pulses being added when stop method is called while PULSE state is already off
1717
1818
*/
1919
#include "adamController.h"
@@ -91,6 +91,7 @@ void setup() {
9191
adam6052.set_pulse_frequency(200); // pulse frequency is global using this library. If use case exists for different frequencies this could be modified after
9292
adam6052.set_pulse_duty(0x00, 0.3); //(output, duty)
9393
adam6052.set_pulse_percent(0x01, 100);
94+
adam6052.stop_pulse_output(0);
9495
adam6052.start_pulse_output(1);
9596
adam6052.start_pulse_output(0);
9697
}
@@ -117,7 +118,7 @@ void loop() {
117118
adam6052.set_pulse_percent(0, percent);
118119
adam6052.set_pulse_duty(1, duty);
119120
adam6052.set_coil(2, coilstate);
120-
delay(1000);
121+
delay(1300);
121122

122123
while (counter_one == 0) {
123124
//adam6052.set_coils(0b00000000);
@@ -128,7 +129,7 @@ void loop() {
128129
adam6052.stop_pulse_output(0);
129130
adam6052.stop_pulse_output(1);
130131
delay(2000);
131-
adam6052.set_coils(0xFF);
132+
adam6052.set_coils(0b11111100);
132133
counter_one = -1;
133134
}
134135
if (counter_one <= 0) counter_one = 50;

examples/adam6052_example/adamController.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,21 +392,37 @@ void adamController::set_pulse_duty(int16_t output, float duty) {
392392

393393
void adamController::set_pulse_percent(int16_t output, int16_t duty_percent) {
394394
float duty = float(duty_percent) / 100.0;
395-
Serial.println(duty);
395+
//Serial.println(duty);
396396
adamController::set_pulse_duty(output, duty);
397397
}
398398

399399
void adamController::start_pulse_output(int16_t output) {
400400
adamController::write_holding_register(CH0_ABSOLUTE_PULSE + output * 2, 0);
401+
// adamController::printBin(pulseState);
402+
pulseState = pulseState | bitmask[output]; // track which pulses are set high
403+
// adamController::printBin(pulseState);
401404
}
402405

403406
// Cannot stop the pulse directly, but can write it to do 1 more absolute pulse
404407
void adamController::stop_pulse_output(int16_t output) {
405-
adamController::write_holding_register(CH0_ABSOLUTE_PULSE + output * 2, 1);
408+
// adamController::printBin(pulseState);
409+
if (pulseState & bitmask[output]) {
410+
// Serial.print("pulse output: ");
411+
// Serial.print(output);
412+
// Serial.println(" stopped");
413+
pulseState = pulseState & not_bitmask[output]; // remove high bit from disabled pulses
414+
adamController::write_holding_register(CH0_ABSOLUTE_PULSE + output * 2, 1);
415+
} else {
416+
// Serial.print("pulse output: ");
417+
// Serial.print(output);
418+
// Serial.println(" not active");
419+
}
420+
// adamController::printBin(pulseState);
406421
}
407422

408423

409424

425+
410426
int16_t adamController::set_DAC_analog_output(int outputNum, uint16_t outputVal) {
411427
int16_t response = -1;
412428

examples/adam6052_example/adamController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class adamController {
156156
char moduleName[32] = { "ADAM-xxxxA" };
157157
bool modbusConnected = false;
158158

159-
159+
uint8_t pulseState = 0b00000000;
160160

161161
uint8_t bitmask[8] = {
162162
0b00000001,

src/adamController.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,21 +392,37 @@ void adamController::set_pulse_duty(int16_t output, float duty) {
392392

393393
void adamController::set_pulse_percent(int16_t output, int16_t duty_percent) {
394394
float duty = float(duty_percent) / 100.0;
395-
Serial.println(duty);
395+
//Serial.println(duty);
396396
adamController::set_pulse_duty(output, duty);
397397
}
398398

399399
void adamController::start_pulse_output(int16_t output) {
400400
adamController::write_holding_register(CH0_ABSOLUTE_PULSE + output * 2, 0);
401+
// adamController::printBin(pulseState);
402+
pulseState = pulseState | bitmask[output]; // track which pulses are set high
403+
// adamController::printBin(pulseState);
401404
}
402405

403406
// Cannot stop the pulse directly, but can write it to do 1 more absolute pulse
404407
void adamController::stop_pulse_output(int16_t output) {
405-
adamController::write_holding_register(CH0_ABSOLUTE_PULSE + output * 2, 1);
408+
// adamController::printBin(pulseState);
409+
if (pulseState & bitmask[output]) {
410+
// Serial.print("pulse output: ");
411+
// Serial.print(output);
412+
// Serial.println(" stopped");
413+
pulseState = pulseState & not_bitmask[output]; // remove high bit from disabled pulses
414+
adamController::write_holding_register(CH0_ABSOLUTE_PULSE + output * 2, 1);
415+
} else {
416+
// Serial.print("pulse output: ");
417+
// Serial.print(output);
418+
// Serial.println(" not active");
419+
}
420+
// adamController::printBin(pulseState);
406421
}
407422

408423

409424

425+
410426
int16_t adamController::set_DAC_analog_output(int outputNum, uint16_t outputVal) {
411427
int16_t response = -1;
412428

src/adamController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class adamController {
156156
char moduleName[32] = { "ADAM-xxxxA" };
157157
bool modbusConnected = false;
158158

159-
159+
uint8_t pulseState = 0b00000000;
160160

161161
uint8_t bitmask[8] = {
162162
0b00000001,

0 commit comments

Comments
 (0)