![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
CANtest module (A base for more complex ones)
Dependencies: mbed-STM32F103C8T6 mbed
Fork of CANtest by
Diff: main.cpp
- Revision:
- 1:3759c5f6cf76
- Parent:
- 0:af731d8a8d2c
- Child:
- 2:bb0f12b33fa1
diff -r af731d8a8d2c -r 3759c5f6cf76 main.cpp --- a/main.cpp Fri Feb 11 14:57:24 2011 +0000 +++ b/main.cpp Mon Feb 14 13:26:46 2011 +0000 @@ -1,19 +1,26 @@ + + #include "mbed.h" #include "CAN.h" Ticker ticker; DigitalOut led1(LED1); DigitalOut led2(LED2); -CAN can1(p9, p10); // rd, td -CAN can2(p30, p29); +CAN can1(p9, p10); // rd, td Transmitter +CAN can2(p30, p29); // rd, td Monitor int counter = 0; char data[8]; -CANMessage msg; +//***************void send is the data you are sending************** +void send() { -void send() { - if(counter==0) { +//-------------------------CONFIGURATION----------------------------- +// Send this part if you want to configure your LSS compatible motor! +// Read more about the LSS protocol for more information. +// Don't forget to include an "else {}" part.(scroll down some) + + /*if(counter==0) { //--------Global Start nodes data[0] = (char)(0x01); // NMT Start RemoteNode data[1] = (char)(0x00); // Global @@ -30,9 +37,9 @@ counter ++; } else if(counter==1) { - //--------LSS Configure mode + //--------LSS Configure Mode data[0] = (char)(0x04); // LSS Set Mode - data[1] = (char)(0x01); // Mode Configure + data[1] = (char)(0x01); // Mode configure data[2] = (char)(0x00); data[3] = (char)(0x00); data[4] = (char)(0x00); @@ -40,7 +47,7 @@ data[6] = (char)(0x00); data[7] = (char)(0x00); if(can1.write(CANMessage(2021,data,2))) { - printf("LSS Config mode sent \n"); + printf("LSS Configure mode sent \n"); } led1 = !led1; counter ++; @@ -48,7 +55,7 @@ else if(counter==2) { //--------LSS Set Node_ID data[0] = (char)(0x11); // LSS Set Node_ID - data[1] = (char)(0x01); // to Node_ID: 01 + data[1] = (char)(0x03); // to Node_ID: 03 data[2] = (char)(0x00); data[3] = (char)(0x00); data[4] = (char)(0x00); @@ -56,28 +63,12 @@ data[6] = (char)(0x00); data[7] = (char)(0x00); if(can1.write(CANMessage(2021,data,2))) { - printf("LSS Set Node_ID to 0x01 sent \n"); + printf("LSS Set Node_ID to 0x03 sent \n"); } led1 = !led1; counter ++; } else if(counter==3) { - //--------LSS Save Config - data[0] = (char)(0x17); // LSS Save Config - data[1] = (char)(0x00); - data[2] = (char)(0x00); - data[3] = (char)(0x00); - data[4] = (char)(0x00); - data[5] = (char)(0x00); - data[6] = (char)(0x00); - data[7] = (char)(0x00); - if(can1.write(CANMessage(2021,data,1))) { - printf("LSS Save Config sent \n"); - } - led1 = !led1; - counter ++; - } - else if(counter==4) { //--------LSS Set Baudrate data[0] = (char)(0x13); // LSS Set Baudrate data[1] = (char)(0x00); // @@ -88,28 +79,28 @@ data[6] = (char)(0x00); data[7] = (char)(0x00); if(can1.write(CANMessage(2021,data,3))) { - printf("LSS Config mode sent \n"); + printf("LSS Set Baudrate sent \n"); } led1 = !led1; counter ++; } - /*else if(counter==5) ( - //--------LSS Set Bit timing - data[0] = (char)(0x15); // LSS Set Bit timing - data[1] = (char)(0x00); // Mode Configure - data[2] = (char)(0x00); + else if(counter==4) { + //--------LSS Save Config + data[0] = (char)(0x17); // LSS Save Config + data[1] = (char)(0x00); + data[2] = (char)(0x00); data[3] = (char)(0x00); data[4] = (char)(0x00); data[5] = (char)(0x00); data[6] = (char)(0x00); data[7] = (char)(0x00); - if(can1.write(CANMessage(2021,data,2))) { - printf("LSS Config mode sent \n"); + if(can1.write(CANMessage(2021,data,3))) { + printf("LSS Save Config sent \n"); } led1 = !led1; counter ++; - }*/ - else if(counter==5) { + } + else if(counter==6) { //--------LSS Operation Mode data[0] = (char)(0x04); // LSS Set Mode data[1] = (char)(0x00); // Mode operation @@ -124,10 +115,104 @@ } led1 = !led1; counter ++; + }*/ +//-----------------------END OF CONFIGURATION--------------------- + + +//-----------------------Running your motor----------------------- +// Look up how to make your motor run. +// This example works on a FAULHABER 3242 G024 BX4 CC +// Don't forget to include an "else {}". Scroll down a bit. + + if(counter==0) { + //--------Start node 3-------- + data[0] = (char)(0x01); // Start Node + data[1] = (char)(0x03); // ID 03 + data[2] = (char)(0x00); + data[3] = (char)(0x00); + data[4] = (char)(0x00); + data[5] = (char)(0x00); + data[6] = (char)(0x00); + data[7] = (char)(0x00); + if(can1.write(CANMessage(0000,data,2))) { + printf("Start Node_ID 03 sent \n"); + } + led1 = !led1; + counter ++; } + else if(counter==1) { + //--------FAULHABER Mode + data[0] = (char)(0xFD); // FAULHABER MODE + data[1] = (char)(0xFF); + data[2] = (char)(0xFF); + data[3] = (char)(0xFF); + data[4] = (char)(0xFF); + data[5] = (char)(0x00); + data[6] = (char)(0x00); + data[7] = (char)(0x00); + if(can1.write(CANMessage(771,data,5))) { + printf("Operation mode: FAULHABER sent \n"); + } + led1 = !led1; + counter ++; + } + else if(counter==2) { + //--------Switch ON using FAULHABER + data[0] = (char)(0x0F); // FAULHABER MODE + data[1] = (char)(0x00); + data[2] = (char)(0x00); + data[3] = (char)(0x00); + data[4] = (char)(0x00); + data[5] = (char)(0x00); + data[6] = (char)(0x00); + data[7] = (char)(0x00); + if(can1.write(CANMessage(771,data,5))) { + printf("Switch ON sent \n"); + } + led1 = !led1; + counter ++; + } + else if(counter==3) { + //--------Speed setting + data[0] = (char)(0x93); // FAULHABER MODE + data[1] = (char)(0xF4); + data[2] = (char)(0x01); + data[3] = (char)(0x00); + data[4] = (char)(0x00); + data[5] = (char)(0x00); + data[6] = (char)(0x00); + data[7] = (char)(0x00); + if(can1.write(CANMessage(771,data,5))) { + printf("Speed = 500RPM sent \n"); + } + led1 = !led1; + counter ++; + } + else if(counter==4) { + //--------Datarequest setting + data[0] = (char)(0x40); // FAULHABER MODE + data[1] = (char)(0x00); + data[2] = (char)(0x00); + data[3] = (char)(0x00); + data[4] = (char)(0x00); + data[5] = (char)(0x00); + data[6] = (char)(0x00); + data[7] = (char)(0x00); + if(can1.write(CANMessage(771,data,5))) { + printf("Requesttype: position sent \n"); + } + led1 = !led1; + counter ++; + } +//--------------------End run motor-------------------------------- + + +//---------------------Request Position---------------------------- +// Run this one to request the position of my motor. +// Check the datasheet of your motor to confirm if this works. else { - //--------Inquire IDNode_ID - data[0] = (char)(0x5E); + //--------Positie opvragen + data[0] = (char)(0x00); data[1] = (char)(0x00); data[2] = (char)(0x00); data[3] = (char)(0x00); @@ -135,18 +220,87 @@ data[5] = (char)(0x00); data[6] = (char)(0x00); data[7] = (char)(0x00); - if(can1.write(CANMessage(2021,data,1))) { + if(can1.write(CANMessage(643))) { + printf("Positie check sent \n"); + } + led1 = !led1; + counter = 10; + } +//--------------------End request position------------------- + + +//---------------------Request Status-------------------------- +// Run this one to request the status of my motor. +// Check the datasheet of your motor to confirm if this works. + /* else { + //--------Status check + data[0] = (char)(0x00); + data[1] = (char)(0x00); + data[2] = (char)(0x00); + data[3] = (char)(0x00); + data[4] = (char)(0x00); + data[5] = (char)(0x00); + data[6] = (char)(0x00); + data[7] = (char)(0x00); + if(can1.write(CANMessage(387))) { printf("Status check sent \n"); } led1 = !led1; counter = 10; - } + }*/ +//------------------End request status------------------ + + +//---------Example------------------ + + /*if(counter==0) { + //--------8 bytes of data + data[0] = (char)(0xFF); + data[1] = (char)(0xFF); + data[2] = (char)(0xFF); + data[3] = (char)(0xFF); + data[4] = (char)(0xFF); + data[5] = (char)(0x00); + data[6] = (char)(0x00); + data[7] = (char)(0x00); + if(can1.write(CANMessage(2021,data,8))) { + printf("8bytes test sent \n"); + } + led1 = !led1; + counter ++; + } + else { + //--------2 bytes of data + data[0] = (char)(0x00); + data[1] = (char)(0xAA); + data[2] = (char)(0x00); + data[3] = (char)(0x00); + data[4] = (char)(0x00); + data[5] = (char)(0x00); + data[6] = (char)(0x00); + data[7] = (char)(0x00); + if(can1.write(CANMessage(2021,data,2))) { + printf("2bytes test sent \n"); + } + led1 = !led1; + counter = 10; + }*/ +//-------End of example-------------- } +//************************************************************* + +//**********The main program********************* int main() { +//----------------Initialization----------------------- can2.frequency(500000); //500kbit/s can1.frequency(500000); + //can2.monitor(1); //Works without this, in my case. ticker.attach(&send, 1); //Send every second + CANMessage msg; +//----------------------------------------------------- + +//---------------The read out---------------------- while(1) { if(can2.read(msg)) { printf("Message read with ID: %d\n", msg.id); @@ -161,7 +315,9 @@ printf("Length: %d", msg.len); printf(", Type: %d", msg.type); printf(", Format: %d\n\n", msg.format); - led2 = !led2; + led2 = !led2; //Blink! } } -} \ No newline at end of file +//---------------------------------------------------- +} +//********************************************************* \ No newline at end of file