peony3000

Gadgeteer_DC_Motor_01

Apr 24th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Threading;
  4. using Microsoft.SPOT;
  5. using Microsoft.SPOT.Presentation;
  6. using Microsoft.SPOT.Presentation.Controls;
  7. using Microsoft.SPOT.Presentation.Media;
  8. using Microsoft.SPOT.Touch;
  9.  
  10. using Gadgeteer.Networking;
  11. using GT = Gadgeteer;
  12. using GTM = Gadgeteer.Modules;
  13. using Gadgeteer.Modules.GHIElectronics;
  14.  
  15. namespace Gadgeteer_DC_Motor_01
  16. {
  17.     public partial class Program
  18.     {
  19.  
  20.         GT.Timer timer = new GT.Timer(100, GT.Timer.BehaviorType.RunContinuously);
  21.  
  22.         // joystick position
  23.         double joystickPosX;
  24.         double joystickPosY;
  25.  
  26.         // previous joystick position
  27.         double previousJoystickPosX;
  28.         double previousJoystickPosY;
  29.  
  30.         // the tolerance to decide
  31.         double joystickTolerance = 0.20;
  32.  
  33.         //bool flip = false;
  34.  
  35.         // This method is run when the mainboard is powered up or reset.  
  36.         void ProgramStarted()
  37.         {
  38.             /*******************************************************************************************
  39.             Modules added in the Program.gadgeteer designer view are used by typing
  40.             their name followed by a period, e.g.  button.  or  camera.
  41.            
  42.             Many modules generate useful events. Type +=<tab><tab> to add a handler to an event, e.g.:
  43.                 button.ButtonPressed +=<tab><tab>
  44.            
  45.             If you want to do something periodically, use a GT.Timer and handle its Tick event, e.g.:
  46.                 GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
  47.                 timer.Tick +=<tab><tab>
  48.                 timer.Start();
  49.             *******************************************************************************************/
  50.  
  51.             timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
  52.             timer.Start();
  53.  
  54.             // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
  55.             Debug.Print("Program Started");
  56.         }
  57.  
  58.         void timer_Tick(GT.Timer timer)
  59.         {
  60.  
  61.             // tell the robot to look at the joystick and get it's position
  62.             joystickPosX = joystick.GetJoystickPosition().X; // assigns the x-direction from 0 to 1 to the variable holder joystickPosX
  63.             joystickPosY = joystick.GetJoystickPosition().Y; // assigns the y-direction from 0 to 1 to the variable holder joystickPosY
  64.  
  65.             Debug.Print("joystickPosX: " + joystickPosX + " joystickPosY: " + joystickPosY);
  66.  
  67.  
  68.             // check to see if the last value is signifcantly different from the current value
  69.             if (joystickPosX < previousJoystickPosX + joystickTolerance && joystickPosX > previousJoystickPosX - joystickTolerance)
  70.             {
  71.                 // send a new value to the motor
  72.                 motorControllerL298.MoveMotor(MotorControllerL298.Motor.Motor1, (int)(joystickPosX * 100));    
  73.             }
  74.  
  75.             if (joystickPosY < previousJoystickPosY + joystickTolerance && joystickPosY > previousJoystickPosY - joystickTolerance)
  76.             {
  77.                 // send a new value to the motor
  78.                 motorControllerL298.MoveMotor(MotorControllerL298.Motor.Motor2, (int)(joystickPosY * 100));
  79.             }
  80.              
  81.             previousJoystickPosX = joystickPosX;
  82.             previousJoystickPosY = joystickPosY;
  83.  
  84.  
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment