2

I have steppers stepping during an interrupt timer at 50 and have all my code working between the interrupts until I tried reading serial commands more than one character long.

I'm getting dropped bytes so my strings are missing a letter every 4-5 chars. I researched all day to try and figure out a solution but have come up with nothing. If I don't use an interrupt my stepper stops for 2 seconds reading a one char serial input as a string.

My goal is to have a remote control app sending speed commands. I need help working this problem out.

https://sourceforge.net/p/open-slider/code/ci/master/tree/OpenSliderFirmware/

String incomingString = "";
if (Serial.available() > 0) {
    incomingString = Serial.readString();
    Serial.println(incomingString);
}

Using Accelstepper library

interrupt:

//Interrupt Timer1
void ISR_stepperManager() {
    Slide.runSpeed();
    Xaxis.runSpeed();
    Yaxis.runSpeed(); 
}
4
  • 1
    Are you using a software (bit-banged?) serial port? What is the serial data rate, and the rate of stepper interrupts? Commented Dec 2, 2016 at 8:14
  • interrupt is 50ms. baud is 115200 on serial0 USB. Eventually I will have a software serial running on pins 2&3 I believe for bluetooth (if I can figure it out) I'm still trying to find someone who can explain how Marlin firmware accomplishes stepping and serial at the same time reliably. Marlin reads 1 char at a time which I'm going to switch back to so it doesn't block other things. I thought they used an interrupt to drive the steppers but I'm not sure of that anymore. I'm using a 2560 and a RAMPS. I'm still a bit of a novice on advanced stuff. Commented Dec 2, 2016 at 20:30
  • You should first try reducing the serial baud rate: 115200 baud is about 80 micro seconds per byte - so if your stepper interrupt service routine takes more than 80us you immediately risk missing a serial char. Try 9600 baud (or slower), that's about a millisecond per byte. If you're not sure how long the ISR takes and have a scope available, make the ISR output a 1 to a port at the start of the ISR, and 0 at the end, and look at the width of the pulse and how much it varies. For bit-banged you will definitely have to slow the serial data rate down. Commented Dec 3, 2016 at 15:27
  • Speaking of scopes, I've always wanted one but don't want to spend $200 for a *cheap one. Is the DSO 138 for $20 any good for what you are suggesting? Commented Dec 4, 2016 at 2:28

2 Answers 2

1

Quick answer: you don't if the interrupt timer is cutting in too often.

I resolved the problem by using a variable interrupt timer and a step multiplier. Basically the steps are called every time the timer interrupts instead of checking millis inside the interrupt function. This solved many issues. The speed of the stepper is now controlled by the interrupt timer. This gave me more free cycles to fully read the incoming serial without corruption and improved efficiency. Calling more steps per cycle when doing over 4k steps/s also improved efficiency requiring less cycles for a high rate of steps.

The serial is processed one char per cycle to prevent blocking.

Overall, if you are using serial and an interrupt timer, any interrupt happening < 100us you should be cautious how much code you are running during the interrupt. It will cause issues with incoming serial and user inputs. A few lines of code in a 25us timer interrupt, incoming serial will not function.

Sign up to request clarification or add additional context in comments.

Comments

0

i'm not sure if it will help to your problem, but i saw along the time that the String type is not safe to use when other things need to happened. i prefer to use char array and read one char at a time.

while(Serial.available())
{
 data[x] = Serial.read();
 x++;
}

i'm finding it much more reliable.

hope it's help!

1 Comment

Unfortunately, using that in my code would not make a difference because the while loop would be interrupted by the timer interrupt which was the issue I was having. Thanks for the suggestion though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.