I finally completted my robot project.
It's nothing big. It doesn't know where it is going, but it knows an obstacle when it sees one.
I got those parts :
- 2 motors & wheels
- 1 IR range detector
- 1 DFRobot shield
- 1 disc-shaped Popolu chassi
- 1 stabilizing ball
I wrote a specific reusable class for Motors. It is not perfect, since it assumes I am using a DFRobot shield, and the speed is hardcoded (even though I prepared a variable speed for the future.
Here is the class :
using System; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; namespace Robot_Tests { enum Motors { M1, M2 } class MotorShield { public const Cpu.Pin M1 = Pins.GPIO_PIN_D7; public const Cpu.Pin E1 = Pins.GPIO_PIN_D6; public const Cpu.Pin M2 = Pins.GPIO_PIN_D4; public const Cpu.Pin E2 = Pins.GPIO_PIN_D5; /// <summary> /// Represents the motor's speed /// </summary> private int _speed; /// <summary> /// Represents the motor's speed /// </summary> public int Speed { get { return _speed; } set { _speed = value; } } /// <summary> /// Represents the left Motor /// regardless the Pin chosen on the Shield /// </summary> private Motor _leftMotor = new Motor(); /// <summary> ///Represents the right Motor /// regardless the Pin chosen on the Shield /// </summary> private Motor _rightMotor = new Motor(); public MotorShield() { //Sets the motors to a default value SetMotors(Motors.M1, Motors.M1); } /// <summary> /// Set which motor is right and which motor is left. /// </summary> /// <param name="MLeft">Motor used as the left Motor</param> /// <param name="MRight">Motor used as the right Motor</param> private void SetMotors(Motors MLeft, Motors MRight) { if (MLeft == MRight || MLeft == Motors.M1) { //Sets a default case in case same motor is chosen twice //Sets the case where M1 is left and M2 is right _leftMotor.E = new PWM(E1); _leftMotor.M = new OutputPort(M1, true); _rightMotor.E = new PWM(E2); _rightMotor.M = new OutputPort(M2, true); } else { //Sets the remainning case : M1 is right and M2 is left _rightMotor.E = new PWM(E1); _rightMotor.M = new OutputPort(M1, true); _leftMotor.E = new PWM(E2); _leftMotor.M = new OutputPort(M2, true); } } public void MoveForward() { _leftMotor.M.Write(true); _leftMotor.E.SetDutyCycle(100); _rightMotor.M.Write(true); _rightMotor.E.SetDutyCycle(100); } public void MoveBackward() { _leftMotor.M.Write(false); _leftMotor.E.SetDutyCycle(50); _rightMotor.M.Write(false); _rightMotor.E.SetDutyCycle(50); } public void TurnLeft() { _leftMotor.M.Write(false); _leftMotor.E.SetDutyCycle(50); _rightMotor.M.Write(true); _rightMotor.E.SetDutyCycle(50); } public void TurnRight() { _leftMotor.M.Write(true); _leftMotor.E.SetDutyCycle(50); _rightMotor.M.Write(false); _rightMotor.E.SetDutyCycle(50); } public void SetSpeed(uint iSpeed) { _rightMotor.setSpeed(iSpeed); _leftMotor.setSpeed(iSpeed); } } /// <summary> /// This class represents a single Motor /// This is designed for the classic Netduino and will use the /// Netduino classes and conventions /// </summary> class Motor { #region Properties /// <summary> /// E represtens the PWM pin chosen for this Motor speed control /// (Conventionnally represented by the letter "E"). /// </summary> private PWM _E; public PWM E { get { return _E; } set { _E = value; } } /// <summary> /// M represents the digital pin chosen for this Motor direction control /// (Conventionnally represented by the letter "E"). /// </summary> private OutputPort _M; public OutputPort M { get { return _M; } set { _M = value; } } private int _speed; public int Speed { get { return _speed; } set { _speed = value; } } //private OutputPort SpeedPort = new OutputPort( #endregion #region Constructors public Motor() { } public Motor(PWM pwm, OutputPort OutP) { E = pwm; M = OutP; } #endregion public void setSpeed(uint iSpeed) { if (iSpeed > 100) { iSpeed = 100; } E.SetDutyCycle(iSpeed); } } }
I also coded an IR Range detector class, but it's nothing new.
Here is the end result :
http://www.youtube.com/watch?v=ercJ2oK5O1I
That's it.
Now I achieved this simple thing, what is the logical next step? Giving it a sense of direction? Making its current direction better by ensuring both motors really run at the same speed? Any suggestion to make it better?
Thanks,
Asymptot