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.
Dependents: 2019_SwitchNode_AR ArmNode 2919_LineSensor 2019_MD ... more
Mycan.cpp@1:e632e292a8c2, 2018-07-04 (annotated)
- Committer:
- Kirua
- Date:
- Wed Jul 04 07:49:29 2018 +0000
- Revision:
- 1:e632e292a8c2
- Parent:
- 0:ce8efac4c651
- Child:
- 2:67a12201e80b
????????????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kirua | 0:ce8efac4c651 | 1 | #include "mbed.h" |
Kirua | 0:ce8efac4c651 | 2 | #include "Mycan.h" |
Kirua | 0:ce8efac4c651 | 3 | |
Kirua | 0:ce8efac4c651 | 4 | Mycan::Mycan(PinName _pin_rd, PinName _pin_td) : can(_pin_rd, _pin_td) |
Kirua | 0:ce8efac4c651 | 5 | { |
Kirua | 0:ce8efac4c651 | 6 | min_id = 1;//ここで最小IDを設定 |
Kirua | 0:ce8efac4c651 | 7 | }; |
Kirua | 0:ce8efac4c651 | 8 | |
Kirua | 0:ce8efac4c651 | 9 | void Mycan::set(unsigned int _id, int _num, short int _data) |
Kirua | 0:ce8efac4c651 | 10 | { |
Kirua | 0:ce8efac4c651 | 11 | td_id = _id; |
Kirua | 0:ce8efac4c651 | 12 | td_num = _num; |
Kirua | 0:ce8efac4c651 | 13 | data = _data; |
Kirua | 0:ce8efac4c651 | 14 | write_val[td_num] = data; |
Kirua | 1:e632e292a8c2 | 15 | _expressAbsoluteValue();//絶対値にする関数 |
Kirua | 0:ce8efac4c651 | 16 | } |
Kirua | 0:ce8efac4c651 | 17 | |
Kirua | 0:ce8efac4c651 | 18 | void Mycan::send() |
Kirua | 0:ce8efac4c651 | 19 | { |
Kirua | 0:ce8efac4c651 | 20 | can.write(CANMessage(td_id, (char*)&td_data, 8)); |
Kirua | 0:ce8efac4c651 | 21 | } |
Kirua | 0:ce8efac4c651 | 22 | |
Kirua | 0:ce8efac4c651 | 23 | void Mycan::read() |
Kirua | 0:ce8efac4c651 | 24 | { |
Kirua | 0:ce8efac4c651 | 25 | CANMessage received; |
Kirua | 0:ce8efac4c651 | 26 | can.read(received); |
Kirua | 0:ce8efac4c651 | 27 | rd_data = *(read_can *)received.data; |
Kirua | 1:e632e292a8c2 | 28 | _expressSignVal(received.id);//符号を付与する関数 |
Kirua | 0:ce8efac4c651 | 29 | } |
Kirua | 0:ce8efac4c651 | 30 | |
Kirua | 0:ce8efac4c651 | 31 | float Mycan::get(unsigned int _id, int _num) |
Kirua | 0:ce8efac4c651 | 32 | { |
Kirua | 0:ce8efac4c651 | 33 | rd_id = _id; |
Kirua | 0:ce8efac4c651 | 34 | rd_num = _num; |
Kirua | 0:ce8efac4c651 | 35 | return read_val[rd_id - min_id][rd_num]; |
Kirua | 0:ce8efac4c651 | 36 | } |
Kirua | 0:ce8efac4c651 | 37 | |
Kirua | 1:e632e292a8c2 | 38 | void Mycan::_expressAbsoluteValue() |
Kirua | 0:ce8efac4c651 | 39 | { |
Kirua | 0:ce8efac4c651 | 40 | for (int i = 1; i < 8; i++) |
Kirua | 0:ce8efac4c651 | 41 | { |
Kirua | 0:ce8efac4c651 | 42 | if (write_val[i] > 0) |
Kirua | 0:ce8efac4c651 | 43 | write_val[0] &= ~(1 << i); |
Kirua | 0:ce8efac4c651 | 44 | else |
Kirua | 0:ce8efac4c651 | 45 | write_val[0] |= (1 << i); |
Kirua | 0:ce8efac4c651 | 46 | } |
Kirua | 0:ce8efac4c651 | 47 | td_data.writeVal_0 = write_val[0]; |
Kirua | 0:ce8efac4c651 | 48 | td_data.writeVal_1 = abs(write_val[1]); |
Kirua | 0:ce8efac4c651 | 49 | td_data.writeVal_2 = abs(write_val[2]); |
Kirua | 0:ce8efac4c651 | 50 | td_data.writeVal_3 = abs(write_val[3]); |
Kirua | 0:ce8efac4c651 | 51 | td_data.writeVal_4 = abs(write_val[4]); |
Kirua | 0:ce8efac4c651 | 52 | td_data.writeVal_5 = abs(write_val[5]); |
Kirua | 0:ce8efac4c651 | 53 | td_data.writeVal_6 = abs(write_val[6]); |
Kirua | 0:ce8efac4c651 | 54 | td_data.writeVal_7 = abs(write_val[7]); |
Kirua | 0:ce8efac4c651 | 55 | } |
Kirua | 0:ce8efac4c651 | 56 | |
Kirua | 1:e632e292a8c2 | 57 | void Mycan::_expressSignVal(unsigned int id) |
Kirua | 0:ce8efac4c651 | 58 | { |
Kirua | 0:ce8efac4c651 | 59 | read_val[id - min_id][0] = rd_data.readVal_0; |
Kirua | 0:ce8efac4c651 | 60 | read_val[id - min_id][1] = rd_data.readVal_1; |
Kirua | 0:ce8efac4c651 | 61 | read_val[id - min_id][2] = rd_data.readVal_2; |
Kirua | 0:ce8efac4c651 | 62 | read_val[id - min_id][3] = rd_data.readVal_3; |
Kirua | 0:ce8efac4c651 | 63 | read_val[id - min_id][4] = rd_data.readVal_4; |
Kirua | 0:ce8efac4c651 | 64 | read_val[id - min_id][5] = rd_data.readVal_5; |
Kirua | 0:ce8efac4c651 | 65 | read_val[id - min_id][6] = rd_data.readVal_6; |
Kirua | 0:ce8efac4c651 | 66 | read_val[id - min_id][7] = rd_data.readVal_7; |
Kirua | 0:ce8efac4c651 | 67 | |
Kirua | 0:ce8efac4c651 | 68 | for (int j = 1; j < 8; j++) |
Kirua | 0:ce8efac4c651 | 69 | { |
Kirua | 0:ce8efac4c651 | 70 | if (read_val[id - min_id][0] & (1 << j)) |
Kirua | 0:ce8efac4c651 | 71 | read_val[id - min_id][j] *= -1; |
Kirua | 0:ce8efac4c651 | 72 | } |
Kirua | 0:ce8efac4c651 | 73 | } |
Kirua | 0:ce8efac4c651 | 74 | |
Kirua | 0:ce8efac4c651 | 75 | short Mycan::read_val[10][8]; |
Kirua | 0:ce8efac4c651 | 76 | short Mycan::write_val[8]; |