FORARKADI

Dependencies:   mbed mypidror1 Motor Map

Revision:
3:94eac3bdfbf5
Parent:
2:0d44b696736b
Child:
4:d40399d8ae44
--- a/recive.cpp	Thu Mar 05 16:49:12 2020 +0000
+++ b/recive.cpp	Fri Mar 06 17:04:41 2020 +0000
@@ -73,6 +73,7 @@
 int startup = 0;
 int counter = 0;
 int errorcounter = 0;
+int recievecounter = 0;
 
 // PID
 
@@ -94,6 +95,7 @@
 //Tickers
 Ticker main_ticker;
 Ticker motor;
+Ticker syserror;
 
 //Flags for indication if something happend - error handling
 int pidflag = 0;
@@ -144,9 +146,12 @@
 
 // if there is Error this function Turn the Motor speed to 0 // Close the Thorttle and then The engine is off
 void sendError() { 
-    main_ticker.detach();
+    if (errorcounter == 1){
+        main_ticker.detach();
+        pid.stop();
+        syserror.attach(sendError, 0.0005);
+        }
     myMotor.speed(0);
-    pid.stop();
 }
     /*while (1) {
         BrakeOn();
@@ -162,28 +167,34 @@
     //pc.printf("%d", 1); 
     CANMessage msg;
         if(can1.read(msg)) {
+            recievecounter ++;
             //pc.printf("Pedal sensor worng values\n");
-            pc.printf("my data: %d", msg.data[0]);
+            //pc.printf("my data: %d", msg.data[2]);
             if (can.read() > 0) {
                 can.stop();
                 can.reset();
                 }
             if (msg.data[2]==1){
+                errorcounter = 1;
                 perrorflag = 1;
                 //sendError();
                 }
+            if (msg.data[2]==0){
+                perrorflag = 0;
+                }
             if (msg.data[3]==1){
                 BrakeOn();
                 }
             if (msg.data[3]==0){
                 BrakeOff();
                 }
-            if(msg.data[0]<30){
+            pdagree = msg.data[0];
+            /*if(msg.data[0]<30){
                 pdagree = 30;
                 }
             if(msg.data[0]>30){
                 pdagree = msg.data[0];
-                }
+                }*/
             //pid.sample();
             //pc.printf("Subthorttle is: %.4f\n\r", pdagree);
             //pc.printf("Error is: %.4f\n\r", msg.data[2]);
@@ -193,7 +204,7 @@
             }
         if (can.read() > 0.1) { // if message not recieved more then 0.1 sec send error
             can.stop();
-            can.reset();
+            //can.reset();
             cantimeoutflag = 1;
             //sendError();
             }
@@ -208,13 +219,13 @@
     thorttle1 = mapvaltovolt.Calculate(thorttle1);
     thorttle2 = mapvaltovolt.Calculate(thorttle2);
     sumThorttle = thorttle1+thorttle2;
-    subThorttle = abs(3500-sumThorttle);
+    subThorttle = abs(3300-sumThorttle);
     getData();
     pid.sample();
     commandMotor();
     //pc.printf("matzeret1 is: %.4f, matzeret2 is:%.4f\n\r", thorttle1, thorttle2);
     //pc.printf("Subthorttle is: %.4f\n\r", subThorttle);
-    if (subThorttle<600) { //checking error
+    if (subThorttle<330) { //checking error
         mythorttle = mtodagree.Calculate(thorttle1);
         sumthorttle = sumthorttle + mythorttle;
         counter++;
@@ -230,7 +241,7 @@
             sumthorttle = 0;
             }
         }
-    if (subThorttle>600) {
+    if (subThorttle>330) {
             if (errorcounter == 0) {
                 t.start();
                 errorcounter = 1;
@@ -239,7 +250,7 @@
                 t.stop();
                 t.reset();
                 terrorflag = 1;
-                sendError();
+                //sendError();
                 }
             }
 }
@@ -253,18 +264,11 @@
         /*if (cantimeoutflag = 1){
             pc.printf(pc.printf("Canbus message timeout\n"););
             }*/
-        /*if (perrorflag = 1){
-            myMotor.speed(0);
-            pc.printf("Pedal sensor worng values\n");
-            }*/
-        /*if (merrorflag = 1){
-            pc.printf("Pedal sensor worng values\n");
-            pc.printf("matzeret1 is: %.4f, matzeret2 is:%.4f\n\r", thorttle1, thorttle2);
-            }*/
-        /*if (merrorflag = 1){
-            pc.printf("Pedal sensor worng values\n");
-            }*/
-        //pc.printf("%.4f,%.4f,%.4f\n", tpid.read(), pdagree, mdagree);
+        //if (perrorflag == 1){
+            //myMotor.speed(0);
+            //pc.printf("Pedal sensor worng values\n");
+            //}
+        pc.printf("Pedal %.4f\n\r, Thorttle %.4f\n\r, subthorttle %.4f\n\r, counter %d\n\r", pdagree, mdagree, subThorttle, recievecounter);
         
         //wait(Ts);
         }