As an example of the application of the
PIDController class, here I present you the
MediumMotorPositionPID class whose main feature is the angular position regulation of the Ev3 Medium Motor. From the Lego shop website:
The Lego Mindstorms Ev3 Medium Motor
The EV3 Medium Servo Motor is great for lower-load, higher speed applications and when faster response times and a smaller profile are needed in the robot’s design. The motor uses tacho feedback for precise control within one degree of accuracy and has a built-in rotation sensor.
Tacho feedback to one degree of accuracy
240-250 RPM
Running torque of 8 N.cm (approximately 11 oz.in)
Stall torque of 12 N.cm (approximately 17 oz.in)
Auto-ID is built into the EV3 software
The
MediumMotorPositionPID class is derived from the
PIDController ; it has a
Motor property that you can use to assign the output port of the Ev3 Brick to which you have connected the medium motor; also, there are the
Start() and the
Stop() methods that you use to control the PID thread. When the thread is stopped, the motor is stopped too and brake is turned on. The
OutputSignal protected setter is overridden to assign the output of the PID controller to the motor raw (force) output.
You can compile the code I propose in this page using Xamarin Studio 5.10.1 (Build 6), with Mono 4.2.1 as active runtime and the Monobrick Firmware Library 1.2.0.39486 to let it work on board the Lego Mindstorms Ev3 with the Monobrick firmware.
The MediumMotorPositionPID class
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/// <summary>
/// EV3 Medium motor position PID
///
publicclassMediumMotorPositionPID:PIDController
{
#region Properties
///
/// Gets or sets the Motor to be used for regulation
///
/// The motor.
publicMotorMotor{get;set;}
///
/// Get the output signal of the PID controller
///
/// The output.
publicoverridesbyteOutputSignal
{
get{
returnoutputSignal;
}
protectedset{
if(outputSignal!=value){
outputSignal=value;
Motor.SetPower(outputSignal);
}
}
}
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
publicMediumMotorPositionPID():base()
{
// Fields initialization
init();
}
///
/// Fields initialization
///
privatevoidinit()
{
// Default constants
Kp=5f;
Ki=0.5f;
Kd=0f;
SampleTime=10;
}
#endregion
#region Public Methods
///
/// Start the PID
///
/// Motor must be assigned before operating the PID
publicoverridevoidStart()
{
// Check for motor validity
if(Motor==null){
InvalidOperationException ex=newInvalidOperationException("Motor must be assigned before operating the PID");
throw(ex);
}
// Stop motor
Motor.SetSpeed(0);
Motor.Brake();
Motor.ResetTacho();
// Start the PID
base.Start();
}
///
/// Stop the PID
///
/// Motor must be assigned before operating the PID
publicoverridevoidStop()
{
// Stop the PID
base.Stop();
// Check for motor validity
if(Motor==null){
InvalidOperationException ex=newInvalidOperationException("Motor must be assigned before operating the PID");
throw(ex);
}
// Stop motor
Motor.SetSpeed(0);
Motor.Brake();
}
#endregion
}
An application of this class is the Sup3r Car, where a PID controller has been used on the steering motor.
The source code happily shared above is subject to the Code Project Open Licence (CPOL) 1.02, hoping that you will enjoy it!
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptRejectRead More
Privacy & Cookies Policy
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.