Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 1:f51eea878288
- Parent:
- 0:0da0fd430e20
- Child:
- 2:861d341b0259
diff -r 0da0fd430e20 -r f51eea878288 main.cpp
--- a/main.cpp Thu Dec 06 14:43:47 2018 +0000
+++ b/main.cpp Thu Jun 20 07:14:16 2019 +0000
@@ -1,42 +1,68 @@
- #include "mbed.h" //5.4.7 (144)
+ #include "mbed.h"
+ #define FREQUENCY 500000
-const PinName can1rxPins[] = {PA_11};
-const PinName can1txPins[] = {PA_12};
-const PinName can2rxPins[] = {PB_12};
-const PinName can2txPins[] = {PB_13};
+
+CAN can(PB_5, PB_6);
-//CAN1
-//0,0=OK 0,1=OK 0,2=OK 1,0=OK 1,1=OK 1,2=OK 2,0=OK 2,1=OK 2,2=OK
-//CAN2
-//0,0=RX_OK 0,1=OK 1,0=RX_OK 1,1=OK
-CAN can1(can1rxPins[0], can1txPins[0]);
-CAN can(can2rxPins[0], can2txPins[0]);
+DigitalOut led1(PA_8);
+DigitalOut led2(PA_9);
- //UART1, Tx, Rx (Debug)
-DigitalOut led1(LED1);
-
+AnalogIn ain(A0);
Thread sendThread(osPriorityAboveNormal, 2048);
Thread canrx;
+
CANMessage messageIn;
CANMessage messageOut;
+int speed = 0;
+int pose = 0;
+
void canRxIsr()
{
while(1)
{
- if(can1.read(messageIn))
+ if(can.read(messageIn))
{
led1 = !led1;
- printf("received\n\r");
+ pose = messageIn.data[0] + (messageIn.data[1] << 8) + (messageIn.data[2] << 16) + (messageIn.data[3] << 24);
+ printf("CAN: mess %d\n\r", pose);
+ printf("CAN: id %x \n\r ",messageIn.id);
}
+ osDelay(10);
}
- }
+}
+
void sendMessage()
{
+ messageOut.data[0] = speed >> 24;
+ messageOut.data[1] = speed >> 16;
+ messageOut.data[2] = speed >> 8;
+ messageOut.data[3] = speed;
+
+ messageOut.id = 769;
int status = can.write(messageOut);
+
+ osDelay(1);
+
+ messageOut.id = 770;
+ status = can.write(messageOut);
+
+ osDelay(1);
+
+ messageOut.id = 771;
+ status = can.write(messageOut);
+
+ osDelay(1);
+
+ messageOut.id = 772;
+ status = can.write(messageOut);
+
+
printf("Send status: %d\r\n", status);
+ if (status==1) led2 = !led2;
+ osDelay(20);
}
void sendMessageLoop()
@@ -50,23 +76,21 @@
int main()
{
-
- printf("CAN receive / transmit test.\r\n");
- //can.frequency(125000);
+ int frequency = FREQUENCY;
+ printf("CAN receive / transmit test. baudrate = %d \r\n", frequency);
+ can.frequency(500000);
+ messageOut.id = 769;
+ messageOut.format = CANExtended;
+ // messageOut.len = 0;
- messageOut.id = 1337;
- //messageOut.format = CANExtended;
- //messageOut.len = 0;
-
- canrx.start(canRxIsr);
- printf("CAN receive / .\r\n");
- sendThread.start(&sendMessageLoop);
- printf("CAN \r\n");
+ //canrx.start(canRxIsr);
+ sendThread.start(sendMessageLoop);
+ printf("CAN INIT \r\n");
while (true)
{
-
- led1 = 0;
- osDelay(1000);
+ speed = (int)((ain * 50000.0f) - 25000.0f);
+ //led1 = 0;
+ osDelay(10);
}
}
\ No newline at end of file