FORARKADI

Dependencies:   mbed mypidror1 Motor Map

Revision:
8:4b5f04325cbe
Parent:
7:34118a0098e8
--- a/recive.cpp	Fri Mar 13 12:35:04 2020 +0000
+++ b/recive.cpp	Thu Apr 30 08:16:51 2020 +0000
@@ -8,6 +8,7 @@
     PA_0 ---> Analogin ---> Read Potentiometer 1
     PA_1 ---> Analogin ---> Read Potentiometer 2
     PA_3 ----> DigitalOut ---> to Led MOSFET
+    PA_7 ----> DigitalOut ---> to NPN 
     LED1 ----> DigitalOut ----> Error Indication
     CANBUS:
     PA_11 ---> CANBUS TX
@@ -42,6 +43,7 @@
 //led indicator
 
 DigitalOut myled(LED1);
+DigitalOut myNPN(PA_7);
 
 // brake signalout
 
@@ -53,7 +55,7 @@
 
 //Xbee
 
-BufferedSerial xbee(PA_9, PA_10);
+//BufferedSerial xbee(PA_9, PA_10);
 
 //Motor Control
 
@@ -78,6 +80,7 @@
 int counter = 0;
 int errorcounter = 0;
 int recievecounter = 0;
+int brakez = 0;
 
 // PID
 
@@ -127,7 +130,7 @@
 //////////////////
 
 Map mapvaltovolt = Map(0, 1, 0, 3300); // Maping Volt value from 0-1 to 0-3300
-Map mtodagree = Map(230, 3070, 0, 255); // Maping Volt value from 490-3150 to 0 - 255
+Map mtodagree = Map(470, 2750, 0, 255); // Maping Volt value from 490-3150 to 0 - 255
 Map nspeed = Map(-255, 255, -1, 1); // Maping out put speed from -255-255 to -1-1
 
 
@@ -137,12 +140,6 @@
 
 // After pid calculate and Sensor data recieve this function command the output to the motor
 
-void BrakeOn() {
-    mybrake = 1;
-    }
-void BrakeOff() {
-    mybrake = 0;
-    }
 void commandMotor(){ 
     float Motorcommand = nspeed.Calculate(speed);
     myMotor.speed(Motorcommand);
@@ -154,26 +151,16 @@
         main_ticker.detach();
         pid.stop();
         syserror.attach(sendError, 0.0005);
+        myNPN = 0;
         }
-    myMotor.speed(0);
+    myMotor.speed(-1);
 }
-    /*while (1) {
-        BrakeOn();
-        //pc.printf("Error is: %.4f\n\r", 1);
-        wait(1);
-        BrakeOff();
-        wait(1);
-        }
-    }*/
     
 // get Data overcanbus    
 void getData() {
-    //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[2]);
             if (can.read() > 0) {
                 can.stop();
                 can.reset();
@@ -187,10 +174,12 @@
                 perrorflag = 0;
                 }
             if (msg.data[3]==1){
-                BrakeOn();
+                mybrake = 1;
+                brakez = 1;
                 }
             if (msg.data[3]==0){
-                BrakeOff();
+                mybrake = 0;
+                brakez = 0;
                 }
             pdagree = msg.data[0];
             }
@@ -199,16 +188,12 @@
             }
         if (can.read() > 0.1) { // if message not recieved more then 0.1 sec send error
             can.stop();
-            //can.reset();
             errorcounter = 1;
             cantimeoutflag = 1;
             sendError();
             }
-        }
-        
-                
+        }      
 //reading Thorttle sensors
-
 void readSensors() { 
     thorttle1 = analog_value1.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
     thorttle2 = analog_value2.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
@@ -251,12 +236,13 @@
             }*/
 }
 int main() {
-    pc.baud(57600);
-    xbee.baud(57600);
+    myNPN = 1;
+    pc.baud(9600);
+    //xbee.baud(57600);
     pid.start();
     main_ticker.attach(readSensors, 0.0005);
     while(1){
-        //pc.printf("Pedal: %.4f\n\n\r, Thorttle: %.4f\n\n\r, subthorttle: %.4f\n\n\r, recievecounter: %d\n\n\r, ThorttleErrorFlag: %d\n\n\r, PedalErrorFlag: %d\n\n\r, CanTimeoutFlag: %d\n\n\r", pdagree, mdagree, subThorttle, recievecounter, terrorflag, perrorflag, cantimeoutflag);
-        xbee.printf("Pedal: %.4f\n\n\r, Thorttle:\n\r, recievecounter: %d\n\n\r, ThorttleErrorFlag: %d\n\n\r,, pdagree, mdagree, subThorttle, recievecounter, terrorflag, perrorflag, cantimeoutflag);
+        pc.printf("Pedal: %.4f\n\n\r, Thorttle: %.4f\n\n\r, subthorttle: %.4f\n\n\r, recievecounter: %d\n\n\r, ThorttleErrorFlag: %d\n\n\r, PedalErrorFlag: %d\n\n\r, CanTimeoutFlag: %d\n\n\r, Brakeon: %d\n\n\r", pdagree, mdagree, subThorttle, recievecounter, terrorflag, perrorflag, cantimeoutflag, brakez);
+        //xbee.printf("Pedal: %.4f\n\n\r, Thorttle:\n\r, recievecounter: %d\n\n\r, ThorttleErrorFlag: %d\n\n\r,, pdagree, mdagree, subThorttle, recievecounter, terrorflag, perrorflag, cantimeoutflag);
         }
 }
\ No newline at end of file