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@8:e813155546a6, 2022-10-06 (annotated)
- Committer:
- ikarashikota
- Date:
- Thu Oct 06 23:34:56 2022 +0000
- Revision:
- 8:e813155546a6
- Parent:
- 7:b94c7d2eaed5
add ifOriginal function for some type of number
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 | 4:d4c9f57a98c6 | 16 | |
| ikarashikota | 0:b9571508f055 | 17 | #include "mbed.h" |
| ikarashikota | 0:b9571508f055 | 18 | #include "ikarashiCAN.h" |
| ikarashikota | 0:b9571508f055 | 19 | |
| ikarashikota | 0:b9571508f055 | 20 | Serial pc(USBTX,USBRX,115200); |
| ikarashikota | 0:b9571508f055 | 21 | InterruptIn button(USER_BUTTON); |
| ikarashikota | 0:b9571508f055 | 22 | Ticker ticker; |
| ikarashikota | 0:b9571508f055 | 23 | |
| ikarashikota | 0:b9571508f055 | 24 | //ikarashiCAN ican1(D15, D14, 1, 50000); //sender |
| ikarashikota | 0:b9571508f055 | 25 | ikarashiCAN ican1(D15, D14, 1); //sender |
| ikarashikota | 0:b9571508f055 | 26 | ikarashiCAN ican2(D4, D10, 2, 50000); //reciever |
| ikarashikota | 0:b9571508f055 | 27 | |
| ikarashikota | 0:b9571508f055 | 28 | uint16_t data = 0; |
| ikarashikota | 4:d4c9f57a98c6 | 29 | //bool btnFlag = 0; |
| ikarashikota | 0:b9571508f055 | 30 | |
| ikarashikota | 0:b9571508f055 | 31 | void pushed(){ |
| ikarashikota | 4:d4c9f57a98c6 | 32 | //btnFlag = !btnFlag; |
| ikarashikota | 4:d4c9f57a98c6 | 33 | data+=12; |
| ikarashikota | 0:b9571508f055 | 34 | } |
| ikarashikota | 0:b9571508f055 | 35 | |
| ikarashikota | 4:d4c9f57a98c6 | 36 | void can_update(){ |
| ikarashikota | 4:d4c9f57a98c6 | 37 | ican2.recieve(); |
| ikarashikota | 0:b9571508f055 | 38 | ican1.setData(data); |
| ikarashikota | 0:b9571508f055 | 39 | } |
| ikarashikota | 0:b9571508f055 | 40 | |
| ikarashikota | 0:b9571508f055 | 41 | int main() |
| ikarashikota | 0:b9571508f055 | 42 | { |
| ikarashikota | 0:b9571508f055 | 43 | |
| ikarashikota | 0:b9571508f055 | 44 | ican1.startSend(0.1); |
| ikarashikota | 4:d4c9f57a98c6 | 45 | |
| ikarashikota | 0:b9571508f055 | 46 | button.mode(PullDown); |
| ikarashikota | 0:b9571508f055 | 47 | |
| ikarashikota | 0:b9571508f055 | 48 | while(1){ |
| ikarashikota | 0:b9571508f055 | 49 | button.fall(&pushed); |
| ikarashikota | 4:d4c9f57a98c6 | 50 | can_update(); |
| ikarashikota | 0:b9571508f055 | 51 | 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 | 52 | } |
| ikarashikota | 0:b9571508f055 | 53 | |
| ikarashikota | 0:b9571508f055 | 54 | } |
| ikarashikota | 0:b9571508f055 | 55 | |
| ikarashikota | 0:b9571508f055 | 56 | */ |
| ikarashikota | 0:b9571508f055 | 57 | |
| ikarashikota | 0:b9571508f055 | 58 | |
| ikarashikota | 0:b9571508f055 | 59 | /// CAN通信のクラスです |
| ikarashikota | 0:b9571508f055 | 60 | class ikarashiCAN : public DataControl |
| ikarashikota | 0:b9571508f055 | 61 | { |
| ikarashikota | 0:b9571508f055 | 62 | public: |
| ikarashikota | 0:b9571508f055 | 63 | |
| ikarashikota | 0:b9571508f055 | 64 | /** コンストラクタ |
| ikarashikota | 0:b9571508f055 | 65 | * |
| ikarashikota | 0:b9571508f055 | 66 | * @param rd CANのrdピン |
| ikarashikota | 0:b9571508f055 | 67 | * @param td CANのtdピン |
| ikarashikota | 0:b9571508f055 | 68 | * @param msgID CANのID |
| ikarashikota | 0:b9571508f055 | 69 | * @param frequency CANの周波数 |
| ikarashikota | 0:b9571508f055 | 70 | * @note nucleoでやる場合、frequencyは50000にしないとなぜか安定しません。 |
| ikarashikota | 0:b9571508f055 | 71 | * なので、frequencyを設定しなかったとき自動的に周波数が50000に設定されるようにしました。 |
| ikarashikota | 4:d4c9f57a98c6 | 72 | * |
| ikarashikota | 0:b9571508f055 | 73 | */ |
| ikarashikota | 0:b9571508f055 | 74 | ikarashiCAN(PinName rd, PinName td, int msgID, int frequency); |
| ikarashikota | 0:b9571508f055 | 75 | ikarashiCAN(PinName rd, PinName td, int msgID); |
| ikarashikota | 0:b9571508f055 | 76 | |
| ikarashikota | 0:b9571508f055 | 77 | /** 送信するデータをセットする関数 |
| ikarashikota | 0:b9571508f055 | 78 | * |
| ikarashikota | 0:b9571508f055 | 79 | * 一度送信するデータをセットします。 |
| ikarashikota | 3:906335cad175 | 80 | * @typedef T 一旦値を保持するための型 |
| ikarashikota | 0:b9571508f055 | 81 | * @param _data 一旦値を保持する変数 |
| ikarashikota | 0:b9571508f055 | 82 | */ |
| ikarashikota | 0:b9571508f055 | 83 | template <class T> |
| ikarashikota | 0:b9571508f055 | 84 | void setData(T _data){ |
| ikarashikota | 0:b9571508f055 | 85 | dataSet(_data, sender); |
| ikarashikota | 0:b9571508f055 | 86 | } |
| ikarashikota | 0:b9571508f055 | 87 | |
| ikarashikota | 0:b9571508f055 | 88 | /** データを受け取る関数 |
| ikarashikota | 0:b9571508f055 | 89 | * |
| ikarashikota | 0:b9571508f055 | 90 | * 受信したデータを返します。 |
| ikarashikota | 0:b9571508f055 | 91 | * @return int 受信したデータ |
| ikarashikota | 0:b9571508f055 | 92 | */ |
| ikarashikota | 0:b9571508f055 | 93 | int getData(); |
| ikarashikota | 0:b9571508f055 | 94 | |
| ikarashikota | 0:b9571508f055 | 95 | /** お手軽関数 |
| ikarashikota | 0:b9571508f055 | 96 | * |
| ikarashikota | 0:b9571508f055 | 97 | * ほんとは色々処理を実行してからデータを取得しなきゃなんですけど、めんどくさいですよね! |
| ikarashikota | 0:b9571508f055 | 98 | * なので、簡単に受信したデータを取得できる関数を用意しました。 |
| ikarashikota | 0:b9571508f055 | 99 | * 特定のIDのデータを取得できます。 |
| ikarashikota | 0:b9571508f055 | 100 | * @param _id 受信したいCANのID |
| ikarashikota | 0:b9571508f055 | 101 | * @return int 受信したデータ |
| ikarashikota | 0:b9571508f055 | 102 | */ |
| ikarashikota | 0:b9571508f055 | 103 | int simpleGetData(int _id); |
| ikarashikota | 7:b94c7d2eaed5 | 104 | |
| ikarashikota | 7:b94c7d2eaed5 | 105 | int simpleGetData(); |
| 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 | 5:664bd71935c3 | 127 | /** オリジナル |
| ikarashikota | 5:664bd71935c3 | 128 | * |
| ikarashikota | 5:664bd71935c3 | 129 | * 引数の値を送信用配列にセットします。この値は下のGet関数で取得できます。 |
| ikarashikota | 5:664bd71935c3 | 130 | * 受信データの判別に使えるかも |
| ikarashikota | 5:664bd71935c3 | 131 | * @param free_num unsigned short型 |
| ikarashikota | 5:664bd71935c3 | 132 | */ |
| ikarashikota | 5:664bd71935c3 | 133 | void originalSet(unsigned short free_num); |
| ikarashikota | 5:664bd71935c3 | 134 | |
| ikarashikota | 5:664bd71935c3 | 135 | /** オリジナルのセット |
| ikarashikota | 5:664bd71935c3 | 136 | * |
| ikarashikota | 5:664bd71935c3 | 137 | * @return int originalSetでセットした値 |
| ikarashikota | 5:664bd71935c3 | 138 | */ |
| ikarashikota | 5:664bd71935c3 | 139 | int originalGet(); |
| ikarashikota | 5:664bd71935c3 | 140 | |
| ikarashikota | 8:e813155546a6 | 141 | void ifOriginalGet(int &buff, int original_num); |
| ikarashikota | 6:503b975c3c8e | 142 | |
| ikarashikota | 8:e813155546a6 | 143 | void ifOriginalGet(uint8_t &buff, int original_num); |
| ikarashikota | 8:e813155546a6 | 144 | |
| ikarashikota | 8:e813155546a6 | 145 | void ifOriginalGet(int16_t &buff, int original_num); |
| ikarashikota | 6:503b975c3c8e | 146 | |
| ikarashikota | 0:b9571508f055 | 147 | ///データの送信が成功したときにtrueになります。 |
| ikarashikota | 0:b9571508f055 | 148 | bool checkSender(); |
| ikarashikota | 0:b9571508f055 | 149 | |
| ikarashikota | 0:b9571508f055 | 150 | ///データの受信に成功したときにtrueになります。 |
| ikarashikota | 0:b9571508f055 | 151 | bool checkReciever(); |
| ikarashikota | 0:b9571508f055 | 152 | |
| ikarashikota | 0:b9571508f055 | 153 | ///受信したデータのIDを返します。取得に失敗したときは-1を返します。 |
| ikarashikota | 0:b9571508f055 | 154 | int recievedCanID(); |
| ikarashikota | 0:b9571508f055 | 155 | |
| ikarashikota | 0:b9571508f055 | 156 | ///このクラスを適用しているCANのIDを返します。 |
| ikarashikota | 0:b9571508f055 | 157 | int myCanID(); |
| ikarashikota | 0:b9571508f055 | 158 | |
| ikarashikota | 0:b9571508f055 | 159 | ///受信したデータのサイズ(byte)を返します。 |
| ikarashikota | 0:b9571508f055 | 160 | int CanLen(); |
| ikarashikota | 0:b9571508f055 | 161 | |
| ikarashikota | 0:b9571508f055 | 162 | |
| ikarashikota | 0:b9571508f055 | 163 | void sender_reset(); |
| ikarashikota | 0:b9571508f055 | 164 | void reciever_reset(); |
| ikarashikota | 0:b9571508f055 | 165 | |
| ikarashikota | 0:b9571508f055 | 166 | |
| ikarashikota | 0:b9571508f055 | 167 | private: |
| ikarashikota | 0:b9571508f055 | 168 | |
| ikarashikota | 0:b9571508f055 | 169 | CAN can; |
| ikarashikota | 0:b9571508f055 | 170 | CANMessage msg; |
| ikarashikota | 0:b9571508f055 | 171 | int _msgID; |
| ikarashikota | 0:b9571508f055 | 172 | int sended_msgID; |
| ikarashikota | 0:b9571508f055 | 173 | |
| ikarashikota | 0:b9571508f055 | 174 | Ticker can_ticker; |
| ikarashikota | 0:b9571508f055 | 175 | |
| ikarashikota | 0:b9571508f055 | 176 | ///送信するデータの配列 |
| ikarashikota | 5:664bd71935c3 | 177 | char sender[5]; |
| ikarashikota | 0:b9571508f055 | 178 | |
| ikarashikota | 0:b9571508f055 | 179 | ///受信したデータが格納される配列 |
| ikarashikota | 5:664bd71935c3 | 180 | char reciever[5]; |
| ikarashikota | 0:b9571508f055 | 181 | |
| ikarashikota | 0:b9571508f055 | 182 | int _can_data; |
| ikarashikota | 0:b9571508f055 | 183 | bool can_s; |
| ikarashikota | 0:b9571508f055 | 184 | bool can_r; |
| ikarashikota | 0:b9571508f055 | 185 | |
| ikarashikota | 0:b9571508f055 | 186 | }; |
| ikarashikota | 0:b9571508f055 | 187 | |
| ikarashikota | 0:b9571508f055 | 188 | #endif |
| ikarashikota | 0:b9571508f055 | 189 |