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.
Dependencies: mbed
main.cpp@2:373260769485, 2017-12-06 (annotated)
- Committer:
- ArmandLambrechts
- Date:
- Wed Dec 06 07:52:44 2017 +0000
- Revision:
- 2:373260769485
- Parent:
- 1:12ddbe69b6e6
- Child:
- 3:7530a7aca1ab
Canbus met potmeter;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ArmandLambrechts | 0:d9f4fedf5253 | 1 | /*** Thread: CAN *** |
ArmandLambrechts | 1:12ddbe69b6e6 | 2 | * Test CAN to controller |
ArmandLambrechts | 2:373260769485 | 3 | * |
ArmandLambrechts | 0:d9f4fedf5253 | 4 | */ |
ArmandLambrechts | 0:d9f4fedf5253 | 5 | #include "mbed.h" |
ArmandLambrechts | 0:d9f4fedf5253 | 6 | #include "CAN.h" |
ArmandLambrechts | 1:12ddbe69b6e6 | 7 | #include <string> |
ArmandLambrechts | 0:d9f4fedf5253 | 8 | /* CAN (RD TD) */ |
ArmandLambrechts | 0:d9f4fedf5253 | 9 | CAN can(PA_11, PA_12); |
ArmandLambrechts | 0:d9f4fedf5253 | 10 | |
ArmandLambrechts | 0:d9f4fedf5253 | 11 | DigitalOut canEN(PA_10); |
ArmandLambrechts | 2:373260769485 | 12 | |
ArmandLambrechts | 2:373260769485 | 13 | DigitalIn safety(PB_1); |
ArmandLambrechts | 2:373260769485 | 14 | DigitalOut buzzer(PB_0); |
ArmandLambrechts | 2:373260769485 | 15 | |
ArmandLambrechts | 2:373260769485 | 16 | AnalogIn analog_value(PA_0); |
ArmandLambrechts | 0:d9f4fedf5253 | 17 | |
ArmandLambrechts | 0:d9f4fedf5253 | 18 | /* Structures to help pack and unpack the 8 CAN data bytes */ |
ArmandLambrechts | 0:d9f4fedf5253 | 19 | typedef struct bytes_64 { |
ArmandLambrechts | 0:d9f4fedf5253 | 20 | uint8_t b0; |
ArmandLambrechts | 0:d9f4fedf5253 | 21 | uint8_t b1; |
ArmandLambrechts | 0:d9f4fedf5253 | 22 | uint8_t b2; |
ArmandLambrechts | 0:d9f4fedf5253 | 23 | uint8_t b3; |
ArmandLambrechts | 0:d9f4fedf5253 | 24 | uint8_t b4; |
ArmandLambrechts | 0:d9f4fedf5253 | 25 | uint8_t b5; |
ArmandLambrechts | 0:d9f4fedf5253 | 26 | uint8_t b6; |
ArmandLambrechts | 0:d9f4fedf5253 | 27 | uint8_t b7; |
ArmandLambrechts | 0:d9f4fedf5253 | 28 | } bytes_64; |
ArmandLambrechts | 0:d9f4fedf5253 | 29 | |
ArmandLambrechts | 0:d9f4fedf5253 | 30 | typedef union { |
ArmandLambrechts | 0:d9f4fedf5253 | 31 | uint64_t data; |
ArmandLambrechts | 0:d9f4fedf5253 | 32 | bytes_64 bytes; |
ArmandLambrechts | 0:d9f4fedf5253 | 33 | } data_packed; |
ArmandLambrechts | 0:d9f4fedf5253 | 34 | |
ArmandLambrechts | 2:373260769485 | 35 | long map(long x, long in_min, long in_max, long out_min, long out_max) //MAP functie voor verschalingen. |
ArmandLambrechts | 2:373260769485 | 36 | { |
ArmandLambrechts | 2:373260769485 | 37 | return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; |
ArmandLambrechts | 2:373260769485 | 38 | } |
ArmandLambrechts | 0:d9f4fedf5253 | 39 | int main() { |
ArmandLambrechts | 0:d9f4fedf5253 | 40 | |
ArmandLambrechts | 0:d9f4fedf5253 | 41 | printf("\r\n---Start---\r\n"); |
ArmandLambrechts | 0:d9f4fedf5253 | 42 | |
ArmandLambrechts | 2:373260769485 | 43 | data_packed send_data; //struct for send message |
ArmandLambrechts | 2:373260769485 | 44 | data_packed receive_data; //struct for rcv message |
ArmandLambrechts | 2:373260769485 | 45 | CANMessage msg; //message object for rcv |
ArmandLambrechts | 2:373260769485 | 46 | int send_id = 513; //Id motorcontroller 0x201 -> 513 |
ArmandLambrechts | 0:d9f4fedf5253 | 47 | int send_status = 0; |
ArmandLambrechts | 2:373260769485 | 48 | /* int ADC_Torque_Filter[8]; |
ArmandLambrechts | 2:373260769485 | 49 | int ADC_Torque_Value; |
ArmandLambrechts | 2:373260769485 | 50 | int i; */ //Voor moving average te berekenen |
ArmandLambrechts | 2:373260769485 | 51 | canEN = 0; //Voor de tranceiver |
ArmandLambrechts | 0:d9f4fedf5253 | 52 | |
ArmandLambrechts | 2:373260769485 | 53 | //send_data.data = 0x0CCD31; //0CCD snelheid motor, 31 register in controller. WERKT |
ArmandLambrechts | 0:d9f4fedf5253 | 54 | send_data.data = 0; |
ArmandLambrechts | 2:373260769485 | 55 | can.frequency(1000000); //1m freq |
ArmandLambrechts | 1:12ddbe69b6e6 | 56 | can.mode(CAN::LocalTest); |
ArmandLambrechts | 2:373260769485 | 57 | //can.mode(CAN::Normal); //1vd2 comment |
ArmandLambrechts | 2:373260769485 | 58 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
ArmandLambrechts | 2:373260769485 | 59 | int speed = 0; //Snelheid naar de motor |
ArmandLambrechts | 2:373260769485 | 60 | int reg = 0x31; //register in de controller |
ArmandLambrechts | 2:373260769485 | 61 | float meas_r; |
ArmandLambrechts | 2:373260769485 | 62 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
ArmandLambrechts | 0:d9f4fedf5253 | 63 | while (true) { |
ArmandLambrechts | 2:373260769485 | 64 | meas_r = analog_value.read() * 100; |
ArmandLambrechts | 2:373260769485 | 65 | speed = map(meas_r, 0, 100, 0, 0x7FFF); //waarde schommelt niet |
ArmandLambrechts | 2:373260769485 | 66 | |
ArmandLambrechts | 2:373260769485 | 67 | /* for (i = 0; i < 7; i++) |
ArmandLambrechts | 2:373260769485 | 68 | { |
ArmandLambrechts | 2:373260769485 | 69 | ADC_Torque_Filter[i] = ADC_Torque_Filter[i+1]; |
ArmandLambrechts | 2:373260769485 | 70 | //wait(0.005); //wacht 5 ms |
ArmandLambrechts | 2:373260769485 | 71 | } |
ArmandLambrechts | 2:373260769485 | 72 | ADC_Torque_Filter[7] = speed; //gas waarde berekenen via 8point moving average |
ArmandLambrechts | 2:373260769485 | 73 | ADC_Torque_Value = (( ADC_Torque_Filter[0]+ |
ArmandLambrechts | 2:373260769485 | 74 | ADC_Torque_Filter[1]+ |
ArmandLambrechts | 2:373260769485 | 75 | ADC_Torque_Filter[2]+ |
ArmandLambrechts | 2:373260769485 | 76 | ADC_Torque_Filter[3]+ |
ArmandLambrechts | 2:373260769485 | 77 | ADC_Torque_Filter[4]+ |
ArmandLambrechts | 2:373260769485 | 78 | ADC_Torque_Filter[5]+ |
ArmandLambrechts | 2:373260769485 | 79 | ADC_Torque_Filter[6]+ |
ArmandLambrechts | 2:373260769485 | 80 | ADC_Torque_Filter[7] |
ArmandLambrechts | 2:373260769485 | 81 | ) / 8); |
ArmandLambrechts | 2:373260769485 | 82 | // for (i = 0; i < 7; i++) //data van array leegmaken |
ArmandLambrechts | 2:373260769485 | 83 | // { |
ArmandLambrechts | 2:373260769485 | 84 | // ADC_Torque_Filter[i] = 0; |
ArmandLambrechts | 2:373260769485 | 85 | //wait(0.005); |
ArmandLambrechts | 2:373260769485 | 86 | //}*/ |
ArmandLambrechts | 2:373260769485 | 87 | |
ArmandLambrechts | 2:373260769485 | 88 | |
ArmandLambrechts | 2:373260769485 | 89 | send_data.data = (speed << 8) + reg; //shiften en optellen |
ArmandLambrechts | 1:12ddbe69b6e6 | 90 | send_status = can.write(CANMessage(send_id, (char*) &send_data, 3)); |
ArmandLambrechts | 0:d9f4fedf5253 | 91 | |
ArmandLambrechts | 0:d9f4fedf5253 | 92 | if (send_status == true) { |
ArmandLambrechts | 1:12ddbe69b6e6 | 93 | //send_data.data = send_data.data + 1; |
ArmandLambrechts | 1:12ddbe69b6e6 | 94 | |
ArmandLambrechts | 0:d9f4fedf5253 | 95 | } |
ArmandLambrechts | 0:d9f4fedf5253 | 96 | |
ArmandLambrechts | 0:d9f4fedf5253 | 97 | /* Receive */ |
ArmandLambrechts | 0:d9f4fedf5253 | 98 | if (can.read(msg)) { |
ArmandLambrechts | 0:d9f4fedf5253 | 99 | //move message bytes to receive, so we can access as a uint64_t (u long long) |
ArmandLambrechts | 0:d9f4fedf5253 | 100 | receive_data.bytes.b7 = msg.data[7]; |
ArmandLambrechts | 0:d9f4fedf5253 | 101 | receive_data.bytes.b6 = msg.data[6]; |
ArmandLambrechts | 0:d9f4fedf5253 | 102 | receive_data.bytes.b5 = msg.data[5]; |
ArmandLambrechts | 0:d9f4fedf5253 | 103 | receive_data.bytes.b4 = msg.data[4]; |
ArmandLambrechts | 0:d9f4fedf5253 | 104 | receive_data.bytes.b3 = msg.data[3]; |
ArmandLambrechts | 0:d9f4fedf5253 | 105 | receive_data.bytes.b2 = msg.data[2]; |
ArmandLambrechts | 0:d9f4fedf5253 | 106 | receive_data.bytes.b1 = msg.data[1]; |
ArmandLambrechts | 0:d9f4fedf5253 | 107 | receive_data.bytes.b0 = msg.data[0]; |
ArmandLambrechts | 0:d9f4fedf5253 | 108 | |
ArmandLambrechts | 0:d9f4fedf5253 | 109 | printf("Message received ID,Msg: %d, %llu\r\n", msg.id, receive_data.data); |
ArmandLambrechts | 0:d9f4fedf5253 | 110 | } |
ArmandLambrechts | 0:d9f4fedf5253 | 111 | wait(0.5); |
ArmandLambrechts | 2:373260769485 | 112 | } |
ArmandLambrechts | 0:d9f4fedf5253 | 113 | } |