PID -Hardware needed for a software fix

Oh dam, that’s too funny.

The last place I worked we had a military project going and the industrial designers were having a hell of a time doing research, our firewall wouldn’t allow “gun” or “bullet” to come through. Too Strict. So they worked from there phones for a lot of it.

My company blocks all the gun related stuff too. I guess it’s part of being owned by a company based out of California.

They also ‘don’t allow’ guns in the office even though there’s no 30-06 or 30-07 signs on the door.

Our company made sure to put up the 30.06 and 30.07 the very moment they could. We arent even supposed to carry pocket knives.

Okay, it’s here. I hope to plug it in this weekend and see if I can melt it or at least make it bleed magic smoke. If not then hopefully we can do some cool stuff! I am soooo close to finished with a new set of MP3DP parts so hopefully I can wrap that up and cut the new parts with this little beast. Super clean, looks quality, fingers crossed.

 

Don’t touch it!

New MP3DP parts? Way to bury the lead.

It is tortuously slow going on the printer parts. Very subtle changes. I have been doing little things on it for months. Every time I try to do a major change I realize why I did it the way I originally did in the first place…Right now I am working on a new extruder mount, maybe move the XZ end stops, then that wraps it up. It isn’t very different but I couldn’t bring myself to build more of the same ones.

I’ve built something similar a while ago in order to control the speed of my DIY air purifier. I guess it would be the same principle here.

Basically, you need to sense the zero crossings of the mains power, analyze it with an arduino and send it back to a triac. Then you can control the speed using PWM and program some speed variations in your code if you want.

I made a schematics here and there are some explanations. Sorry, it’s all in French:

What could possibly go wrong…

[attachment file=52746]

1 Like

So fr so good test code works and drives a wagner heat gone on low, High pulls more than 10A I don’t remember what low pulls but I know it was just within the board specs. So now to figure out the best way to plug it into Marlin. I am assuming like the laser to get a 5V pwm. Pretty excited. This week has been full of fun excitement and one threat of a lawsuit…perfect.

Get the speed under control, then add a nano or micro in the loop for rpm sensing and pid control, like the one we talked about here, https://www.v1engineering.com/forum/topic/arduinopid/

unsigned int i;

void setup()
{
pinMode(3, OUTPUT);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

void loop() {

for(i=240;i>20;i–)
{
analogWrite(3,i);
Serial.println(i);
delay(20);
}

delay(1000);

for(i=20;i<240;i++)
{
analogWrite(3,i);
Serial.println(i);
delay(20);
}

}

 

Although I would rather mount the optical eye under the plastic cover at the top, there is space near the bearing if I remember right. I think I have all the hardware to try it, cross your fingers. I promise I will tell you if I get shocked…

That code runs on the nano, and pin 3 controls the output?

So you will need to set up these things on the Arduino:

  1. a way to set the desired speed
  2. a way to sense the current speed
  3. a PID loop to take the error, and generate an output value.
    Adding a separate interface for controlling the gains and enabling the anti-chatter feature can come right after this works.

There will be some nuance in each one of these. If you can pull any part from someone else’s attempts, that will save some trouble. Each one of these can sort of be worked on independently. If you keep the serial debug, you should print the state of each of these components, so you can find errors quicker.

For part 1) you can read in PWM. Can you configure Marlin to use PPM (servos) or even better something like a serial or i2c connection? It would be more accurate, which would help to keep a steady speed. PWM will be moving a little bit due to noise. PPM will only be marginally better.

For part 3) The Kp, Ki, and Kd will need to be tuned. Also good is a max integrator. Also also, you need to reset it at the right time, so I won’t build up when it’s not in control. Two common gotchas in PID.

This is a great project for you to stretch your software skills. A little science, a little original code, and some clear subsystems. As usual, let me know if you hit a wall, or if you want me to review something.

Whoa whoa whoa, slow your role. Step 2 basic “dumb” speed control. Step 3 add in the screen and speed sense optical. Step 4 the PID equations, step 5 take over the world, if step 5 fails just hope I don’t get shocked too many times.

I’m just stoked that I could find it since I order it early October.

Really the next step is how does it handle the heat. That heat sink got pretty hot but I am pretty sure the heat gun and fan really took it to it’s limits. The dewalt shouldn’t, fingers crossed.

I forgot to order one of those oled screens so I have to figure out how to use one of the other ones I have. I think the 2004 style will be best no library to add and it has a encoder with 2 buttons.

Marlin 2.0, making things easy.

 

//#define SPINDLE_LASER_ENABLE
#if ENABLED(SPINDLE_LASER_ENABLE)

 

#define SPINDLE_LASER_ENABLE_INVERT false // set to “true” if the on/off function is reversed
#define SPINDLE_LASER_PWM true // set to true if your controller supports setting the speed/power
#define SPINDLE_LASER_PWM_INVERT true // set to “true” if the speed/power goes up when you want it to go slower
#define SPINDLE_LASER_POWERUP_DELAY 5000 // delay in milliseconds to allow the spindle/laser to come up to speed/power
#define SPINDLE_LASER_POWERDOWN_DELAY 5000 // delay in milliseconds to allow the spindle to stop
#define SPINDLE_DIR_CHANGE true // set to true if your spindle controller supports changing spindle direction
#define SPINDLE_INVERT_DIR false
#define SPINDLE_STOP_ON_DIR_CHANGE true // set to true if Marlin should stop the spindle before changing rotation direction

 

/**
* The M3 & M4 commands use the following equation to convert PWM duty cycle to speed/power
*
* SPEED/POWER = PWM duty cycle * SPEED_POWER_SLOPE + SPEED_POWER_INTERCEPT
* where PWM duty cycle varies from 0 to 255
*
* set the following for your controller (ALL MUST BE SET)
*/

 

#define SPEED_POWER_SLOPE 118.4
#define SPEED_POWER_INTERCEPT 0
#define SPEED_POWER_MIN 5000
#define SPEED_POWER_MAX 30000 // SuperPID router controller 0 - 30,000 RPM

 

//#define SPEED_POWER_SLOPE 0.3922
//#define SPEED_POWER_INTERCEPT 0
//#define SPEED_POWER_MIN 10
//#define SPEED_POWER_MAX 100 // 0-100%
#endif

 

Well on second thought. That math is just going to need to get reversed on the other end…That might actually get in the way.

Pretty sure that equation works out to,

pwm value = (desired RPM-intercept) * (1/Slope)

That’s fine, reverse it on the other end easy enough, the rest seems to check out. Finally easy math and not all over the place.

The enable pin could come in handy to stop the PID?

Rambo pins-

#define SPINDLE_LASER_PWM_PIN 45 // MUST BE HARDWARE PWM
#define SPINDLE_LASER_ENABLE_PIN 31 // Pin should have a pullup!
#define SPINDLE_DIR_PIN 32

That equation is used for making the speed easier for the “user” so they can put in 20k and think they are getting 20k. PWM still only has 0-255, and that’s not terribly accurate to read on the other end. So it’s a start, but you’re not going to get 20,000 written on your display, it will be 20,136 and suddenly change to 20,925 and back. Something to think about. It sure would be easier if Marlin could just send “S20000” digitally to the slave Arduino. For now. Leave in the equation, and let you dumb speed control just transfer that 0-255 right to the output.

You don’t want direction, right? I’m not sure what that will enable.

An enable would be a great idea. Just so that a little noise on the line won’t try to spin up the router to 1krpm. You would definitely reset the PID in disable. You would also reset it if the sensor was measuring zero speed, because that would mean someone forgot to flip the switch. Reset won’t stop the P component, just the I and D parts.

So PWM will only go in increments of 1, so 255 levels of speed only? If that is the case it won’t be the end of the world but I wouldn’t be happy until there was another way.

Jeez reading pwm in is not so straight forward, rising and falling…

I figure the display will read actual RPM unless you are in “manual mode” setting the rpm with a knob. Or Desired and actual RPM on screen would be cool.

I hoped to use M05 to kill the enable pin and M03 should trigger an enable. It would just be used as a master switch. Right now it does register but the value bounces back and forth, gotta dig deeper on that one. M05 seems to switch it though.

Use pulseIn:

Now that I think about it, it’s 0-1023… Is that right? I am also thinking it will have 1-2% noise, but that might be pessimistic too.

AnalogWrite is 0-255. It’s probably 256.

PulseIn looks better, I read it wrong initially. That also gives me a little better control of how often to check the value. In over my head but learning, I hope. Took out the element of danger and unplugged the mains…I’m a sissy.