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@4:c154ba84b582, 2017-12-06 (annotated)
- Committer:
- TiboSTM
- Date:
- Wed Dec 06 09:46:46 2017 +0000
- Revision:
- 4:c154ba84b582
- Parent:
- 3:7530a7aca1ab
met motorfunctie
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); |
| TiboSTM | 4:c154ba84b582 | 17 | |
| ArmandLambrechts | 0:d9f4fedf5253 | 18 | |
| ArmandLambrechts | 0:d9f4fedf5253 | 19 | /* Structures to help pack and unpack the 8 CAN data bytes */ |
| ArmandLambrechts | 0:d9f4fedf5253 | 20 | typedef struct bytes_64 { |
| ArmandLambrechts | 0:d9f4fedf5253 | 21 | uint8_t b0; |
| ArmandLambrechts | 0:d9f4fedf5253 | 22 | uint8_t b1; |
| ArmandLambrechts | 0:d9f4fedf5253 | 23 | uint8_t b2; |
| ArmandLambrechts | 0:d9f4fedf5253 | 24 | uint8_t b3; |
| ArmandLambrechts | 0:d9f4fedf5253 | 25 | uint8_t b4; |
| ArmandLambrechts | 0:d9f4fedf5253 | 26 | uint8_t b5; |
| ArmandLambrechts | 0:d9f4fedf5253 | 27 | uint8_t b6; |
| ArmandLambrechts | 0:d9f4fedf5253 | 28 | uint8_t b7; |
| ArmandLambrechts | 0:d9f4fedf5253 | 29 | } bytes_64; |
| ArmandLambrechts | 0:d9f4fedf5253 | 30 | |
| ArmandLambrechts | 0:d9f4fedf5253 | 31 | typedef union { |
| ArmandLambrechts | 0:d9f4fedf5253 | 32 | uint64_t data; |
| ArmandLambrechts | 0:d9f4fedf5253 | 33 | bytes_64 bytes; |
| ArmandLambrechts | 0:d9f4fedf5253 | 34 | } data_packed; |
| ArmandLambrechts | 0:d9f4fedf5253 | 35 | |
| TiboSTM | 4:c154ba84b582 | 36 | int send_id = 513; |
| TiboSTM | 4:c154ba84b582 | 37 | data_packed send_data; |
| TiboSTM | 4:c154ba84b582 | 38 | int reg = 0x31; //Id motorcontroller 0x201 -> 513 //Voor moving average te berekenen |
| TiboSTM | 4:c154ba84b582 | 39 | |
| TiboSTM | 4:c154ba84b582 | 40 | |
| ArmandLambrechts | 2:373260769485 | 41 | long map(long x, long in_min, long in_max, long out_min, long out_max) //MAP functie voor verschalingen. |
| ArmandLambrechts | 2:373260769485 | 42 | { |
| ArmandLambrechts | 2:373260769485 | 43 | return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; |
| ArmandLambrechts | 2:373260769485 | 44 | } |
| TiboSTM | 4:c154ba84b582 | 45 | |
| TiboSTM | 4:c154ba84b582 | 46 | |
| TiboSTM | 4:c154ba84b582 | 47 | void MOTOR_Calculate_Power(int speed) |
| TiboSTM | 4:c154ba84b582 | 48 | { |
| TiboSTM | 4:c154ba84b582 | 49 | |
| TiboSTM | 4:c154ba84b582 | 50 | send_data.data = (speed << 8) + reg; //shiften en optellen |
| TiboSTM | 4:c154ba84b582 | 51 | can.write(CANMessage(send_id, (char*) &send_data, 3)); |
| TiboSTM | 4:c154ba84b582 | 52 | } |
| TiboSTM | 4:c154ba84b582 | 53 | |
| ArmandLambrechts | 0:d9f4fedf5253 | 54 | int main() { |
| ArmandLambrechts | 0:d9f4fedf5253 | 55 | |
| ArmandLambrechts | 0:d9f4fedf5253 | 56 | printf("\r\n---Start---\r\n"); |
| ArmandLambrechts | 0:d9f4fedf5253 | 57 | |
| ArmandLambrechts | 2:373260769485 | 58 | data_packed send_data; //struct for send message |
| ArmandLambrechts | 2:373260769485 | 59 | data_packed receive_data; //struct for rcv message |
| ArmandLambrechts | 2:373260769485 | 60 | CANMessage msg; //message object for rcv |
| TiboSTM | 4:c154ba84b582 | 61 | int send_id = 513; //Id motorcontroller 0x201 -> 513 //Voor moving average te berekenen |
| ArmandLambrechts | 2:373260769485 | 62 | canEN = 0; //Voor de tranceiver |
| ArmandLambrechts | 0:d9f4fedf5253 | 63 | |
| ArmandLambrechts | 2:373260769485 | 64 | //send_data.data = 0x0CCD31; //0CCD snelheid motor, 31 register in controller. WERKT |
| ArmandLambrechts | 0:d9f4fedf5253 | 65 | send_data.data = 0; |
| ArmandLambrechts | 2:373260769485 | 66 | can.frequency(1000000); //1m freq |
| ArmandLambrechts | 1:12ddbe69b6e6 | 67 | can.mode(CAN::LocalTest); |
| ArmandLambrechts | 2:373260769485 | 68 | //can.mode(CAN::Normal); //1vd2 comment |
| ArmandLambrechts | 2:373260769485 | 69 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| ArmandLambrechts | 2:373260769485 | 70 | int speed = 0; //Snelheid naar de motor |
| ArmandLambrechts | 2:373260769485 | 71 | int reg = 0x31; //register in de controller |
| ArmandLambrechts | 2:373260769485 | 72 | float meas_r; |
| ArmandLambrechts | 2:373260769485 | 73 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| ArmandLambrechts | 0:d9f4fedf5253 | 74 | while (true) { |
| ArmandLambrechts | 2:373260769485 | 75 | meas_r = analog_value.read() * 100; |
| ArmandLambrechts | 2:373260769485 | 76 | speed = map(meas_r, 0, 100, 0, 0x7FFF); //waarde schommelt niet |
| ArmandLambrechts | 2:373260769485 | 77 | |
| TiboSTM | 4:c154ba84b582 | 78 | |
| TiboSTM | 4:c154ba84b582 | 79 | MOTOR_Calculate_Power(speed); |
| ArmandLambrechts | 2:373260769485 | 80 | |
| TiboSTM | 4:c154ba84b582 | 81 | |
| ArmandLambrechts | 0:d9f4fedf5253 | 82 | |
| TiboSTM | 4:c154ba84b582 | 83 | |
| ArmandLambrechts | 0:d9f4fedf5253 | 84 | |
| ArmandLambrechts | 0:d9f4fedf5253 | 85 | /* Receive */ |
| ArmandLambrechts | 0:d9f4fedf5253 | 86 | if (can.read(msg)) { |
| ArmandLambrechts | 0:d9f4fedf5253 | 87 | //move message bytes to receive, so we can access as a uint64_t (u long long) |
| ArmandLambrechts | 0:d9f4fedf5253 | 88 | receive_data.bytes.b7 = msg.data[7]; |
| ArmandLambrechts | 0:d9f4fedf5253 | 89 | receive_data.bytes.b6 = msg.data[6]; |
| ArmandLambrechts | 0:d9f4fedf5253 | 90 | receive_data.bytes.b5 = msg.data[5]; |
| ArmandLambrechts | 0:d9f4fedf5253 | 91 | receive_data.bytes.b4 = msg.data[4]; |
| ArmandLambrechts | 0:d9f4fedf5253 | 92 | receive_data.bytes.b3 = msg.data[3]; |
| ArmandLambrechts | 0:d9f4fedf5253 | 93 | receive_data.bytes.b2 = msg.data[2]; |
| ArmandLambrechts | 0:d9f4fedf5253 | 94 | receive_data.bytes.b1 = msg.data[1]; |
| ArmandLambrechts | 0:d9f4fedf5253 | 95 | receive_data.bytes.b0 = msg.data[0]; |
| ArmandLambrechts | 0:d9f4fedf5253 | 96 | |
| ArmandLambrechts | 0:d9f4fedf5253 | 97 | printf("Message received ID,Msg: %d, %llu\r\n", msg.id, receive_data.data); |
| ArmandLambrechts | 0:d9f4fedf5253 | 98 | } |
| ArmandLambrechts | 0:d9f4fedf5253 | 99 | wait(0.5); |
| ArmandLambrechts | 2:373260769485 | 100 | } |
| ArmandLambrechts | 0:d9f4fedf5253 | 101 | } |