You have reached the legacy GHI Electronics, LLC website, for the new website please visit here. For the new forum please visit here.

This entry's latest version is outdated and must be revised. Please see the documentation for the latest API.

Brushless motor ESC for aircrafts by zerov83 (Chris)

Oct. 22, 2010   |   Snippet   |   Unlicensed   |   1663 views

With this driver you can control a brushless motor conected to an ESC (Electronic Speed Control). It's designed for aircrafts (eg. quadrocopter), thats the reason why there is no reverse.
You can use any hardware PWM pin. You can set your own input scale over the "Scale" property (0-100%, 0-1023, ...).
You can also set the frequency of the PWM signal. But be aware, the Period field is static, so all PWM's having the same frequency. This is by design. The hardware (USBizi) doesn't allow different signals. If you own a hardware which allows, feel free to make it non-static...



Author Version Date
zerov83 (Chris) 1 05/04 '12 at 01:44pm
1 — Source
  1. using GHIElectronics.NETMF.Hardware;
  2.  
  3. namespace QuadroLib.Acuator {
  4. /// <summary>
  5. /// Class to controll an Electronic Speed Controler (ESC) for Airplanes (NO REVERSE)
  6. /// </summary>
  7. public class BrushlessMotor {
  8. protected static uint Period;
  9. protected const uint Max = 20 * 100 * 1000;
  10. protected const uint Min = 10 * 100 * 1000;
  11.  
  12. // Scale
  13. private uint Precalc = (Max - Min) / _scale;
  14. private uint _scale = 100;
  15. public uint Scale {
  16. get {
  17. return _scale;
  18. }
  19. set {
  20. Stop();
  21. _scale = value;
  22. Precalc = (Max - Min) / _scale;
  23. }
  24. }
  25.  
  26. private readonly PWM _pwmPin;
  27.  
  28. /// <summary>
  29. /// Uses 50Hz
  30. /// </summary>
  31. /// <param name="pin">PWM Pin</param>
  32. public BrushlessMotor(PWM.Pin pin)
  33. : this(pin, Periods.P50Hz) {
  34. }
  35.  
  36. /// <summary>
  37. /// Use higher period, means faster response, not supported by all ESC
  38. /// </summary>
  39. /// <param name="pin">PWM pin</param>
  40. /// <param name="period">Period</param>
  41. public BrushlessMotor(PWM.Pin pin, Periods period) {
  42. Period = (uint)period;
  43. this._pwmPin = new PWM(pin);
  44. this._pwmPin.SetPulse(Period, Min);
  45. }
  46.  
  47. /// <summary>
  48. /// Sets the outputpower, from 0 to "Scale"
  49. /// </summary>
  50. /// <param name="power">0..."Scale"</param>
  51. public void SetPower(int power) {
  52. uint highTime = (uint)(Min + (Precalc * Constrain(power, 0, _scale)));
  53. this._pwmPin.SetPulse(Period, highTime);
  54. }
  55.  
  56. public void Stop() {
  57. this._pwmPin.SetPulse(Period, Min);
  58. }
  59.  
  60. public void Full() {
  61. this._pwmPin.SetPulse(Period, Max);
  62. }
  63.  
  64. private static int Constrain(int x, int a, int b) {
  65. if (x > a && x < b) {
  66. return x;
  67. }
  68. if (x < a) {
  69. return a;
  70. }
  71. return b;
  72. }
  73. }
  74.  
  75. public enum Periods : uint {
  76. /// <summary>
  77. /// Standard rate 50Hz
  78. /// </summary>
  79. P50Hz = 20 * 1000000,
  80. /// <summary>
  81. /// Turbo PWM 400Hz
  82. /// </summary>
  83. P400Hz = 25 * 100000,
  84. /// <summary>
  85. /// Turbo PWM 500Hz
  86. /// </summary>
  87. P500Hz = 2 * 1000000,
  88. }
  89. }
  90.