Improvement to the PID control

42 views
Skip to first unread message

lionel...@gmail.com

unread,
May 13, 2018, 1:23:43 PM5/13/18
to R2C2 - Support
Hello,

Just in case other people are still playing around with their R2C2 boards: I managed to dramatically improve the PID controller algorithm by adding a filter to the D term.
The problem with the previous implementation of the PID algorithm (at least for me) was that the temperature reading is an integer, so the temperature monitoring makes some jumps of 1 degree every second or so. This is a problem when processing the derivative, as it gives 0 most of the time and then a very large value. Using a IIR filter (similar to that used for the temperature reading) solves the issue to some extent. Ideally the temperature reading should be a double precision variable, but that would change too many things in the system.
With this filter I managed to setup my PID control parameters in 5 min, it is much more stable now! I just wished I had a look at that 4 years ago...

If I can access the git repository I will upload the commit. Otherwise, the changes are as follow:

in pid2.h, add a field into the PID structure:
      double Dmem;

in pid2.c, in PID_Compute, locate the lines:

      double P = error * p->kp;
      double I = p->ITerm * p->ki * SampleTimeInSec;
      double D = (p->lastInput - input) * p->kd / SampleTimeInSec;

then add:
    
      p->Dmem = ((p->Dmem * 15) + D) / 16;
      D = p->Dmem;

That should work.


Jeremie Francois

unread,
May 14, 2018, 2:59:36 AM5/14/18
to R2C2 - Support
Yes, this is an excellent fix! I hope you will make it to github -- I just hope the repository is not dead!


lionel...@gmail.com

unread,
May 14, 2018, 2:43:21 PM5/14/18
to R2C2 - Support
Good point.
Just in case I don't have any answer from the admins, here is the firmware file that I compiled with the improvements.
firmware.bin

lionel...@gmail.com

unread,
May 19, 2018, 5:02:05 AM5/19/18
to R2C2 - Support
Hello all,

I made some more changes in the code (probably not as clean as it should) so that now the temperature is read with a double precision variable. The result is that the PID values are much more precise, especially the derivative term. The control is much smoother now, and there are much less problems to setup the PID control. As a matter of fact, I can now set the power limits to 0 and 100 %, this does no longer matter.

I attach the firmware with the modifications, this is from the devel branch of the github repository. Still waiting to get someone to grant me the permission to upload my  commits...

Lionel
firmware.bin

lionel...@gmail.com

unread,
May 19, 2018, 4:32:48 PM5/19/18
to R2C2 - Support
Just to show off a bit, here is a screen capture of the Repetier window during a print. The temperature fluctuates by less than 0.1 degree C. For info, I am using P = 6, I = 5 and D = 10.
:)
temperature.png

lionel...@gmail.com

unread,
May 20, 2018, 4:52:57 AM5/20/18
to R2C2 - Support
More testing done and there are a couple of problems so be careful if you use my mods:
- the print head temperature goes crazy at the end of a print
- the temperature starts to fluctuate quite a lot when the print is fast

I'm looking at this.

lionel...@gmail.com

unread,
May 20, 2018, 12:27:46 PM5/20/18
to R2C2 - Support
I found another problem with the code: when the print is launched, the PID values are never taken into account. I suppose that the heat pattern is not refreshed because the queue is always busy, but I am not sure if this is true because I do not understand how this all works. I am having a look at this, but any help would be welcome.

nc...@laposte.net

unread,
May 20, 2018, 12:39:40 PM5/20/18
to R2C2 Community
Thank you very much for all the work you put into this!
Unfortunately I'm not really  into the code part so I can't help you a lot... I'd love to build a new hardware based on r2c2 though to bring 3D printing forward!

Nicolas Chaslot
Drone Maker, Vamatis

Le 20 mai 2018 18:28, lionel...@gmail.com a écrit :
I found another problem with the code: when the print is launched, the PID values are never taken into account. I suppose that the heat pattern is not refreshed because the queue is always busy, but I am not sure if this is true because I do not understand how this all works. I am having a look at this, but any help would be welcome.

--
You received this message because you are subscribed to the Google Groups "R2C2 - Support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to r2c2---suppor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

lionel...@gmail.com

unread,
May 20, 2018, 3:52:53 PM5/20/18
to R2C2 - Support
I solved the issue: the problem was due to the max and min power limits being over ridden to 35 and 15 while the object was printing. I do not know where this over ridding is coded, so I had to hard code the limits to 0 and 100 (the parameters I and J in M130 and M133 are now ignored). This is not a problem though as the regulation is working really well. I attach the firmware file.
Note that the extruder temperature still goes crazy at the end of a print, which may damage the extruder head. BE careful if you use this...
firmware.bin

Bryan Lambrecht

unread,
May 20, 2018, 5:48:12 PM5/20/18
to r2c2---...@googlegroups.com
Hey Nicolas, 

I did a mini-remix version of the R2C2 quite a few years ago, bumped it to 4 layers and changed out a decent amount of parts. It works very well and gets less noise on the analog lines, better thermal handling (no heat sinks needed), and the BOM cost was reduced a bit as well. 

It would be pretty easy to keep the same base and add a bit of functionality or even shrink it down much further or take advantage of new MFG methods. 
I'm not huge into the firmware side of things, but pretty darn good with Altium since I use it everyday for work. 

What did you have in mind as far as new hardware design goals?

I remember some one was trying to add some PID settings for the heaters but came to the conclusion that it needs more precision out of the ADC to accurately implement the PID controls and get the best performance out of it. I think the hardware is there to do it, just need to re-code a large part of it or something like that, and coding is more of a hassle than fun for me :( 

-Bryan 

To unsubscribe from this group and stop receiving emails from it, send an email to r2c2---support+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "R2C2 - Support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to r2c2---support+unsubscribe@googlegroups.com.

nc...@laposte.net

unread,
May 20, 2018, 6:28:25 PM5/20/18
to R2C2 Community
Hi Bryan,

Well in my opinion a true problem in 3D printing today is that even though there are a lot of cool boards out there, most of them aren't really designed by professional hardware designers and lack the reliability/design efficiency they deserve as a component that drive such critical loads (I mean, a fucked up thermistor and you could get an awesome flame out of your printer)

Even though new firmwares solve a lot of this, I still think that deserves to be treated better.

When I had in mind to redesign a new r2c2 board, I had a few things in mind:

- wifi connectivity on top of USB to allow easier use of the printer without tether.
- touch screen (or just screen and controls) for standalone printing
- detachable drivers to allow easier Change of fucked up ones (even though that is really rare on that board, I think newer drivers are really good, like tmcxxxx ones)
- smaller size
- additional sensor connections (filament run out..)

If there is enough people interested in this thread, maybe we could build something good!

Nick

Nicolas Chaslot
Drone Maker, Vamatis
To unsubscribe from this group and stop receiving emails from it, send an email to r2c2---suppor...@googlegroups.com.

Bryan Lambrecht

unread,
May 21, 2018, 11:17:38 AM5/21/18
to r2c2---...@googlegroups.com
Ya I was thinking about this a few months ago (hadn't fired up the printer in a while) and totally agree with making it more reliable as well as maybe adding smarter software/interfaces. 
Couple things I thought would be nice was. 

- move stepper divers off board and onto stepper motors more modular 
- along same lines, see about using an I2C, CAN bus or Muxing interface so we can just have one driver bus and daisy chain them, which would free up a lot of I/O and allow many more steppers as well as making cabling easier. 
- move flash from SD card to integrated chip. 
- smart sense functions and error codes (beeps) so like if the thermistor is NC or out of range, won't start print, or aborts print and beeps code. 
- bed leveling sensor or calibration 
- better cabling options, hate free wires, but don't want to lock into a single header you will have to crimp. Maybe use standard cable type (usb 3 or somthing) and just have a non standard pin-out to make it easy and cheep to hook up. 
- reduce size significantly, probably move to two sided assembly and change out connector types. 
-  try to reduce end stops to a single I/O and header, and just daisy chain them. 
- try moving heaters drivers off board, so we can use smaller headers ( low voltage/current) I had one melt on me :S 



I'm totally down to help out, I have 7 years in the field as an EE doing hardware and layout.  

On Sun, May 20, 2018 at 3:28 PM, ncx94 via R2C2 - Support <r2c2---...@googlegroups.com> wrote:
Hi Bryan,

Well in my opinion a true problem in 3D printing today is that even though there are a lot of cool boards out there, most of them aren't really designed by professional hardware designers and lack the reliability/design efficiency they deserve as a component that drive such critical loads (I mean, a fucked up thermistor and you could get an awesome flame out of your printer)

Even though new firmwares solve a lot of this, I still think that deserves to be treated better.

When I had in mind to redesign a new r2c2 board, I had a few things in mind:

- wifi connectivity on top of USB to allow easier use of the printer without tether.
- touch screen (or just screen and controls) for standalone printing
- detachable drivers to allow easier Change of fucked up ones (even though that is really rare on that board, I think newer drivers are really good, like tmcxxxx ones)
- smaller size
- additional sensor connections (filament run out..)

If there is enough people interested in this thread, maybe we could build something good!

Nick

Nicolas Chaslot
Drone Maker, Vamatis
Le 20 mai 2018 23:48, Bryan Lambrecht <bryan.l...@gmail.com> a écrit :
Hey Nicolas, 

I did a mini-remix version of the R2C2 quite a few years ago, bumped it to 4 layers and changed out a decent amount of parts. It works very well and gets less noise on the analog lines, better thermal handling (no heat sinks needed), and the BOM cost was reduced a bit as well. 

It would be pretty easy to keep the same base and add a bit of functionality or even shrink it down much further or take advantage of new MFG methods. 
I'm not huge into the firmware side of things, but pretty darn good with Altium since I use it everyday for work. 

What did you have in mind as far as new hardware design goals?

I remember some one was trying to add some PID settings for the heaters but came to the conclusion that it needs more precision out of the ADC to accurately implement the PID controls and get the best performance out of it. I think the hardware is there to do it, just need to re-code a large part of it or something like that, and coding is more of a hassle than fun for me :( 

-Bryan 

On Sun, May 20, 2018 at 9:39 AM, ncx94 via R2C2 - Support <r2c2---support@googlegroups.com> wrote:
Thank you very much for all the work you put into this!
Unfortunately I'm not really  into the code part so I can't help you a lot... I'd love to build a new hardware based on r2c2 though to bring 3D printing forward!

Nicolas Chaslot
Drone Maker, Vamatis

Le 20 mai 2018 18:28, lionel...@gmail.com a écrit :
I found another problem with the code: when the print is launched, the PID values are never taken into account. I suppose that the heat pattern is not refreshed because the queue is always busy, but I am not sure if this is true because I do not understand how this all works. I am having a look at this, but any help would be welcome.

--
You received this message because you are subscribed to the Google Groups "R2C2 - Support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to r2c2---support+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "R2C2 - Support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to r2c2---support+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "R2C2 - Support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to r2c2---support+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nicolas Chaslot

unread,
May 21, 2018, 11:54:12 AM5/21/18
to r2c2---...@googlegroups.com

Sounds like we have the same ideas.

-regarding the driver's interface, newer drivers use SPI as communication so that could free up some pins.

-for the standard connectors with non standard cabling, i'm not really into that because it can cause to much trouble if beginners try to plug things where they shouldn't.

- bltouch would be awesome for bed levelling

- i like the idea of having the drivers for the heaters out of the board. Most people including myself tend to use SSRs anyway as a board relief

To unsubscribe from this group and stop receiving emails from it, send an email to r2c2---suppor...@googlegroups.com.

lionel...@gmail.com

unread,
May 21, 2018, 2:59:15 PM5/21/18
to R2C2 - Support
Hello,
I would be interested in an upgraded version of the R2C2 board, I do a bit of electronics but if you are looking for professional layouts then don't count on me. However I would be glad to be a beta-tester and to help coding. C is not my forte but I understand enough to debug problems.
About the new board:
- the temperature ADC could be improved a bit but this may not be the biggest problem that I see. It turns out that the PID works rather well now that the code is fixed, I could get a temperature within 0.2 C for an hour, using variable print speeds even with the window opened next to the printer. A couple of bits more may help with temperature readings for lower temperatures, but that is about all.
- auto bed levelling is definitely something I would be happy to have! I have not had a look at the code that was added about this for the current R2C2 card but that is on my todo list
- I would not advise removing the SD card, it came handy on occasions to be able to access it directly (for formatting after corrupted memory occurred, and to get fast data transfer). Is there a reason to replace it with on board memory?
- I would also be interested in multiple print heads, ideally in a modular way. Not sure how to achieve this without cluttering the card with connectors, though.

Lionel

lionel...@gmail.com

unread,
May 21, 2018, 3:14:47 PM5/21/18
to R2C2 - Support
By the way, there is another interesting feature that I have been thinking about detecting extruder jams. I do not know if anything has been done yet about this, but there is a relatively easy thing we could do for that: the filament extruder usually has a ball bearing to push the filament against the teethed rod, this ball bearing stops when the filament is jammed. A rotary encoder could do just that: when the machine detects a stop of the encoder, it can initiate a purge procedure and restart where it stopped.

nc...@laposte.net

unread,
May 22, 2018, 1:05:01 AM5/22/18
to R2C2 Community
I really like the idea of the anti jamming.
But adding more sensors to the extruder makes me feel we should do a small daughter board directly near the extruder that would communicate with the main board via a standard bus like can or serial and that would control the extruder motor, encoder, thermistance etc so we'll have way less cables to mess with and a better performance (no more extra long cables for analog temp signals etc)

Also, adding the skipped step detection on every axis would be cool (and it can be done through firmware with newer stepper drivers)

Nicolas Chaslot
Drone Maker, Vamatis
Le 21 mai 2018 21:15, lionel...@gmail.com a écrit :
By the way, there is another interesting feature that I have been thinking about detecting extruder jams. I do not know if anything has been done yet about this, but there is a relatively easy thing we could do for that: the filament extruder usually has a ball bearing to push the filament against the teethed rod, this ball bearing stops when the filament is jammed. A rotary encoder could do just that: when the machine detects a stop of the encoder, it can initiate a purge procedure and restart where it stopped.

--

Nicolas Chaslot

unread,
May 28, 2019, 11:58:13 AM5/28/19
to r2c2---...@googlegroups.com
Hi guys,

Don't know if there really is someone here but it's worth trying anyway :)

After a few years of good use, I've decided to move on from my R2C2
board...
If anyone here is interested, I'll be happy to sell it for cheap :)

Nick

Triffid Hunter

unread,
May 12, 2020, 1:50:35 PM5/12/20
to r2c2---...@googlegroups.com
The 7th most recent thread is 5 years old, pretty sure this group is basically dead ;)

--
You received this message because you are subscribed to the Google Groups "R2C2 - Support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to r2c2---suppor...@googlegroups.com.

nc...@laposte.net

unread,
May 12, 2020, 2:18:20 PM5/12/20
to R2C2 Community
Yeah I'm sure it is but it was worth a shot anyway ;)

Rui Ribeiro

unread,
May 12, 2020, 5:22:12 PM5/12/20
to r2c2---...@googlegroups.com
Still here!

My board isn't in use any more, but I'm saving it for some future projects.

Rui


Nicolas Chaslot

unread,
May 13, 2020, 3:44:57 AM5/13/20
to r2c2---...@googlegroups.com

Hi Rui how are you?
It's been a long time!

Rui Ribeiro

unread,
May 13, 2020, 7:11:45 PM5/13/20
to r2c2---...@googlegroups.com
Hi Nicolas,

Everything good here in Lisbon. No stress, teleworking much more than working, but loving it!

Today I spend much more time on other kinds of development. I try to play KSP (Kerbal Space Program) and do some modeling for my "chinese 3d printer" that "just works". I've made some drones and learned how to fly those without much assistence from the "onboad flight controller".

My current hobby is to use an EUC to move around in the city (and commute to work! 3000km last year). You can see one of these machines in action in this link: https://www.youtube.com/watch?v=S72OoSUfWig

What about you?

PS: contact me directy on my e-mail: racri...@gmail.com

See you,

Rui


L. B.

unread,
May 14, 2020, 11:34:18 AM5/14/20
to r2c2---...@googlegroups.com
Hi Rui and all,

R2C2 is still up and kicking here.
Just to let you know, I found a problem a while ago in the implementation of the PID feedback loop, there was an improper rounding operation that messed up with the derivative term (if I remember well, the measurement was rounded to the nearest integer before the derivative term was processed, so it was almost always zero and the PID only had the integral and proportional terms).
I made a quick and dirty change to the code a long while ago, it is a commit on the git repo but it is a bit buggy and the temperature tends to go crazy when a print is finished. I do not have access to the repo so could not merge it. Apart from that I have been using that board for the last years, with a temperature precision of less than +/-0.5 degrees throughout the prints. So, thanks for all the efforts, everyone ;)

All the best

Lionel


You received this message because you are subscribed to a topic in the Google Groups "R2C2 - Support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/r2c2---support/lq2pMaPl2IY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to r2c2---suppor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/r2c2---support/CAP0gxK-GmdVqCvz3qEtnKr6-LUr7sQs%3DsXvZByU8OrXQnqZ2FQ%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages