Help with rotary positioning

I am working on a project modifying a rebar bender to display and limit the angle of bending of the material that is getting bent. The machine works via an electric motor that drives a gearbox that drives a large gear, which is on the center of rotation. There is a post attached to the face of the gear, offset from the center of rotation that does the bending. Currently, to set the angle of bending, there are knobs connected to bevel gears attached to the main shaft that use limit switches to stop the bender. My thought is to use a hollow rotary encoder on the main shaft connected to a display with an encoder knob to adjust the stop/limit position. There doesn't need to be any kind of return scenario, because it uses a big coil spring for getting back to it's starting point once the limit switch is hit and the motor shuts off. I need this machine to be pretty accurate, because some of the parts we bend have a +or- 0.5 degree tolerance. We can always do a little post bend tweaking, but i would like to mitigate that as much as possible. I have proven out the repeatability in its current state, so that is not a major concern. I am planning on gutting out all of the gears and knobs that control the position, so space shouldn't be an issue. My main questions are, is this a valid solution to what i am trying to achieve? What specific components will i need to make this work? I know this post is long winded, but there is a lot of information i am trying to convey. Any help that i can get will be greatly appreciated. I can try to provide any additional information or pictures or clarification. Thanks in advance.

Best advice is use a SECOND machine to strip and modify. Otherwise you have NOTHING to compare you modifications to!!!!

So you need to choose an encoder that can easily be mounted onto the machine you have. The last photo suggests around 300 degrees bend maximum? with 0.25 deg tolerance.

Hollow shaft rotary encoder is a good choice, preferable 2048ppr. Quadrature input for the electronics would then give a resolution of better than 0.1 deg.

ESP32 libraries exist for encoders with a quadrature output. Use a CYD to set a target value and then the same CYD can show the actual angular value. A Zero preset button could also be used to set bar zero point. Plus other 'stuff' as you think of it.

Retain the two ultimate limits as hardware switches into the motor controller for when things go tits up.

You'll have to consider who will be maintaining the system. If you're the guy who implements it, then leaves the company, that will be a problem. If it's your machine at home, then I guess it's your choice.

But I would be concerned regarding the safety around the machine. Guarding can not be detected by something like an ESP32 for interlocking. It is too dangerous.

Really, I would go down a commercial plc route, which has approved safety electronic modules for that. It could also provide the angle data and bending control on a single HMI display.

It'll cost a lot more but be much safer.

The machine has around 270 degrees, so good guess just from looking at a questionable quality picture at an off angle. The increments at the dial are suggestive at best. The knobs had haphazardly placed stickers with arrows on them. As far as mounting an encoder, I was planning on pulling the shaft and turning the bottom of it on a lathe to fit whichever encoder i end up going with.

That CYD definitely looks like the way to go for display and control. I do want a zeroing function, because the point of contact for the start of the bend will probably change part to part depending on the geometry and the fixturing i build.

I will be keeping the mechanical limit switch in place for the end of the stroke. There isn't one on the return, because there is a coil spring that brings it back and stops on a rubber bump stop.

I will be the one doing all the setup and building of this, but I will hand the jobs off for someone else to run. We do have an in house maintenance guy that can fix things as needed. Not a lot of maintenance on it though. Grease on the gears, brushes for the motor and a few bearings. This is not my personal machine thankfully. I work at an aerospace machine shop, hence the tight tolerances for the angles.

Safety is always a concern, so i will need to address that. There are foot pedals that start the cycle and they have a decently long lead, so the operator doesn't need to be right on top of the machine. Most of the parts we do will probably stay within the footprint of the machine, so that helps. This thing is supposed to be capable of bending 1" rebar, so it will mess you up if any body part gets in the way. I hadn't considered any kind of interlocking scenario. I will be sure to look into that. OSHA would probably like that as well. Can an ESP32 utilize some kind of optical sensor, like a light curtain or garage door opener?

Thanks for all the input. I am well versed on the mechanical side, but i don't really know what i am doing when it comes to electrical anything outside of playing with car stereos 20 years ago.

It will be as soon as you modify it!

Nah. I've been given the green light to do what i want to it. The only permanent modification would be the turning down the end of the shaft to install the encoder. All the other parts that will be removed are bolted on. For now.

Famous last words. If you can turn the shaft down, you can also make and adapter to link the shaft to the encoder. The shaft would need a flat ground into it for the set screws, anyway you choose to make the connections.

You're right. I could make an adapter, but the bottom of the shaft is almost hitting the floor of the machine. Also, I'm not sure why you said "Famous last words".

Then use a belt to remote the encoder.

My suggestion is to validate the raw stock first and determine whether it is within the specifications required to bend it accurately. I expect you will find that variations in the raw stock are magnified by the bending process and actual size.

Also, without a known starting point (zero location), how can the final position be accurate when only the endpoint is known?

As I understand it, you are trying to accurately control an endpoint without having an accurately known starting point.

All of the material we get comes with certs and is verified before it hits the floor. This is a requirement and not a suggestion, being that we are an aerospace company that does defense work. That being said, your statement is valid, because i have seen variations in some materials, depending on how they were processed. Different heat treat lots can vary and even how the material is cut can make a difference.

There was a controller that someone suggested that would allow me to set a zero from wherever. That is an important feature because depending on the parts geometry and how i design my fixturing the starting point may not be the same every time. Again, valid point.

What nobody has directly mentioned yet is the software. There are several issues. You say your maintenance 'guy' can take over once you are gone. Will he be by your side while you develop the code so he is at least as familiar with it as you are? What happens when he is gone? Do OSHA and other agencies have requirements? Will the code be backed up, with at least one copy off-site? Since you are a defence contractor, that off-site probably needs to be run by the Pentagon or similar.
I am not saying it can't or shouldn't be done, but you'll likely need professional project management and development staff versed in defence agency rules and procedures. From the sound of things, your role is idea man and not much more.

Please correct me if I'm wrong, but don't you use whatever software is used to write the code for the Arduino? I've only played with it once before and that was with a small cnc router i had. and even that was just flashing something. I've never actually written that kind of code before.

As far as my maintenance guy, he would be there for the PM or to fix anything that i cant. I did just have a conversation with him about this yesterday and he does have a little knowledge about different kinds of controllers and sensors. More than me anyway.

Not to sound cold or anything, but if and when I leave this company, any issues that occur won't be my problem anymore.

Regarding the safety side, we just hired some kind of advisors to help with resolving any safety concerns throughout the entire shop, so i will be working with them directly for my department.

All of our computer systems and servers are on site, but are locked down to be ITAR compliant. We have an IT department dedicated to maintain all of that.

I am the process development/improvement staff. I do all of the designing and manufacturing of the fixtures/tooling for my department (tool room), which includes manual mills and lathes, a 3 axis cnc mill, a cnc cylindrical grinder,a manual cylindrical grinder, a couple manual surface grinders, a sinker edm, a wire edm, and a 3D printing cell. I've been a machinist for 20 years and in aerospace for most of that. All that being said, I am well versed with manufacturing and most of the rules and specs involving all of my processes. I have to be. There is no way that you could have known any of this about me, so please don't take this as a prove you wrong moment. You don't know until you know. All of the points you made are valid, but most of them have been addressed. I am not concerned with the manufacturing side of this project, as this is what i do for a living. I really only need help with the electrical side, hence why i am in this forum, and not a manufacturing forum. I do appreciate all of the input though.

Good luck!

Actually, the learning path will lead you to write the code several times. The more you write, the more you will learn that the current path will not work and you will need a new scheme. We all have to do that with any new/fresh project.

I couldn't agree more. I've experience this with trying to learn G code for my cnc machines. I've skimmed the surface with writing macros for them and that seems kinda similar to the kind of code that i will be needing for this project. I could be way off the mark with that assumption though.

I recommend that you use a display like a Nextion or DWIN which takes the display processing from the esp32 and runs it on an internal processor.

I have used this library for encoder reading. It uses the esp32 pcnt module rather than interrupts.

madhephaestus/ESP32Encoder

What increment of angular movement do you need (whole degrees or fractional degrees)?
If fractional, base 2 (1/2, 1/4, 1/8, etc.) or base 10 (0.5, 0.43, 0.703, etc)?
Is the motor fixed or variable speed, does it coast to stop or have a brake?
What is the motor's rated power (horsepower or watts)?
What is the bending speed (how many seconds to turn 90 degrees)?

I will definitely look into that. Thanks.

been experimenting with a Optical Incremental Rotary Encoder DC 5-24V 600P/R AB 2-Phase NPN Output Quadrature connected to an ESP32 running

// ESP32encoder - works ON ESP32 ESP32-S3
// NOTE: HalfQuad is steps of 2 FullQuad is steps of 4

// https://github.com/madhephaestus/ESP32Encoder
// https://www.arduino.cc/reference/en/libraries/esp32encoder/
// documentation https://madhephaestus.github.io/ESP32Encoder/classESP32Encoder.html

// A note on KY-040 and similar
// need electrical debouncing in the range of 0.1 - 2 uf per encoder line to ground
// and A note on KY-040 and similar

// Incremental Rotary Encoder DC 5-24V 600P/R AB 2-Phase NPN Output Quadrature Optical Encoder
// https://forum.arduino.cc/t/how-to-connect-24-volt-powered-lpd3806-incremental-encoder-to-esp32/1078435/4
//  NOTE: requires external 5V-24V power supply - ESP32 5V does not work
//     and 4.7Kohm pullup resistors to 3.3V on A and B

#include <ESP32Encoder.h>

ESP32Encoder encoder;
#define SWITCH 4  // encoder press switch to reset count

void setup() {
  Serial.begin(115200);
  delay(2000);
  Serial.println("\n\nESP32encoder test\nrotate to display count - press to reset");
  // Enable the weak pull up resistors
  ESP32Encoder::useInternalWeakPullResistors = puType::up;
  ;
  encoder.attachHalfQuad(17, 16);
  //encoder.attachFullQuad(16, 17);
  encoder.setFilter(1023);
  // clear the encoder's raw count and set the tracked count to zero
  encoder.clearCount();
  pinMode(SWITCH, INPUT_PULLUP);  // switch input
  ///while(1)  Serial.println(digitalRead(SWITCH));
}

void loop() {
  static int64_t oldEncoder = -32000, checkEncoder = 0;
  int64_t encoder1 = encoder.getCount();
  // if switch pressed reset count
  if (!digitalRead(SWITCH)) {
    while (!digitalRead(SWITCH))
      ;  // wait for switch release
    encoder.clearCount();
    checkEncoder = 0;
    Serial.println("reset count ");  // + String(digitalRead(SWITCH)));
  }
  // if count changed display it
  if (encoder1 != oldEncoder) {
    Serial.printf("Encoder %d ", (int32_t)(encoder1));
    Serial.printf("%0.2f degrees\n ", (int32_t)encoder1 * 360.0 / 1200);
    int64_t test = abs(encoder1 - checkEncoder);
    if (test > 1) Serial.printf("error difference %d\n", test);
    checkEncoder = oldEncoder = encoder1;
    // delay(100);
  }
}

sample serial monitor output

ESP32encoder test
rotate to display count - press to reset
Encoder 0 0.00 degrees
 Encoder 1 0.30 degrees
 Encoder 2 0.60 degrees
 Encoder 3 0.90 degrees
 Encoder 4 1.20 degrees
 Encoder 5 1.50 degrees
 Encoder 6 1.80 degrees
 Encoder 7 2.10 degrees
…
Encoder 298 89.40 degrees
 Encoder 299 89.70 degrees
 Encoder 300 90.00 degrees
 Encoder 301 90.30 degrees
 Encoder 302 90.60 degrees
 Encoder 303 90.90 degrees
 Encoder 304 91.20 degrees
 Encoder 305 91.50 degrees
 Encoder 306 91.80 degrees
 Encoder 307 92.10 degrees
 Encoder 308 92.40 degrees
 Encoder 309 92.70 degrees
…
Encoder 593 177.90 degrees
 Encoder 594 178.20 degrees
 Encoder 595 178.50 degrees
 Encoder 596 178.80 degrees
 Encoder 597 179.10 degrees
 Encoder 598 179.40 degrees
 Encoder 599 179.70 degrees
 Encoder 600 180.00 degrees
 Encoder 601 180.30 degrees
 Encoder 602 180.60 degrees
 Encoder 603 180.90 degrees
…
Encoder 895 268.50 degrees
 Encoder 896 268.80 degrees
 Encoder 897 269.10 degrees
 Encoder 898 269.40 degrees
 Encoder 899 269.70 degrees
 Encoder 900 270.00 degrees
 Encoder 901 270.30 degrees
 Encoder 902 270.60 degrees
 Encoder 903 270.90 degrees
 Encoder 904 271.20 degrees
 Encoder 905 271.50 degrees
…
Encoder 1192 357.60 degrees
 Encoder 1193 357.90 degrees
 Encoder 1194 358.20 degrees
 Encoder 1195 358.50 degrees
 Encoder 1196 358.80 degrees
 Encoder 1197 359.10 degrees
 Encoder 1198 359.40 degrees
 Encoder 1199 359.70 degrees
 Encoder 1200 360.00 degrees
 Encoder 1201 360.30 degrees
 Encoder 1202 360.60 degrees
 Encoder 1201 360.30 degrees
 Encoder 1200 360.00 degrees

using the protractor the angle readings look correct, e.g.


while and green wires are the QEI A and B signals with 4.7K pullup resistors to 3.3V
red is 5V from an external power supply (encoder would not work with the ESP32 5V)

as only QEI A and B signals are required should be easy enough to connect to a CYD

will look to see if I can couple the encoder to a stepper motor to test accuracy of angle readings