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

SeeedStudio I²C motor driver by Bec a Fuel

May. 27, 2012   |   Snippet   |   Licensed as Apache 2.0   |   1594 views

This is a simple driver for the SeeedStudio Grove I2C motor driver http://www.seeedstudio.com/depot/grove-i2c-motor-driver-p-907.html?cPath=170_173

It features simple commands like Move(), Stop() and SetPWM(). It also returns the Moving state of both motors.



Author Version Date
Bec a Fuel 1 05/27 '12 at 10:35am
1 — Source
  1. using Microsoft.SPOT.Hardware;
  2.  
  3. namespace Becafuel
  4. {
  5. /// <summary>
  6. /// Direction of the move
  7. /// </summary>
  8. public enum eDirection
  9. {
  10. /// <summary>
  11. /// Moves forward
  12. /// </summary>
  13. Forward = 1,
  14. /// <summary>
  15. /// Moves backward
  16. /// </summary>
  17. Backward = 2
  18. }
  19.  
  20. /// <summary>
  21. /// SeeedStudio Grove - I2C Motor Driver class
  22. /// </summary>
  23. public class SeeedMotor
  24. {
  25. private I2CDevice.Configuration I2CConfig;
  26. private I2CDevice I2C;
  27. private byte BoardAddress;
  28. private sMotor[] TabMotors;
  29.  
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="SeeedMotor"/> class.
  32. /// </summary>
  33. /// <param name="Address">Board address, 0x28 is default value</param>
  34. /// <param name="DefaultPWM">The default PWM for both motors at startup. Default to 255 if not set in the constructor.</param>
  35. public SeeedMotor(byte Address, byte DefaultPWM = 255)
  36. {
  37. TabMotors = new sMotor[2];
  38. TabMotors[0] = new sMotor(0xA1, 255);
  39. TabMotors[1] = new sMotor(0xA5, 255);
  40. TabMotors[0].IsMoving = false;
  41. TabMotors[1].IsMoving = false;
  42. BoardAddress = Address;
  43. I2CConfig = new I2CDevice.Configuration((ushort)BoardAddress, 100);
  44. I2C = new I2CDevice(I2CConfig);
  45. I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[3];
  46. xActions[0] = I2CDevice.CreateWriteTransaction(new byte[3] { 0x82, 255, 255 });
  47. xActions[1] = I2CDevice.CreateWriteTransaction(new byte[3] { TabMotors[0].Address, 0, 0 });
  48. xActions[2] = I2CDevice.CreateWriteTransaction(new byte[3] { TabMotors[1].Address, 0, 0 });
  49. I2C.Execute(xActions, 1000);
  50. }
  51.  
  52. /// <summary>
  53. /// Stops one or both motors.
  54. /// </summary>
  55. /// <param name="Motor">The motor to be stopped.</param>
  56. public void StopMotor(byte Motor=255)
  57. {
  58. if (Motor >= 2) // Stop both motors at the same time
  59. {
  60. TabMotors[0].IsMoving = false;
  61. TabMotors[1].IsMoving = false;
  62. I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2];
  63. xActions[0] = I2CDevice.CreateWriteTransaction(new byte[3] { TabMotors[0].Address, 0, 0 });
  64. xActions[1] = I2CDevice.CreateWriteTransaction(new byte[3] { TabMotors[1].Address, 0, 0 });
  65. I2C.Execute(xActions, 1000);
  66. }
  67. else
  68. {
  69. TabMotors[Motor].IsMoving = false;
  70. I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[1];
  71. xActions[0] = I2CDevice.CreateWriteTransaction(new byte[3] { TabMotors[Motor].Address, 0, 0 });
  72. I2C.Execute(xActions, 1000);
  73. }
  74. }
  75.  
  76. /// <summary>
  77. /// Moves the specified motor at a specified PWM.
  78. /// </summary>
  79. /// <param name="Motor">The motor.</param>
  80. /// <param name="Direction">The direction.</param>
  81. /// <param name="PWM">The PWM.</param>
  82. public void Move(byte Motor, eDirection Direction, byte PWM)
  83. {
  84. if (Motor >= 2) // Move both motors at the same time
  85. {
  86. TabMotors[0].IsMoving = true;
  87. TabMotors[1].IsMoving = true;
  88. I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2];
  89. xActions[0] = I2CDevice.CreateWriteTransaction(new byte[3] { TabMotors[0].Address, (byte)Direction, PWM });
  90. xActions[1] = I2CDevice.CreateWriteTransaction(new byte[3] { TabMotors[1].Address, (byte)Direction, PWM });
  91. I2C.Execute(xActions, 1000);
  92. }
  93. else
  94. {
  95. TabMotors[Motor].IsMoving = true;
  96. I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[1];
  97. xActions[0] = I2CDevice.CreateWriteTransaction(new byte[3] { TabMotors[Motor].Address, (byte)Direction, PWM });
  98. I2C.Execute(xActions, 1000);
  99. }
  100. }
  101.  
  102. /// <summary>
  103. /// Moves the specified motor at its current PWM value.
  104. /// </summary>
  105. /// <param name="Motor">The motor.</param>
  106. /// <param name="Direction">The direction.</param>
  107. public void Move(byte Motor, eDirection Direction)
  108. {
  109. if (Motor >= 2) // Move both motors at the same time
  110. {
  111. TabMotors[0].IsMoving = true;
  112. TabMotors[1].IsMoving = true;
  113. I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2];
  114. xActions[0] = I2CDevice.CreateWriteTransaction(new byte[3] { TabMotors[0].Address, (byte)Direction, TabMotors[0].PWM });
  115. xActions[1] = I2CDevice.CreateWriteTransaction(new byte[3] { TabMotors[1].Address, (byte)Direction, TabMotors[1].PWM });
  116. I2C.Execute(xActions, 1000);
  117. }
  118. else
  119. {
  120. TabMotors[Motor].IsMoving = true;
  121. I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[1];
  122. xActions[0] = I2CDevice.CreateWriteTransaction(new byte[3] { TabMotors[Motor].Address, (byte)Direction, TabMotors[Motor].PWM });
  123. I2C.Execute(xActions, 1000);
  124. }
  125. }
  126.  
  127. /// <summary>
  128. /// Sets the PWM for the specified motor
  129. /// </summary>
  130. /// <param name="Motor">The motor.</param>
  131. /// <param name="PWM">The PWM.</param>
  132. public void SetPWM(byte Motor, byte PWM)
  133. {
  134. if (Motor >= 2) { TabMotors[0].PWM = TabMotors[1].PWM = PWM; }
  135. else { TabMotors[Motor].PWM = PWM; }
  136. }
  137.  
  138. /// <summary>
  139. /// Determines whether the specified motor is moving.
  140. /// </summary>
  141. /// <param name="Motor">The motor.</param>
  142. /// <returns>
  143. /// <c>true</c> if the specified motor is moving; otherwise, <c>false</c>.
  144. /// </returns>
  145. public bool IsMoving(byte Motor)
  146. {
  147. return TabMotors[Motor].IsMoving;
  148. }
  149. }
  150.  
  151. class sMotor
  152. {
  153. public byte Address;
  154. public byte PWM;
  155.  
  156. public sMotor(byte pAddress, byte pPWM)
  157. {
  158. Address = pAddress;
  159. PWM = pPWM;
  160. }
  161.  
  162. public bool IsMoving
  163. {
  164. get;
  165. internal set;
  166. }
  167. }
  168. }
  169.