Lab 6 code.

Dependencies:   mbed

Fork of WaG by GroupA

Revision:
49:80d4ffabec16
Parent:
48:d612de6880b0
Child:
50:e3a03bc1e1a6
--- a/stepper.cpp	Fri Mar 30 20:28:41 2018 +0000
+++ b/stepper.cpp	Tue Apr 03 16:31:18 2018 +0000
@@ -17,6 +17,9 @@
 #include "spi.h"
 #include "stepper.h"
 #include "utility.h"
+#include "laser.h"
+#include "analog.h"
+#include "wag.h"
 
 extern DigitalIn jog_ccw;
 extern DigitalIn jog_cw;
@@ -24,8 +27,9 @@
 extern DigitalIn cal_button;
 extern DigitalIn home_sensor;
 extern Serial pc;
+extern DigitalOut laser;
 
-int stp_cur_pos;
+int stp_cur_pos = STP_POS_UNKN;
 int stp_sensor_pos[TGT_SENSOR_QUAN];
 
 extern spi_cfg drv8806 {
@@ -75,6 +79,17 @@
     //static int cur_pos = stp_cur_pos;
     static int turn[4] = {0x03, 0x06, 0x0c, 0x09};
     if (direction == STP_CW) {
+        
+        if (stp_cur_pos <= 400) {
+                if (stp_cur_pos != STP_POS_UNKN)
+                    stp_cur_pos++;
+        }
+        else {
+            pc.printf("Cannot turn past maximum position. Fatal error.\n");
+            while(1);
+        }
+        
+                
         for (int i = 0; i < 4; i++) {
             wait(MOTOR_DELAY);
             //pc.printf("i = %d\n", i);
@@ -83,6 +98,17 @@
         wait(0.015);
     }
     else if (direction == STP_CCW) {
+        
+        if (stp_cur_pos != 0) {
+            if (stp_cur_pos != STP_POS_UNKN)
+                stp_cur_pos--;
+        }
+        else {
+            pc.printf("Cannot turn past home position.\n");
+            wait(0.5);
+            return;
+        }
+        
         for (int i = 3; i >= 0; i--) {
             wait(MOTOR_DELAY);
             //pc.printf("i = %d\n", i);
@@ -110,27 +136,10 @@
     pc.printf("step motor test begin\n");
     while(1) {
             if (jog_ccw == 0) {
-                if (stp_cur_pos != 0) {
-                    stp_step(STP_CCW);
-                    if (stp_cur_pos != STP_POS_UNKN)
-                        stp_cur_pos--;
-                }
-                else {
-                    pc.printf("Cannot turn past home position.\n");
-                    wait(0.02);
-                }
-                
+                stp_step(STP_CCW);
             }
             if (jog_cw == 0) {
-                if (stp_cur_pos <= 400) {
-                    stp_step(STP_CW);
-                    if (stp_cur_pos != STP_POS_UNKN)
-                        stp_cur_pos++;
-                }
-                else {
-                    pc.printf("Cannot turn past maximum position. Fatal error.\n");
-                    while(1);
-                }
+                stp_step(STP_CCW);
             } 
             if (cal_button == 0) {
                 stp_find_home();
@@ -194,13 +203,17 @@
  *      Returns: void
 */
 void stp_calibrate(int station, float * sensor_values, int * cal_status){
+    while (uti_chk_ubutton() == 0);
+    pc.printf("step 9 test begin\n");
+    
     int sensor_no = 0;
-    if (station == STATION_B) sensor = 8;
+    if (station == STATION_B) sensor_no = 8;
     
-    // find home position 
+    // find home position
     stp_find_home();
     
     // turn laser on
+    wait(1);
     lzr_on();
     
     for (int i = 0; i < TGT_SENSOR_QUAN; i++) {