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: DataControl
ikarashiCAN.h@3:906335cad175, 2022-09-09 (annotated)
- Committer:
- ikarashikota
- Date:
- Fri Sep 09 07:17:01 2022 +0000
- Revision:
- 3:906335cad175
- Parent:
- 0:b9571508f055
- Child:
- 4:d4c9f57a98c6
:-):-):-)
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ikarashikota | 0:b9571508f055 | 1 | /** @author 五十嵐幸多 |
| ikarashikota | 0:b9571508f055 | 2 | * |
| ikarashikota | 0:b9571508f055 | 3 | * 割と簡単にCAN通信ができるクラスです。 |
| ikarashikota | 0:b9571508f055 | 4 | * 通信初心者なので何かあったら、すぐ五十嵐へGO |
| ikarashikota | 0:b9571508f055 | 5 | */ |
| ikarashikota | 0:b9571508f055 | 6 | #ifndef IKARASHI_CAN |
| ikarashikota | 0:b9571508f055 | 7 | |
| ikarashikota | 0:b9571508f055 | 8 | #define IKARASHI_CAN |
| ikarashikota | 0:b9571508f055 | 9 | |
| ikarashikota | 0:b9571508f055 | 10 | #include "mbed.h" |
| ikarashikota | 0:b9571508f055 | 11 | #include "DataControl.h" |
| ikarashikota | 0:b9571508f055 | 12 | |
| ikarashikota | 0:b9571508f055 | 13 | /*サンプルコード |
| ikarashikota | 0:b9571508f055 | 14 | 2台のCANを使って通信するヤツです。 |
| ikarashikota | 0:b9571508f055 | 15 | |
| ikarashikota | 0:b9571508f055 | 16 | #include "mbed.h" |
| ikarashikota | 0:b9571508f055 | 17 | #include "ikarashiCAN.h" |
| ikarashikota | 0:b9571508f055 | 18 | |
| ikarashikota | 0:b9571508f055 | 19 | Serial pc(USBTX,USBRX,115200); |
| ikarashikota | 0:b9571508f055 | 20 | |
| ikarashikota | 0:b9571508f055 | 21 | InterruptIn button(USER_BUTTON); |
| ikarashikota | 0:b9571508f055 | 22 | DigitalOut led(LED2); |
| ikarashikota | 0:b9571508f055 | 23 | Ticker ticker; |
| ikarashikota | 0:b9571508f055 | 24 | |
| ikarashikota | 0:b9571508f055 | 25 | //ikarashiCAN ican1(D15, D14, 1, 50000); //sender |
| ikarashikota | 0:b9571508f055 | 26 | ikarashiCAN ican1(D15, D14, 1); //sender |
| ikarashikota | 0:b9571508f055 | 27 | ikarashiCAN ican2(D4, D10, 2, 50000); //reciever |
| ikarashikota | 0:b9571508f055 | 28 | |
| ikarashikota | 0:b9571508f055 | 29 | uint16_t data = 0; |
| ikarashikota | 0:b9571508f055 | 30 | |
| ikarashikota | 0:b9571508f055 | 31 | void pushed(){ |
| ikarashikota | 0:b9571508f055 | 32 | ican1.setData(999999); |
| ikarashikota | 0:b9571508f055 | 33 | } |
| ikarashikota | 0:b9571508f055 | 34 | void add(){ |
| ikarashikota | 0:b9571508f055 | 35 | |
| ikarashikota | 3:906335cad175 | 36 | led = !led; |
| ikarashikota | 0:b9571508f055 | 37 | |
| ikarashikota | 0:b9571508f055 | 38 | data++; |
| ikarashikota | 0:b9571508f055 | 39 | ican1.setData(data); |
| ikarashikota | 0:b9571508f055 | 40 | } |
| ikarashikota | 0:b9571508f055 | 41 | |
| ikarashikota | 0:b9571508f055 | 42 | int main() |
| ikarashikota | 0:b9571508f055 | 43 | { |
| ikarashikota | 0:b9571508f055 | 44 | |
| ikarashikota | 0:b9571508f055 | 45 | ticker.attach(&add, 1); |
| ikarashikota | 0:b9571508f055 | 46 | ican1.startSend(0.1); |
| ikarashikota | 0:b9571508f055 | 47 | ican2.startRecieve(); |
| ikarashikota | 0:b9571508f055 | 48 | led = 1; |
| ikarashikota | 0:b9571508f055 | 49 | button.mode(PullDown); |
| ikarashikota | 0:b9571508f055 | 50 | |
| ikarashikota | 0:b9571508f055 | 51 | while(1){ |
| ikarashikota | 0:b9571508f055 | 52 | button.fall(&pushed); |
| ikarashikota | 0:b9571508f055 | 53 | pc.printf("sdata: %d rdata: %d len: %d s: %d r: %d\n\r", data,ican2.simpleGetData(1), ican2.CanLen(), ican1.checkSender(), ican2.checkReciever()); |
| ikarashikota | 0:b9571508f055 | 54 | } |
| ikarashikota | 0:b9571508f055 | 55 | |
| ikarashikota | 0:b9571508f055 | 56 | } |
| ikarashikota | 0:b9571508f055 | 57 | |
| ikarashikota | 0:b9571508f055 | 58 | */ |
| ikarashikota | 0:b9571508f055 | 59 | |
| ikarashikota | 0:b9571508f055 | 60 | |
| ikarashikota | 0:b9571508f055 | 61 | /// CAN通信のクラスです |
| ikarashikota | 0:b9571508f055 | 62 | class ikarashiCAN : public DataControl |
| ikarashikota | 0:b9571508f055 | 63 | { |
| ikarashikota | 0:b9571508f055 | 64 | public: |
| ikarashikota | 0:b9571508f055 | 65 | |
| ikarashikota | 0:b9571508f055 | 66 | /** コンストラクタ |
| ikarashikota | 0:b9571508f055 | 67 | * |
| ikarashikota | 0:b9571508f055 | 68 | * @param rd CANのrdピン |
| ikarashikota | 0:b9571508f055 | 69 | * @param td CANのtdピン |
| ikarashikota | 0:b9571508f055 | 70 | * @param msgID CANのID |
| ikarashikota | 0:b9571508f055 | 71 | * @param frequency CANの周波数 |
| ikarashikota | 0:b9571508f055 | 72 | * @note nucleoでやる場合、frequencyは50000にしないとなぜか安定しません。 |
| ikarashikota | 0:b9571508f055 | 73 | * なので、frequencyを設定しなかったとき自動的に周波数が50000に設定されるようにしました。 |
| ikarashikota | 0:b9571508f055 | 74 | * もっといい方法があるかも... |
| ikarashikota | 0:b9571508f055 | 75 | */ |
| ikarashikota | 0:b9571508f055 | 76 | ikarashiCAN(PinName rd, PinName td, int msgID, int frequency); |
| ikarashikota | 0:b9571508f055 | 77 | ikarashiCAN(PinName rd, PinName td, int msgID); |
| ikarashikota | 0:b9571508f055 | 78 | |
| ikarashikota | 0:b9571508f055 | 79 | /** 送信するデータをセットする関数 |
| ikarashikota | 0:b9571508f055 | 80 | * |
| ikarashikota | 0:b9571508f055 | 81 | * 一度送信するデータをセットします。 |
| ikarashikota | 3:906335cad175 | 82 | * @typedef T 一旦値を保持するための型 |
| ikarashikota | 0:b9571508f055 | 83 | * @param _data 一旦値を保持する変数 |
| ikarashikota | 0:b9571508f055 | 84 | */ |
| ikarashikota | 0:b9571508f055 | 85 | template <class T> |
| ikarashikota | 0:b9571508f055 | 86 | void setData(T _data){ |
| ikarashikota | 0:b9571508f055 | 87 | dataSet(_data, sender); |
| ikarashikota | 0:b9571508f055 | 88 | } |
| ikarashikota | 0:b9571508f055 | 89 | |
| ikarashikota | 0:b9571508f055 | 90 | /** データを受け取る関数 |
| ikarashikota | 0:b9571508f055 | 91 | * |
| ikarashikota | 0:b9571508f055 | 92 | * 受信したデータを返します。 |
| ikarashikota | 0:b9571508f055 | 93 | * @return int 受信したデータ |
| ikarashikota | 0:b9571508f055 | 94 | */ |
| ikarashikota | 0:b9571508f055 | 95 | int getData(); |
| ikarashikota | 0:b9571508f055 | 96 | |
| ikarashikota | 0:b9571508f055 | 97 | /** お手軽関数 |
| ikarashikota | 0:b9571508f055 | 98 | * |
| ikarashikota | 0:b9571508f055 | 99 | * ほんとは色々処理を実行してからデータを取得しなきゃなんですけど、めんどくさいですよね! |
| ikarashikota | 0:b9571508f055 | 100 | * なので、簡単に受信したデータを取得できる関数を用意しました。 |
| ikarashikota | 0:b9571508f055 | 101 | * 特定のIDのデータを取得できます。 |
| ikarashikota | 0:b9571508f055 | 102 | * @param _id 受信したいCANのID |
| ikarashikota | 0:b9571508f055 | 103 | * @return int 受信したデータ |
| ikarashikota | 0:b9571508f055 | 104 | */ |
| ikarashikota | 0:b9571508f055 | 105 | int simpleGetData(int _id); |
| ikarashikota | 0:b9571508f055 | 106 | |
| ikarashikota | 0:b9571508f055 | 107 | ///セットしたデータを送信します。 |
| ikarashikota | 0:b9571508f055 | 108 | void send(); |
| ikarashikota | 0:b9571508f055 | 109 | |
| ikarashikota | 0:b9571508f055 | 110 | ///データを受信します。データの取得とは違います。 |
| ikarashikota | 0:b9571508f055 | 111 | void recieve(); |
| ikarashikota | 0:b9571508f055 | 112 | |
| ikarashikota | 0:b9571508f055 | 113 | /** attach関数 |
| ikarashikota | 0:b9571508f055 | 114 | * |
| ikarashikota | 0:b9571508f055 | 115 | * send関数のattachバージョン。 |
| ikarashikota | 0:b9571508f055 | 116 | * @param second 繰り返す間隔。単位は[s] |
| ikarashikota | 0:b9571508f055 | 117 | */ |
| ikarashikota | 0:b9571508f055 | 118 | void startSend(float second); |
| ikarashikota | 0:b9571508f055 | 119 | |
| ikarashikota | 0:b9571508f055 | 120 | /** attach関数 |
| ikarashikota | 0:b9571508f055 | 121 | * |
| ikarashikota | 0:b9571508f055 | 122 | * recieve関数のattachバージョン。データが送信されたときに自動で受信します。 |
| ikarashikota | 3:906335cad175 | 123 | * @attention mbed-os5以上だとバグるみたいです。mbed2を使うか、素のrecieve関数を使ってください |
| ikarashikota | 0:b9571508f055 | 124 | */ |
| ikarashikota | 0:b9571508f055 | 125 | void startRecieve(); |
| ikarashikota | 0:b9571508f055 | 126 | |
| ikarashikota | 0:b9571508f055 | 127 | ///データの送信が成功したときにtrueになります。 |
| ikarashikota | 0:b9571508f055 | 128 | bool checkSender(); |
| ikarashikota | 0:b9571508f055 | 129 | |
| ikarashikota | 0:b9571508f055 | 130 | ///データの受信に成功したときにtrueになります。 |
| ikarashikota | 0:b9571508f055 | 131 | bool checkReciever(); |
| ikarashikota | 0:b9571508f055 | 132 | |
| ikarashikota | 0:b9571508f055 | 133 | ///受信したデータのIDを返します。取得に失敗したときは-1を返します。 |
| ikarashikota | 0:b9571508f055 | 134 | int recievedCanID(); |
| ikarashikota | 0:b9571508f055 | 135 | |
| ikarashikota | 0:b9571508f055 | 136 | ///このクラスを適用しているCANのIDを返します。 |
| ikarashikota | 0:b9571508f055 | 137 | int myCanID(); |
| ikarashikota | 0:b9571508f055 | 138 | |
| ikarashikota | 0:b9571508f055 | 139 | ///受信したデータのサイズ(byte)を返します。 |
| ikarashikota | 0:b9571508f055 | 140 | int CanLen(); |
| ikarashikota | 0:b9571508f055 | 141 | |
| ikarashikota | 0:b9571508f055 | 142 | |
| ikarashikota | 0:b9571508f055 | 143 | void sender_reset(); |
| ikarashikota | 0:b9571508f055 | 144 | void reciever_reset(); |
| ikarashikota | 0:b9571508f055 | 145 | |
| ikarashikota | 0:b9571508f055 | 146 | |
| ikarashikota | 0:b9571508f055 | 147 | private: |
| ikarashikota | 0:b9571508f055 | 148 | |
| ikarashikota | 0:b9571508f055 | 149 | CAN can; |
| ikarashikota | 0:b9571508f055 | 150 | CANMessage msg; |
| ikarashikota | 0:b9571508f055 | 151 | int _msgID; |
| ikarashikota | 0:b9571508f055 | 152 | int sended_msgID; |
| ikarashikota | 0:b9571508f055 | 153 | |
| ikarashikota | 0:b9571508f055 | 154 | Ticker can_ticker; |
| ikarashikota | 0:b9571508f055 | 155 | |
| ikarashikota | 0:b9571508f055 | 156 | ///送信するデータの配列 |
| ikarashikota | 0:b9571508f055 | 157 | char sender[4]; |
| ikarashikota | 0:b9571508f055 | 158 | |
| ikarashikota | 0:b9571508f055 | 159 | ///受信したデータが格納される配列 |
| ikarashikota | 0:b9571508f055 | 160 | char reciever[4]; |
| ikarashikota | 0:b9571508f055 | 161 | |
| ikarashikota | 0:b9571508f055 | 162 | int _can_data; |
| ikarashikota | 0:b9571508f055 | 163 | bool can_s; |
| ikarashikota | 0:b9571508f055 | 164 | bool can_r; |
| ikarashikota | 0:b9571508f055 | 165 | |
| ikarashikota | 0:b9571508f055 | 166 | }; |
| ikarashikota | 0:b9571508f055 | 167 | |
| ikarashikota | 0:b9571508f055 | 168 | #endif |
| ikarashikota | 0:b9571508f055 | 169 |