[. NET and raspberry pie] control the steering gear

Whether it's a small motor or a large motor, well, that's the motor. I'm sure everyone will be familiar with it. 4WD is an excellent toy. It has been popular since Lao Zhou was a child (it is estimated that many big friends have seen the cartoon "4WD brothers"), and until now, we can still see many people selling 4WD. Why do you think of 4WD? Because playing 4WD as a child made Lao Zhou know many wonderful motors, such as "red devil", "blue core" and "Purple devil"... I don't know what logical naming method it was. Anyway, everyone called it at that time.

These motors have a rotating shaft with red or orange coils (these colors in memory) and four magnets. The upper part of the metal shell is provided with two heat dissipation holes. The overall shape is round or flat. The motors of many remote control cars are round. There is also a motor like a bamboo tube. There are some sun hats and small fans inside.

The characteristic of General Motors is that they turn in one direction as soon as they are powered on (if the positive and negative connections are reversed, they turn in the opposite direction), but the steering gear is more fun than ordinary motors. The 360 degree steering gear is similar to the general motor. After power on, it will rotate in one direction, and the signal line controls its rotation direction and speed; However, we often say that the steering gear generally refers to the steering gear of 180 degrees. This kind of steering gear can rotate it by a certain angle through the signal line. When it reaches the specified angle, the rudder will stop.

180 degree steering gear can:

1. The movement of each joint of the robot can simulate human joint activity. Because the program can control the steering gear to the specified angle.

2. The remote control car can turn with the steering gear.

3. Mechanical arm / mechanical claw.

4. Make a camera that can rotate.

5. 3D printed components.

......

As long as you need to control the direction of rotation, you can use it.

 

There are many kinds of steering gear according to its working load and torque. We'd better choose 9g steering gear for development and experiment. Although this kind of steering gear has little strength, the development board can supply power directly without additional power supply. That's it. It's blue and transparent.

 

Because the shell is transparent, you can see what's inside. Of course, if you like sabotage like Lao Zhou, you can take it apart for fun.

 

It's best to wear gloves when dismantling. After all, this is a motor. You know there's a lot of lubricating oil in it. Pay attention to the reduction gears when installing them back. Some big partners dismantle them on impulse and can't install them back. Although this kind of steering gear usually costs a few yuan, you can't waste it like this.

 

The main components in the steering gear are:

1. Motor. There must be some of this, otherwise how to turn.

2. Potentiometer. Different angles will change different resistance values, so that the control board can drive the motor for how many revolutions.

3. Control panel.

4. N reduction gears.

 

The above is just to understand the steering gear. Next, let's focus on how to control the angle. The general tutorial will tell you that the steering gear rotation angle is controlled by setting different duty cycle through PWM. This is not wrong, because the control of the steering gear is really realized by PWM. However, this is not accurate. In fact, how many degrees to turn the steering gear is controlled by the duration of the high level.

Normally, the cycle length of the control pulse is 20 milliseconds, i.e. 20000 microseconds. Therefore, when PWM is used, the frequency is set to 50 Hz (1 / 0.02 second = 50 Hz). The time range for the actuator to recognize the high level is 0.5 ms to 2.5 ms, i.e. 500 us to 2500 us. The following figure is a moving picture stolen by Lao Zhou.

This is the reason why PWM can control the steering gear. The steering gear doesn't care before 0.5ms, so no matter whether the output signal is high-level or low-level during this period of time, so when the core of concern changes to high-level and turns off (changes to low-level).

 

As shown in the figure above, if the high level changes to low level when it lasts for 0.5ms, the steering gear rotates to 0 degrees.

 

 

If the high level turns off at 1.5 milliseconds, the steering gear rotates to 90 degrees.

 

 

If the high level turns off at 2.5 milliseconds, the steering gear rotates to 180 degrees.

 

To sum up, the following conditions need to be prepared to convert the signal controlling the steering gear into the duty cycle of PWM:

1. Cycle time length, generally 20000 us, converted to PWM frequency of 50Hz.

2. The effective duration of high level, generally: min = 500us, max = 2500us.

3. 500 - 2500 us, with a time period of 2500-500 = 2000us. Divide the 2000 microseconds equally with 180 degrees, that is, the time corresponding to each one degree angle is 2000 / 180 ≈ 11.11 us / deg.

To sum up, the duty cycle can be calculated as follows:

Suppose you want to rotate 90 degrees, that is, the duty cycle:

 

 

Translate the formula into Chinese, that's it

 

 

With the above foundation, it's easy to write code. Here, Lao Zhou wrote a test program. This command-line program can modify parameters by entering commands, which is convenient for everyone to do experiments. The command help information is as follows:

        using static System.Console;

        static void ShowHelps()
        {
            WriteLine("{0,-15}{1}", "h", "display help information");
            WriteLine("{0,-15}{1}", "x", "sign out");
            WriteLine("{0,-15}{1}", "d n", "Set cycle (microseconds)");
            WriteLine("{0,-15}{1}", "t n", "High level start time (microseconds)");
            WriteLine("{0,-15}{1}", "p n", "High level end time (microseconds)");
            WriteLine("{0,-15}{1}", "a n", "angle");
            WriteLine("{0,-15}{1}", "o", "Send pulse signal");
            WriteLine("{0,-15}{1}", "s", "Stop pulse");
            WriteLine();
        }
        

The following code is to calculate the length of time (microseconds) corresponding to 1 degree angle.

        static void ComputeMicroToAngle()
        {
            microForAngle = (maxMicrosec - minMicrosec) / 180.0d;
        }

minMicrosec is the minimum duration of high level: 500us; maxMicrosec is the maximum duration of high level: 2500us.

These variables are declared as follows:

        /// <summary>
        /// The minimum duration of high level is generally 500 us
        /// </summary>
        static int minMicrosec = 500;

        /// <summary>
        /// The maximum duration of high level is generally 2500 us
        /// </summary>
        static int maxMicrosec = 2500;

        /// <summary>
        /// Duration corresponding to one degree angle, in microseconds( us)
        /// </summary>
        static double microForAngle = 0d;

 

The main code is as follows. You can download the source code to view the rest.

            // establish PWM Channel instance
            PwmChannel ch = PwmChannel.Create(0, 0);
            ShowHelps(); //Print the help information once after running
            bool working = true; //Flag variable used to jump out of the loop
            while(working)
            {
                Write(">>>");
                // Read a line of text entered by the keyboard
                string line = ReadLine();
                // Read first character
                char first = line[0];
                // Analysis command
                switch(first)
                {
                    case 'h': //display help information
                        ShowHelps();
                        break;
                    case 'x': //Jump out of the loop and exit the program
                        working = false;
                        break;
                    case 'd': //Setting cycle, usually 20000 us
                        ch.Frequency = ParseFreq(line[1..].Trim());
                        break;
                    case 't': //Set the minimum duration of high level
                        ParseMinMicrosecond(line[1..].Trim());
                        ComputeMicroToAngle();
                        break;
                    case 'p': //Sets the maximum duration of the high level
                        ParseMaxMicrosecond(line[1..].Trim());
                        ComputeMicroToAngle();
                        break;
                    case 'a': //Sets the angle to rotate
                        double angle = ParseAngle(line[1..].Trim());
                        ch.DutyCycle = ComputeDuty(angle, ch.Frequency);
                        break;
                    case 'o': //Start sending pulse
                        ch.Start();
                        break;
                    case 's': //Stop sending pulse
                        ch.Stop();
                        break;
                    default:
                        WriteLine("<<< Invalid command");
                        break;
                }
            }
            ch.Dispose();

 

Compile, publish, upload to raspberry pie. Note that the steering gear has three lines:

1. The red wire (usually in the middle) is connected to the 5V pin of raspberry pie (positive supply).

2. Black wire or brown wire, connected to any GND pin of raspberry pie (negative power supply).

3. Yellow (some white) is the signal line, which is used to control the steering gear. It is connected to the GPIO 18 of raspberry pie. This is the default PWM pin for the whole series of raspberry pie.

 

Run the program. In the first step, enter d 20000 and set the cycle (for unification, all time parameters are in microseconds).

 

Input t 500 to set the starting time of high-level control angle, that is, the minimum value of duration, and take 500.

 

Input p 2500 to set the end time of high level, i.e. the maximum value of duration, which is generally 2500.

 

Enter the lowercase letter o to start PWM.

 

Enter a 120 and rotate to 120 degrees.

 

Enter a 30 and rotate to 30 degrees.

 

Input the lowercase letter s to stop the PWM signal and input x to exit.

 

See the effect.

 

Related source code, please Click here to download

 

Tags: .NET

Posted by fox_m on Mon, 18 Apr 2022 00:41:33 +0930