Define new pin in marlin

Hey all. What would it take to define a new digital output pin in marlin on the mini Rambo which simply inverts the logic of an existing pin?

Explanation: the vfd for my spindle runs on 12v logic, and has unique pins for fwd, rev, and reset (enable). These pins are pulled high by a pull-up internal to the vfd. They are activated by shorting to ground. So, pulling reset low disables the spindle. Fwd low turns forward, rev low turns in reverse. I am using an optoisolator to step up the 5v signal to 12v. The opto inverts the outputs. So in order to turn on reverse, I need to simultaneously set fwd high and rev low (I don’t know what happens if both are pulled low. When both are left high the spindle won’t turn, regardless of enable state. I assume one is set as default, or it won’t turn at all).

Right now fwd and enable work perfectly. But in order to get reverse working, I need a new output that is simply configured to be the opposite state as the SPINDLE-DIR pin.

I know enough coding in order to do this, but I don’t know WHERE in marlin to put it. I assume I would define a new pin in pins_minirambo…but where would I put the if statement. If spindle_dir is HIGH, rev_pin is LOW, and vice versa.

Is it actually more complex than this, or can it be done relatively easily?

Thanks!

Note: I’m not a Marlin developer, only a hack that ocassionally builds Marlin for his own machines.

I think you’d need to do that as code you write yourself, I don’t think there’s an easy way to define a pin that is opposite a different pin. If I"m wrong, then I’m sure the more competent developers will be along shortly to point out the error of my ways.

Wouldn’t it be easier to just use an inverting gate or even just daisy chain the opto , and thus do the logic level flip in hardware?

This is the point of my question. If it’s “a few lines of code” and a compile, then it’s easier than sourcing (albeit cheap) hardware I don’t have. If it’s REALLY involved on the software side, then you are absolutely right and the hardware approach IS easier

Don’t these align with M3, M4, M5 gcode commands? Are you sure they’re not already linked to pins for PWM spindle control? If so, you can probably just invert the existing pin rather than have to assign a new one.

I don’t think there is a way to invert an arbitrary pin, but for spindles there is SPINDLE_INVERT_DIR within Configuration_adv.h.

The same general approach is used for the stepper DIR pins via INVERT_X_DIR for example. It doesn’t change pin polarity in general, but the firmware has implemented the ability to change polarity for that specific pin.

Edit: Oops, I didn’t read the question carefully. This doesn’t help.

Within spindle_laser.cpp on or around line 168, perhaps you can hack an extra assignment to a pin.

In this example I’ve chosen pin 45 which I pulled from thin air as an example.

Find-in-files appears to show that this is the only important place where SPINDLE_DIR_PIN is used, so it should be sufficient to accomplish your complementary output.

4 Likes

Brilliant, exactly what I was looking for. I will test and report.

Thanks!

THIS FORUM IS ABSOLUTELY INCREDIBLE!

Works perfectly!

2 Likes

And, there’s the appearance of a wizzard!

That’s still a possibility, I started reading that- but since the request is solved will back burner that education.

Jamie’s a rock star.
Really happy that it’s working for you.

To close the book on this, yes, the pins are linked to the m3, m4, m5 commands. and these are exactly the commands I am using. However m3 and m4 toggle the same pin (spindle_dir) in Marlin…HIGH for forward, LOW for reverse (or vise versa, dont remember the exact logic). However for my specific model of VFD, I need a unique pin for each m3 AND m4, since the VFD has a unique FWD (LOW=FWD, HIGH=OFF) and REV (LOW=REV, HIGH=OFF) pin. Hence Jaime’s solution was perfect as it just took another unused DO pin and made it the opposite of the native SPINDLE_DIR.

Now I can consider the ER style automatic tool changer like the RapidChange system: https://rapidchangeatc.com/

1 Like

I’ll second that. He’s the only person I’ve ever met who has been featured on Hackaday.

Link?

When you get that working, please post a build thread here!

Oh no, I was just talking about buying that system, not developing my own. Having a working reverse is a pre-requisite. I could probably do it, but no reason to reinvent the wheel! :stuck_out_tongue_winking_eye: