📜 ⬆️ ⬇️

What we need CNC drive to build

Not so long ago, I attended to the question of how to make a drive for CNC systems on a collector engine, and even with feedback, and that would work on Modbus RTU.

I had at my disposal, Arduino UNO, Driver for L298N, collector motor from a 12V AEG car seat drive, slot photo interrupter 2 pcs.

From the beginning I soldered a handkerchief with 2 photo interrupters, according to this scheme.


')
In the finished form, it all looks like this:

image

The essence of this ugly thing - the match crosses the first breaker from the beginning, then the second, so the microcontroller understands which way the motor rotates.

All connections look like this:

d2 is the first photo interrupter;
d3 - the second photo interrupter;
d5 - exit pitch to rotate left;
d6 - exit pitch to rotate right;

Everything was connected and I sat down to write the code, in the end my flour gave birth to the following:

#include <ModbusRtu.h> #define ID 1 //  ,  ,   TX Modbus slave(ID, 0, 0); int8_t state = 0; //  uint16_t au16data[11];//   modbus #include <PID_v1.h> //     double Setpoint1, Input1, Output1; double Setpoint2, Input2, Output2; double Kp = 1, Ki = 2.5, Kd = 0.5; PID myPID1(&Input1, &Output1, &Setpoint1, Kp, Ki, Kd, DIRECT); //1-     PID myPID2(&Input2, &Output2, &Setpoint2, Kp, Ki, Kd, REVERSE); //2-    int LeftPWM = 5; //     int RightPWM = 6; //     volatile byte EncA, EncB = 0; //     volatile int Position = 0; //   int SetPosition = 0; //     void setup() { slave.begin(9600); pinMode (LeftPWM, OUTPUT); pinMode (RightPWM, OUTPUT); attachInterrupt(0, ChangePosition1, FALLING); //  0-(pin2)    ChangePosition1() attachInterrupt(1, ChangePosition2, FALLING); //  1-(pin3)    ChangePosition2() Setpoint1 = 0; Setpoint2 = 0; digitalWrite (LeftPWM, LOW); digitalWrite (RightPWM, LOW); } //  0 void ChangePosition1() //          ! { EncA = 1; if (EncA == 1 && EncB == 1) { EncA = 0; EncB = 0; Position++; } } //  1 void ChangePosition2() { EncB = 1; if (EncA == 1 && EncB == 1) { EncA = 0; EncB = 0; Position--; } } void loop() { //  ModBus //   state = slave.poll(au16data, 11); //   au16data[2] = Position;//  //   SetPosition = au16data[3];//  //   au16data[8] = slave.getInCnt(); //  au16data[9] = slave.getOutCnt(); //  au16data[10] = slave.getErrCnt();// if (SetPosition == Position) //     { myPID1.SetMode(MANUAL); myPID2.SetMode(MANUAL); Output1 = 0; Output2 = 0; digitalWrite (LeftPWM, LOW); digitalWrite (RightPWM, LOW); } if (SetPosition < Position) { myPID2.SetMode(AUTOMATIC); Setpoint2 = SetPosition; Input2 = Position; myPID2.Compute(); analogWrite (LeftPWM, Output2); digitalWrite (RightPWM, LOW); } if (SetPosition > Position) { myPID1.SetMode(AUTOMATIC); Setpoint1 = SetPosition; Input1 = Position; myPID1.Compute(); analogWrite (RightPWM, Output1); digitalWrite (LeftPWM, LOW); } } 

And now it's up to the computer. To make it all move, SinplLight SCADA was chosen, the guys give it to homemade artists for free. So - here are the settings in the scud.

image

image

image

The interface was made like this:

image

As a result, the engine was subordinated to the slider in the scud, accelerates and brakes the engine smoothly, if you manually rotate the armature of the motor, it does not allow to do this, and comes back. Thanks for attention. And here is a general view of the mess.

image

Source: https://habr.com/ru/post/410931/


All Articles