Moving code from ESP8266 to ESP32

Image
A while ago I made a mashup of Dan Royer's code CNC 2 Axis Demo with my own code for trapezoidal motion stepper and servo control for ESP8266.

I assumed porting the code to the ESP32 would be trivial, and that was true for the most part: changes like library name being Wifi.h instead of Wifi8266.h were not a problem. UDP now does not like multicharacter writes but you can use print instead. So far so good.

However, when it came to the interrupt code I was stuck with the stepper interrupt causing an exception sometimes. And to make things weirder, the servo interrupt worked flawlessly (both of them had the IRAM_ATTR directive if you ask me).

Going little by little, I could narrow down the culprit to a floating point operation during the interrupt, that would cause problems sometimes but not always. Browsing around I found this post. Where the solution was simple: do not use floats within the interrupt routines but doubles. The reason was the float calculation would be performed by…

Testing sinusoidal S-curves

My main goal here was to keep a simple mechanism to be easy to calculate and able to be included into 8-bit firmware like Marlin. Which by the way already has support for s-curves for quite a while (at least on this mod for Ultimaker).

So the goal is to have a function f(t) that will map the speed at each given moment but in a way that the maximum speed will be reached at the same time as it would using a uniform acceleration, so the rest of the math and motion planning is not affected. In doing so, the peak acceleration is going to be higher than that when using uniform acceleration. However, if that is a problem it is ok to reduce the maximum acceleration so the system can work flawlessly.

I have tested a couple of functions, both based on sinusoidal functions, the former is:
f(t)=V*(1-cos(PI*(t/t1))/2 (where V is max speed and t1 marks the end of the S-curve ramping up). It is the dotted-green curve below. The shape of the acceleration for this speed pattern is the dotted-pink curve.


The second candidate is the solid-red curve, that is smoother on the edges but has a higher slope in the middle. That is the function f(t)=V*(2*PI*t/t1-sin(2*PI*t/t1))/2/PI. The shape of the acceleration for this speed pattern is the solid-blue curve. 

So I did some tests on a sample one-axis machine, running dcservo and moving a bit of liquid that should give us an idea of the vibration each one of the motion patterns will create (I am using the ramp of the trapezoidal motion as control case here).

The following video shows the result, running a random sequence of movements, all with the same maximum speed. Acceleration is modulated in a way that for each case, the same amount of time (t1) is needed for reaching the desired maximum speed.

 

While the code is not thoroughly tested but more alpha code, it is available here.



Comments

Popular posts from this blog

VFD control with Arduino using RS485 link

4xiDraw: Another pen plotter

Software I2C for Arduino