Advertisement
Guest User

Motor Control

a guest
Jan 14th, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.01 KB | None | 0 0
  1. using System;
  2. using System.Timers;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Diagnostics;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17.  
  18.  
  19. using WpfCalculator;
  20.  
  21. namespace RotaryTable
  22. {
  23. /// <summary>
  24. /// Interaction logic for MainWindow.xaml
  25. /// </summary>
  26. public partial class MainWindow : Window
  27. {
  28. public MainWindow()
  29. {
  30. InitializeComponent();
  31. }
  32.  
  33. StepperWindow stepper = null;
  34. StepperWindow stepper2 = null;
  35. Calculator c = null;
  36. Stopwatch time = new Stopwatch();
  37. Stopwatch testt = new Stopwatch();
  38.  
  39.  
  40.  
  41. int Mode = 0;
  42. int State = 0;
  43.  
  44. private void Window_Loaded(object sender, RoutedEventArgs e)
  45. {
  46. stepper = new StepperWindow(304413, "Top Motor");
  47. //stepper = new StepperWindow(269156, "Rotary Table - Rev6");
  48.  
  49. stepper.SetScale(3000);
  50. stepper.SetAltIncrement(360);
  51. stepper.CurrentLimit = 2.0;
  52. stepper.Show();
  53. stepper.SetDial(0.0);
  54. stepper.StopStartActive = true;
  55. stepper.PositionChanged += new PhidgetPositionEventHandler(getPosn);
  56.  
  57. stepper2 = new StepperWindow(268801, "Bottom Motor");
  58. //stepper2 = new StepperWindow(268801, "X-drive (Rotary Table - Rev6)");
  59.  
  60. stepper2.SetScale(3000);
  61. stepper2.SetAltIncrement(360);
  62. stepper2.CurrentLimit = 2.0;
  63. stepper2.Show();
  64. stepper2.SetDial(0.0);
  65. stepper2.StopStartActive = false;
  66. stepper2.PositionChanged += new PhidgetPositionEventHandler(getPosn2);
  67. }
  68.  
  69. private void Window_Unloaded(object sender, RoutedEventArgs e)
  70. {
  71. stepper.engagedCheckBox.IsChecked = false;
  72. stepper2.engagedCheckBox.IsChecked = false;
  73. if (stepper != null) stepper.Close();
  74. if (stepper2 != null) stepper2.Close();
  75. if (c != null) c.Close();
  76. }
  77.  
  78.  
  79. double ratio = 1.5; //Ratio from Stepper motor to park interlock if different ratios were used.
  80. double acceleration = .1; //Hard coded.
  81. double speed = 0; //Initialized by user.
  82.  
  83. double angle = 0;
  84. double currPos = 0;
  85. double currPos2 = 0; //Divides GetPosn by 20 for stepper2 to be equal to stepper
  86.  
  87. double angleConv = -360 / 1.0667; //Converts position to angle
  88.  
  89. double currAngle = 0;
  90.  
  91. int count = 0;
  92.  
  93. long t = 0;
  94.  
  95. long time0 = 1;
  96. long time1 = 0;
  97. long time2 = 0;
  98.  
  99. int prevAngle = -1;
  100.  
  101. bool direction = false;//True if bottom motor is going slower, false if going faster.
  102. bool move = false;
  103. bool start = false;
  104. bool find = false; //Set true if angles want to be found.
  105.  
  106. bool check0 = false;
  107. bool check150 = false;
  108. bool check300 = false;
  109.  
  110. double angle0=10; //Set all values to 1000 if find is set to true.
  111. double angle150=190;
  112. double angle300=380;
  113.  
  114. //Sequence currently only goes from 0 to 150 to 300 and both 150 and 300 must have times set
  115. public void getPosn(object sender, PhidgetPositionEventArgs e)
  116. {
  117. if (Mode != 3) return;
  118.  
  119. //if (!e.m_complete) return;
  120.  
  121. //UI update for switch conditions
  122. Input0.IsChecked = stepper.input0Chk.IsChecked;
  123. Input1.IsChecked = stepper.input1Chk.IsChecked;
  124. Input2.IsChecked = stepper.input2Chk.IsChecked;
  125. Input3.IsChecked = stepper.input3Chk.IsChecked;
  126.  
  127.  
  128. //Angle updater
  129. currPos = e.m_position;
  130. //currAngle = (currPos2 + currPos - 90000) * angleConv;
  131. //CurrAngle.Text = currAngle.ToString();
  132. t = time.ElapsedMilliseconds;
  133.  
  134. //Moves the cam to the initial position.
  135. if(start==true&&(stepper.input0Chk.IsChecked==false&&stepper.input1Chk.IsChecked==true))
  136. {
  137. start=false;
  138. stepper.SetVelocityRpm(0);
  139. stepper.IsEnabled=false;
  140. Initialize();
  141. }
  142.  
  143. //Condition for angle 0 of cam
  144. if (prevAngle == 0 && t >= time0)
  145. {
  146. check0 = false;
  147. time.Reset();
  148. prevAngle = 1;
  149. direction = false;
  150. stepper2.SetVelocityRpm(speed * 1.5 * ratio);
  151. if (find == false)
  152. {
  153. angle = angle150;
  154. move = true;
  155. }
  156.  
  157. }
  158. //Condition for angle 150 of cam
  159. else if (prevAngle == 1 && t >= time1)
  160. {
  161. check150 = false;
  162. time.Reset();
  163. prevAngle = 2;
  164. stepper2.SetVelocityRpm(speed * 1.5 * ratio);
  165. direction = false;
  166. if (find == false)
  167. {
  168. angle = angle300;
  169. move = true;
  170. }
  171. }
  172. //Condition for angle 300 of cam
  173. else if (prevAngle == 2 && t >= time2)
  174. {
  175. check300 = false;
  176. time.Reset();
  177. prevAngle = 0;
  178. direction = true;
  179. stepper2.SetVelocityRpm(speed * .5 * ratio);
  180. if (find == false)
  181. {
  182. angle = angle0;
  183. move = true;
  184. }
  185. }
  186.  
  187.  
  188. //Checks for mechanical error after a 100ms delay
  189. if (check0 == true&&t>100)
  190. {
  191. if (stepper.input0Chk.IsChecked == true || stepper.input1Chk.IsChecked == false)
  192. {
  193. go.Content = "ERROR 01";
  194. Stop_Click(null, null);
  195. }
  196. }
  197. if (check150 == true&&t>100)
  198. {
  199. if (stepper.input2Chk.IsChecked == false || stepper.input3Chk.IsChecked == false)
  200. {
  201. go.Content = "ERROR 23";
  202. Stop_Click(null, null);
  203. }
  204. }
  205. if (check300 == true&&t>100)
  206. {
  207. if (stepper.input3Chk.IsChecked == false)
  208. {
  209. go.Content = "ERROR 3";
  210. Stop_Click(null, null);
  211. }
  212. }
  213.  
  214.  
  215. }
  216.  
  217. public void getPosn2(object sender, PhidgetPositionEventArgs e)
  218. {
  219. if (Mode != 3) return;
  220.  
  221.  
  222. //Angle updater
  223. currPos2 = e.m_position;
  224. currAngle = (currPos2 + currPos - 90000) * angleConv;
  225. CurrAngle.Text = currAngle.ToString();
  226.  
  227. //Sets initial angle positions.
  228. if (find == true)
  229. {
  230. if (angle150 == 1000 && stepper.input2Chk.IsChecked == true && stepper.input3Chk.IsChecked == true)
  231. {
  232. stepper2.SetVelocityRpm(speed * ratio);
  233. angle150 = currAngle;
  234. time.Start();
  235. }
  236. if (angle300 == 1000 && stepper.input3Chk.IsChecked == true)
  237. {
  238. stepper2.SetVelocityRpm(speed * ratio);
  239. angle300 = currAngle;
  240. time.Start();
  241. }
  242. if (angle0 == 10000 && stepper.input0Chk.IsChecked == false && stepper.input1Chk.IsChecked == true)
  243. {
  244. stepper2.SetVelocityRpm(speed * ratio);
  245. angle0 = currAngle;
  246. find = false;
  247. time.Start();
  248. }
  249. }
  250.  
  251.  
  252.  
  253. //Returns motor to original speed once it reaches the angle specified.
  254. if (currAngle>=angle && direction == false&&move==true)
  255. {
  256. move = false;
  257. time.Start();
  258. stepper2.SetVelocityRpm(speed * ratio);
  259. if (angle < 300)
  260. {
  261. check150 = true;
  262. }
  263. if (angle > 300)
  264. {
  265. check300 = true;
  266. }
  267. }
  268. if (currAngle <= angle && direction == true&&move==true)
  269. {
  270. move = false;
  271. count++;
  272. Count.Text = count.ToString();
  273. time.Start();
  274. stepper2.SetVelocityRpm(speed * ratio);
  275. check0 = true;
  276. }
  277.  
  278. }
  279.  
  280.  
  281. private void Button_Click(object sender, RoutedEventArgs e)
  282. {
  283. //Initializes speed and position
  284. speed = Convert.ToSingle(Speed.Text);
  285. if (speed < 0 || speed > 1000)
  286. {
  287. Speed.Text = "0";
  288. speed = 0;
  289. }
  290. speed = speed / 18000 * 53.6;
  291.  
  292. acceleration = Convert.ToDouble(Acceleration.Text);
  293. stepper.SetAcceleration(acceleration * 56);
  294. stepper2.SetAcceleration(acceleration * 56);
  295. if (stepper.input0Chk.IsChecked == true || stepper.input1Chk.IsChecked == false)
  296. {
  297. stepper.SetVelocityRpm(speed * ratio*.1);
  298. start = true;
  299. stepper.SetPosn(90001);
  300. stepper.engagedCheckBox.IsChecked = true;
  301. }
  302. else
  303. {
  304. Initialize();
  305. }
  306.  
  307.  
  308. }
  309.  
  310.  
  311.  
  312. private void Initialize()
  313. {
  314. //Variables
  315. stepper.SetVelocityRpm(speed * ratio);
  316. stepper2.SetVelocityRpm(speed * ratio);
  317. stepper.SetDial(45000);
  318. stepper2.SetDial(45000);
  319. stepper.SetPosn(90000);
  320. stepper2.SetPosn(0);
  321.  
  322. if (Angle0.Text != "")
  323. {
  324. angle0 = Convert.ToDouble(Angle0.Text);
  325. }
  326. if (Angle150.Text != "")
  327. {
  328. angle150 = Convert.ToDouble(Angle150.Text);
  329. }
  330. if (Angle300.Text != "")
  331. {
  332. angle300 = Convert.ToDouble(Angle300.Text);
  333. }
  334.  
  335.  
  336. //initializes conditions for angle sequence
  337. time0 = Convert.ToInt32(zero.Text);
  338. if (time0 <= 0)
  339. {
  340. time0 = 1;
  341. }
  342. time0 = time0 * 1000;
  343. time1 = Convert.ToInt32(onefifty.Text) * 1000;
  344. time2 = Convert.ToInt32(three.Text) * 1000;
  345.  
  346. if (time1 > 0 || time2 > 0)
  347. {
  348. time.Start();
  349. prevAngle = 0;
  350.  
  351. }
  352.  
  353. //Starts operation
  354. Start.IsEnabled = false;
  355. stepper.engagedCheckBox.IsChecked = true;
  356. stepper2.engagedCheckBox.IsChecked = true;
  357. Stop.IsEnabled = true;
  358. testt.Start();
  359. check0 = true;
  360. }
  361.  
  362.  
  363.  
  364. //Stops the test operation
  365. private void Stop_Click(object sender, RoutedEventArgs e)
  366. {
  367. TestTime.Text = testt.Elapsed.ToString();
  368. count = 0;
  369. time.Reset();
  370. testt.Reset();
  371. check0 = false;
  372. check150 = false;
  373. check300 = false;
  374. stepper.SetVelocityRpm(0);
  375. stepper.engagedCheckBox.IsChecked = false;
  376. stepper2.SetVelocityRpm(0);
  377. stepper2.engagedCheckBox.IsChecked = false;
  378. stepper.SetDial(stepper.GetCurrentPosition());
  379. stepper2.SetDial(stepper2.GetCurrentPosition());
  380. Stop.IsEnabled = false;
  381. Start.IsEnabled = true;
  382. }
  383.  
  384. }
  385. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement