Kenneth Dunlop / Mbed 2 deprecated Ken_CAN_test

Dependencies:   mbed

Revision:
3:79133dcea836
Parent:
2:11339018dda6
Child:
4:e8e9bc25b1ca
--- a/Ken_CAN_test.cpp	Wed Jun 15 08:48:18 2022 +0000
+++ b/Ken_CAN_test.cpp	Thu Jun 16 14:19:54 2022 +0000
@@ -8,10 +8,13 @@
 #include <ctype.h> //For 'is digit' condition
 #include <sstream> //This is used to convert an integer to hex
 #include <time.h> //To measure how long a process took? Includes clock_t and CLOCKS_PER_SEC
+#include <list> //To let me make lists of CAN IDs
 
-RawSerial pc(USBTX, USBRX); // USB UART Terminal. Is needed for 'pc.printf' functions to work.
-CAN CanBus(p9, p10); //CANBus to use pins 9 and 10. This defines CanBus so other 'CanBus' lines will work.
-//CAN CanBus2(p29, p30); //CANBus2 for recieving to use pins 29 and 30. This defines CanBus so other 'CanBus' lines will work.
+RawSerial pc(USBTX, USBRX); // USB UART Terminal, tx, rx. Is needed for 'pc.printf' functions to work.
+
+// NOTE: Original pins for CAN Bus 1 are 9 and 10.
+CAN CanBus(p9, p10); //CANBus to use pins 29 and 30. This defines CanBus so other 'CanBus' lines will work.
+CAN CanBus2(p30, p29); //CANBus2 for recieving to use pins 29 and 30. This defines CanBus so other 'CanBus' lines will work.
 CANMessage messageIn;
 
 //Define a CAN message to send
@@ -21,12 +24,14 @@
 string messageOutText = "3AF 8 01 AA BB CC DD EE FF 99";
 string part = "";
 int partincrement = 0;
-int incrementer = 0;
+__int64 incrementer = 0;
 int CANCount = 0;
-int spamon = 0;
+int spamon = 1; //Temporarily have spam on
 int spamcount = 0;
+int totalspamsever = 0;
 int spamstarttime = 0;
 int spamendtime = 0;
+int vlistincrementer = 0;
 
 //A function to deal with each CAN message part (e.g.: 301, 8, 01, 02...)
 void dealwithpart(void)
@@ -94,7 +99,7 @@
     //pc.printf("--------------------------------------\r\n");
 }
 
-//An attempted function to get a coherent CAN message from one, uninterrupted string
+//A function to get a coherent CAN message from one, uninterrupted string
 void getCANfromstring(void)
 {
     //pc.printf("messageOutText is '%s'\r\n", messageOutText);
@@ -141,12 +146,14 @@
     //messageOut1.data[7] = 0x37;
 }
 
-void printMessageOut (void){
+void printMessageOut (void)
+{
     //This function will print out whatever the CAN bus is sending out. Can't be used constantly as it sends '000 8 00 00 00 00 00 00 00 00' all the time.
     pc.printf("Message OUT: %03X %01X %02X %02X %02X %02X %02X %02X %02X %02X\r\n",messageOut1.id,messageOut1.len,messageOut1.data[0],messageOut1.data[1],messageOut1.data[2],messageOut1.data[3],messageOut1.data[4],messageOut1.data[5],messageOut1.data[6],messageOut1.data[7]);
 }
 
-void printMessageIn (void){
+void printMessageIn (void)
+{
     //This function will print out whatever the CAN bus is receiving.
     pc.printf("Message IN: %03X %01X %02X %02X %02X %02X %02X %02X %02X %02X\r\n",messageIn.id,messageIn.len,messageIn.data[0],messageIn.data[1],messageIn.data[2],messageIn.data[3],messageIn.data[4],messageIn.data[5],messageIn.data[6],messageIn.data[7]);
 }
@@ -156,7 +163,9 @@
 {
     pc.baud(115200); // serial port at 115200
     CanBus.frequency(500 * 1000); // CAN bus at 500k
+    CanBus2.frequency(500 * 1000); // CAN bus at 500k
     CanBus.reset(); // clear any bus errors
+    CanBus2.reset(); // clear any bus errors
     //NOTE: Print messages must be below this line to work.
     pc.printf("------------------------------------------\r\n");
     pc.printf("Welcome to Ken CAN test.\r\n");
@@ -174,19 +183,17 @@
         if (pc.readable())
         {
             c = pc.getc();
-            if ((c != NULL))
+            if (c != NULL)
             {
-                // When the a key is pressed, define a CAN message and send it.
+                //When the a key is pressed, define a CAN message and send it.
                 //pc.printf("A key was pressed! (%c)\r\n", c);
-                //pc.printf("Will try to send CAN...\r\n");
-                //defineCANmessage();
                 messageOutText = "";
                 if (c == '1')
                     {messageOutText = "301 8 01 01 01 01 01 01 01 01";}
                 if (c == '2')
                     {messageOutText = "302 8 02 02 02 02 02 02 02 02";}
                 if (c == '3')
-                    {messageOutText = "333 8 00 00 00 00 00 00 00 33";}
+                    {messageOutText = "303 8 00 00 00 00 00 00 00 33";}
                 if (c == '4')
                     {messageOutText = "304 8 04 04 04 04 04 04 04 04";}
                 if (c == '5')
@@ -202,22 +209,39 @@
                 if (c == 'f')
                     {messageOutText = "FFF 8 FF FF FF FF FF FF FF FF";}
                 if (c == 'q')
-                    {pc.printf("Changing CAN bus speed to 125.\r\n");
-                    CanBus.frequency(125 * 1000);} // CAN bus at 125k
+                    {
+                    pc.printf("Changing CAN bus speed to 125.\r\n");
+                    CanBus.frequency(125 * 1000); // CAN bus at 125k
+                    CanBus2.frequency(125 * 1000);} // CAN bus at 125k
+                    }
                 if (c == 'w')
-                    {pc.printf("Changing CAN bus speed to 250.\r\n");
-                    CanBus.frequency(250 * 1000);} // CAN bus at 250k
+                    {
+                    pc.printf("Changing CAN bus speed to 250.\r\n");
+                    CanBus.frequency(250 * 1000); // CAN bus at 250k
+                    CanBus2.frequency(250 * 1000);} // CAN bus at 250k
+                    }
                 if (c == 'e')
-                    {pc.printf("Changing CAN bus speed to 500.\r\n");
-                    CanBus.frequency(500 * 1000);} // CAN bus at 500k
+                    {
+                    pc.printf("Changing CAN bus speed to 500.\r\n");
+                    CanBus.frequency(500 * 1000); // CAN bus at 500k
+                    CanBus2.frequency(500 * 1000);// CAN bus at 500k
+                    }
                 if (c == 'r')
-                    {pc.printf("Changing CAN bus speed to 1000.\r\n");
-                    CanBus.frequency(1000 * 1000);} // CAN bus at 1000k
+                    {
+                    pc.printf("Changing CAN bus speed to 1000.\r\n");
+                    CanBus.frequency(1000 * 1000); // CAN bus at 1000k
+                    CanBus2.frequency(1000 * 1000); // CAN bus at 1000k
+                    }
                 if (c == 's')
                     {
                     pc.printf("Sending authentic sats message.\r\n");
                     messageOutText = "301 8 06 3F B2 29 12 97 67 37";
                     }
+                if (c == 'o')
+                    {
+                    incrementer = (incrementer * 16);
+                    pc.printf("Multiplied Incrementer by 16. It's now now %d.\r\n", incrementer);
+                    }
                 if (c == 'i')
                     {
                     incrementer = incrementer + 16;
@@ -237,38 +261,63 @@
                 messageOut1.format = CANStandard;
                 if (c == 'z' and spamon == 0)
                     {
-                    pc.printf("Starting spam sequence...\r\n");
+                    pc.printf("Starting spam sequence. Press 'x' to end. \r\n");
                     spamon = 1;
                     incrementer = 0;
                     spamcount = 0;
-                    int spamstarttime = clock();
+                    spamstarttime = clock();
                     }
                 //Pressing 'x' switches off spam mode
                 if (c == 'x' and spamon == 1)
                     {
+                    //spamcount = 9999999;
                     spamon = 0;
-                    pc.printf("Ending spam mode. Spammed %d times.\r\n", spamcount);
-                    int spamendtime = clock();
+                    int spamendtime = 0;
+                    spamendtime = clock();
                     int spamtime = 0;
                     spamtime = spamendtime - spamstarttime;
-                    pc.printf("Clock right now is %d.\r\n", (spamtime));
+                    int spamtimeseconds = 0;
+                    spamtimeseconds = (spamtime / CLOCKS_PER_SEC);
+                    pc.printf("Ending spam mode. Spammed %d times for %d ticks (%d seconds) at %d ticks per second.\r\n", spamcount, spamtime, spamtimeseconds, CLOCKS_PER_SEC);
+                    //pc.printf("Clock right now is %d.\r\n", (spamtime));
+                    //pc.printf("Ticks per second are %d.\r\n", CLOCKS_PER_SEC);
+                    //pc.printf("Spam start/end is %d/%d.\r\n", spamstarttime, spamendtime);
+                    //pc.printf("Spamtime was %d ticks.\r\n", spamtime);
                     int spamspersecond = 0;
-                    spamspersecond = (spamcount / spamtime)/CLOCKS_PER_SEC;
-                    pc.printf("Spams per second are %d.\r\n", (spamspersecond));
+                    spamspersecond = (10000 * spamcount);
+                    //pc.printf("spamcount times 10,000 is %d.\r\n", spamspersecond);
+                    spamspersecond = spamspersecond / spamtime;
+                    //pc.printf("10,000 times spamcount divided by spamtime is %d spams per tick.\r\n", spamspersecond);
+                    spamspersecond = ((spamspersecond * CLOCKS_PER_SEC))/10000;
+                    pc.printf("Spams per second are %d.\r\n", spamspersecond);
+                    pc.printf("Total spams ever are %d.\r\n", totalspamsever);
+                    pc.printf("-------------------------\r\n");
+                    }
+                if (c == 'v')
+                    {
+                    string mylist[] = {"301", "302", "303", "304", "305", "306", "307", "308", "309", "313", "314", "322", "324", "329", "32A"};
+                    pc.printf("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s.\r\n", mylist[0], mylist[1], mylist [2], mylist [3], mylist [4], mylist [5], mylist [6], mylist [7], mylist [8], mylist [9], mylist [10], mylist [11], mylist [12], mylist [13], mylist [14]);
+                    pc.printf("Part %d of list says '%s'\r\n.", vlistincrementer, mylist[vlistincrementer]);
+                    vlistincrementer++;
                     }
                 if (messageOutText != "")
                     {
                     getCANfromstring();
                     CanBus.write(messageOut1);
+                    CanBus2.write(messageOut1);
                     if (spamon == 0)
-                        {printMessageOut();}
+                        {
+                        printMessageOut();
+                        }
+                    messageOutText = "";
                     }
             }
-        }
+        //}
         //If spam mode is on, spam an incrementing CAN message
         if (spamon == 1)
             {
             spamcount ++;
+            totalspamsever ++;
             incrementer ++;
             std::stringstream sstream;
             sstream << std::hex << incrementer;
@@ -282,16 +331,10 @@
             wait(0.01);
             }
         //Check for CAN messages coming in
-            if (CanBus.read(messageIn))
+        if (CanBus.read(messageIn))
             {
             CANCount ++;
             printMessageIn();
             }
-        //Spam out the message as fast as possible
-        //while (1)
-        //{
-        //  defineCANmessage();
-        //  CanBus.write(messageOut1);
-        //}
-    }
-}
\ No newline at end of file
+       }
+//}
\ No newline at end of file