new

Dependencies:   mbed CANMsg

Revision:
15:2af7ca4c5eb9
Parent:
14:24c5d58e8bc6
--- a/main.cpp	Thu Jan 10 10:40:27 2019 +0000
+++ b/main.cpp	Tue Jan 29 07:05:20 2019 +0000
@@ -143,7 +143,7 @@
         speedTimer.reset();
         pulseRised=1;
         oldTractorSpeedRead=1;
-        spazioCoperto+= pulseDistance;
+        //spazioCoperto+= pulseDistance;
     }
     speedFilter.reset();
     speedClock=1;
@@ -332,7 +332,7 @@
     }
 }
 // ------------------------------------------------------------------------------------------------------------------------------------------------------------------
-void stepSetting(){
+/*void stepSetting(){
     // Stepper driver init and set
     TBmotorRst=1;                               // reset stepper driver
     TBmotorDirecti=1;                           // reset stepper direction
@@ -352,7 +352,7 @@
     }else if (TBmotorSteps==1600){
         TBmotor_M1=0;
         TBmotor_M2=1;
-        TBmotor_M3=0;
+        TBmotor_M3=1;
     }else if (TBmotorSteps==3200){
         TBmotor_M1=1;
         TBmotor_M2=0;
@@ -380,7 +380,8 @@
     }
     TBmotorRst=0;
 }
-/*void stepSetting(){
+*/
+void stepSetting(){
     // Stepper driver init and set
     TBmotorRst=1;                               // reset stepper driver
     TBmotorDirecti=1;                           // reset stepper direction
@@ -393,47 +394,110 @@
     // 1    0   1       1/128
     // 0    1   1       1/10
     // 1    1   1       1/20
+    #if defined(provaStepSet)
+        switch (changeStep){
+            case 0:
+              TBmotorSteps=400.0f;
+              changeStep++;
+              break;
+            case 1:
+              TBmotorSteps=1600.0f;
+              changeStep++;
+              break;
+            case 2:
+              TBmotorSteps=3200.0f;
+              changeStep++;
+              break;
+            case 3:
+              TBmotorSteps=6400.0f;
+              changeStep++;
+              break;
+            case 4:
+              TBmotorSteps=12800.0f;
+              changeStep++;
+              break;
+            case 5:
+              TBmotorSteps=25600.0f;
+              changeStep++;
+              break;
+            case 6:
+              TBmotorSteps=2000.0f;
+              changeStep++;
+              break;
+            case 7:
+              TBmotorSteps=4000.0f;
+              changeStep=0;
+              break;
+        }
+    #endif
     if (TBmotorSteps==400.0f){
-        TBmotor_M1=0;
-        TBmotor_M2=0;
-        TBmotor_M3=0;
+        TBmotor_M1=1;//0;
+        TBmotor_M2=1;//0;
+        TBmotor_M3=1;//0;
+        #if defined(pcSerial)
+            pc.printf("Steps  400\n");
+        #endif
     }else if (TBmotorSteps==1600.0f){
-        TBmotor_M1=0;
-        TBmotor_M2=0;
-        TBmotor_M3=1;
+        TBmotor_M1=0;//0;
+        TBmotor_M2=1;//1;
+        TBmotor_M3=1;//1;
+        #if defined(pcSerial)
+            pc.printf("Steps  1600\n");
+        #endif
     }else if (TBmotorSteps==3200.0f){
-        TBmotor_M1=0;
-        TBmotor_M2=1;
-        TBmotor_M3=0;
+        TBmotor_M1=1;//0;
+        TBmotor_M2=0;//1;
+        TBmotor_M3=1;//0;
+        #if defined(pcSerial)
+            pc.printf("Steps  3200\n");
+        #endif
     }else if (TBmotorSteps==6400.0f){
-        TBmotor_M1=1;
-        TBmotor_M2=1;
-        TBmotor_M3=0;
+        TBmotor_M1=0;//1;
+        TBmotor_M2=0;//1;
+        TBmotor_M3=1;//0;
+        #if defined(pcSerial)
+            pc.printf("Steps  6400\n");
+        #endif
     }else if (TBmotorSteps==12800.0f){
-        TBmotor_M1=0;
-        TBmotor_M2=0;
-        TBmotor_M3=1;
+        TBmotor_M1=1;//0;
+        TBmotor_M2=1;//0;
+        TBmotor_M3=0;//1;
+        #if defined(pcSerial)
+            pc.printf("Steps  12800\n");
+        #endif
     }else if (TBmotorSteps==25600.0f){
-        TBmotor_M1=1;
-        TBmotor_M2=0;
-        TBmotor_M3=1;
+        TBmotor_M1=0;//1;
+        TBmotor_M2=1;//0;
+        TBmotor_M3=0;//1;
+        #if defined(pcSerial)
+            pc.printf("Steps  25600\n");
+        #endif
     }else if (TBmotorSteps==2000.0f){
-        TBmotor_M1=0;
-        TBmotor_M2=1;
-        TBmotor_M3=1;
+        TBmotor_M1=1;//0;
+        TBmotor_M2=0;//1;
+        TBmotor_M3=0;//1;
+        #if defined(pcSerial)
+            pc.printf("Steps  2000\n");
+        #endif
     }else if (TBmotorSteps==4000.0f){
-        TBmotor_M1=1;
-        TBmotor_M2=1;
-        TBmotor_M3=1;
+        TBmotor_M1=0;//1;
+        TBmotor_M2=0;//1;
+        TBmotor_M3=0;//1;
+        #if defined(pcSerial)
+            pc.printf("Steps  4000\n");
+        #endif
     }else{
         // set dei 1600 passi
         TBmotor_M1=1;
         TBmotor_M2=0;
         TBmotor_M3=0;
+        #if defined(pcSerial)
+            pc.printf("Step standard\n");
+        #endif
     }
     TBmotorRst=0;
 }
-*/
+
 //****************************************
 void dcSetting(){
         if ((speedFromPick==0)&&(encoder==false)){
@@ -809,9 +873,9 @@
             }
             if (rxMsg.id==RX_Configure){
                 uint8_t flags = rxMsg.data[0];
-                uint16_t steps = (uint32_t) rxMsg.data[1]*0x000000FF;
-                steps = steps + ((uint32_t)rxMsg.data[2]);
-                //TBmotorSteps =steps;
+                uint16_t steps = (uint32_t) rxMsg.data[2]*0x00000100;
+                steps = steps + ((uint32_t)rxMsg.data[1]);
+                TBmotorSteps =steps;
                 stepSetting();
                 cellsCountSet = rxMsg.data[3];
                 if ((flags&0x01)==0x01){
@@ -1089,7 +1153,6 @@
                 #if !defined(speedMaster)
                     if (quincCnt>=3){
                             if (speedFromMaster>0.0f){
-                                //Questo 
                                 if (enableSimula==0){
                                     tractorSpeed_MtS_timed = speedFromMaster + percento;
                                 }
@@ -1123,7 +1186,9 @@
     //wait(1.0f);
     wd.Configure(2);  //watchdog set at xx seconds
     
-    stepSetting();
+    #if !defined(provaStepSet)
+        stepSetting();
+    #endif
     
     for (int a=0; a<5;a++){
         mediaSpeed[a]=0;
@@ -1191,6 +1256,12 @@
        TATicker.attach(&inverti,2.0f);
     #endif // end prova stepper
     
+    #if defined(provaStepSet)
+       TBmotorRst=0;
+       TBticker.attach_us(&step_TBPulseOut,500.0f);  // clock time are seconds and attach seed motor stepper controls
+       TATicker.attach(&stepSetting,2.0f);
+    #endif 
+    
     #if defined(canbusActive)
         can1.attach(&leggiCAN, CAN::RxIrq);
     #endif
@@ -1199,7 +1270,7 @@
 // MAIN LOOP
 //**************************************************************************************************************
     while (true){
-        // ripetitore segnale di velocità
+        // ripetitore segnale di velocità. Il set a 1 è nella task ad interrupt
         if (tractorSpeedRead==0){speedClock=0;}
         
         // inversione segnali ingressi
@@ -1584,8 +1655,8 @@
     //***************************************************************************************************        
     // pulseRised define the event of speed wheel pulse occurs
     //        
-            double maxInterval = pulseDistance/minWorkSpeed;
-            double minIntervalPulse = pulseDistance/maxWorkSpeed;
+            //double maxInterval = pulseDistance/minWorkSpeed;
+            //double minIntervalPulse = pulseDistance/maxWorkSpeed;
             if (pulseRised==1){ 
                 if (enableSpeed<10){enableSpeed++;}
                 pulseRised=0;
@@ -1597,13 +1668,6 @@
                         if((quincCnt<3)||(speedFromMaster==0.0f)||(enableSimula==1)){
                             tractorSpeed_MtS_timed = ((pulseDistance  / pulseSpeedInterval));  // tractor speed (unit= Mt/S) from pulse time interval
                         }
-                        /*#if !defined(speedMaster)
-                            if (quincCnt>=5){
-                                    if (speedFromMaster>0.0f){
-                                        tractorSpeed_MtS_timed = speedFromMaster + percento;
-                                    }
-                            }
-                        #endif*/
                         if (checkSDrotation==0){
                             checkSDrotation=1;
                             SDwheelTimer.start();