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: EthernetInterface mbed-rtos mbed
Fork of mbed_driver_ver_0_2_0 by
main.cpp@1:b1c3d31236f8, 2018-10-22 (annotated)
- Committer:
- yukisega
- Date:
- Mon Oct 22 15:47:42 2018 +0000
- Revision:
- 1:b1c3d31236f8
- Parent:
- 0:c490b05d753e
- Child:
- 2:233951f9354d
ethernet???????; ???????????????????????????
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| yukisega | 0:c490b05d753e | 1 | |
| yukisega | 0:c490b05d753e | 2 | /** |
| yukisega | 0:c490b05d753e | 3 | |
| yukisega | 0:c490b05d753e | 4 | @file main.cpp |
| yukisega | 0:c490b05d753e | 5 | @brief Thrust Motor Controller 2018 edition ver. 0.2.0 |
| yukisega | 0:c490b05d753e | 6 | \mainpage Thrust Motor Controller 2018 edition |
| yukisega | 0:c490b05d753e | 7 | TCPSocket経由でスラスターを制御するプログラムです。 |
| yukisega | 0:c490b05d753e | 8 | DHCPサーバとしては機能しません。 |
| yukisega | 0:c490b05d753e | 9 | 固定IPを割り振る必要があります。 |
| yukisega | 0:c490b05d753e | 10 | \htmlonly |
| yukisega | 0:c490b05d753e | 11 | <font color="red">mbedの電源を投入する前に,あらかじめクライアントのコンピュータを接続しておく必要があります。</font> |
| yukisega | 0:c490b05d753e | 12 | \endhtmlonly |
| yukisega | 0:c490b05d753e | 13 | @sa ethernet_thread |
| yukisega | 0:c490b05d753e | 14 | @author Wada Suisei Copyright (C) 2018 - All rights reserved |
| yukisega | 0:c490b05d753e | 15 | @author modified by yukimakura |
| yukisega | 0:c490b05d753e | 16 | |
| yukisega | 1:b1c3d31236f8 | 17 | @date LastModified:2018 Oct 23 |
| yukisega | 0:c490b05d753e | 18 | */ |
| yukisega | 0:c490b05d753e | 19 | |
| yukisega | 0:c490b05d753e | 20 | /*** Including Libraries ***/ |
| yukisega | 0:c490b05d753e | 21 | #include "mbed.h" |
| yukisega | 0:c490b05d753e | 22 | #include "EthernetInterface.h" |
| yukisega | 0:c490b05d753e | 23 | #include "rtos.h" |
| yukisega | 0:c490b05d753e | 24 | |
| yukisega | 0:c490b05d753e | 25 | |
| yukisega | 0:c490b05d753e | 26 | /** |
| yukisega | 0:c490b05d753e | 27 | * @brief PWMの周期(秒) |
| yukisega | 0:c490b05d753e | 28 | * |
| yukisega | 0:c490b05d753e | 29 | */ |
| yukisega | 0:c490b05d753e | 30 | #define PWM_FREQ 0.01 |
| yukisega | 0:c490b05d753e | 31 | |
| yukisega | 0:c490b05d753e | 32 | /** |
| yukisega | 0:c490b05d753e | 33 | * @brief 接続のタイムアウト(ミリ秒) |
| yukisega | 0:c490b05d753e | 34 | * |
| yukisega | 0:c490b05d753e | 35 | */ |
| yukisega | 1:b1c3d31236f8 | 36 | #define TIMEOUT_MS 1000 |
| yukisega | 0:c490b05d753e | 37 | |
| yukisega | 0:c490b05d753e | 38 | |
| yukisega | 0:c490b05d753e | 39 | /*** Network Information ***/ |
| yukisega | 0:c490b05d753e | 40 | ///myIpAddrこのモータドライバのIPアドレスを入力 |
| yukisega | 1:b1c3d31236f8 | 41 | #define myIpAddr "10.42.0.98" |
| yukisega | 0:c490b05d753e | 42 | /// myPortこのプログラムで使用するポートを入力 |
| yukisega | 1:b1c3d31236f8 | 43 | #define myPort 50000 |
| yukisega | 0:c490b05d753e | 44 | /// NetMask このモータドライバ用のネットマスクを入力 |
| yukisega | 1:b1c3d31236f8 | 45 | #define NetMask "255.255.255.0" |
| yukisega | 0:c490b05d753e | 46 | /// Gateway このモータドライバ用のゲートウェイを入力 |
| yukisega | 1:b1c3d31236f8 | 47 | #define Gateway "10.42.0.1" |
| yukisega | 0:c490b05d753e | 48 | |
| yukisega | 0:c490b05d753e | 49 | |
| yukisega | 0:c490b05d753e | 50 | /*** Instantiation ***/ |
| yukisega | 0:c490b05d753e | 51 | Timer Timeout_timer; |
| yukisega | 0:c490b05d753e | 52 | |
| yukisega | 0:c490b05d753e | 53 | //! PWM信号の差動出力ピンを指定 |
| yukisega | 0:c490b05d753e | 54 | PwmOut motor_P(p26); |
| yukisega | 0:c490b05d753e | 55 | //! PWM信号の差動出力ピンを指定 |
| yukisega | 0:c490b05d753e | 56 | PwmOut motor_N(p25); |
| yukisega | 0:c490b05d753e | 57 | |
| yukisega | 1:b1c3d31236f8 | 58 | Thread *(eth_pri); |
| yukisega | 1:b1c3d31236f8 | 59 | |
| yukisega | 0:c490b05d753e | 60 | double pipe_pwmduty; |
| yukisega | 0:c490b05d753e | 61 | |
| yukisega | 1:b1c3d31236f8 | 62 | Mutex g_mtx; |
| yukisega | 0:c490b05d753e | 63 | |
| yukisega | 0:c490b05d753e | 64 | //Network |
| yukisega | 0:c490b05d753e | 65 | DigitalOut EthRecv_LED(LED1); |
| yukisega | 0:c490b05d753e | 66 | DigitalOut EthSend_LED(LED2); |
| yukisega | 0:c490b05d753e | 67 | DigitalOut Ethconnect_LED(LED4); |
| yukisega | 0:c490b05d753e | 68 | EthernetInterface eth; |
| yukisega | 0:c490b05d753e | 69 | |
| yukisega | 0:c490b05d753e | 70 | |
| yukisega | 0:c490b05d753e | 71 | |
| yukisega | 0:c490b05d753e | 72 | |
| yukisega | 0:c490b05d753e | 73 | |
| yukisega | 0:c490b05d753e | 74 | /** |
| yukisega | 0:c490b05d753e | 75 | * |
| yukisega | 0:c490b05d753e | 76 | * @brief コネクション時に重大なエラーが発生した時に呼び出されます |
| yukisega | 0:c490b05d753e | 77 | * |
| yukisega | 0:c490b05d753e | 78 | * pwmをduty比それぞれ50%にしてから、 |
| yukisega | 0:c490b05d753e | 79 | * mbedOSに−1を返します。 |
| yukisega | 0:c490b05d753e | 80 | * \htmlonly |
| yukisega | 0:c490b05d753e | 81 | * <font color="red">こうなった場合は再起動が必要です。</font> |
| yukisega | 0:c490b05d753e | 82 | * \endhtmlonly |
| yukisega | 0:c490b05d753e | 83 | * |
| yukisega | 0:c490b05d753e | 84 | * |
| yukisega | 0:c490b05d753e | 85 | */ |
| yukisega | 1:b1c3d31236f8 | 86 | void timeout(void){ |
| yukisega | 1:b1c3d31236f8 | 87 | g_mtx.lock(); |
| yukisega | 0:c490b05d753e | 88 | pipe_pwmduty = 0.0; |
| yukisega | 1:b1c3d31236f8 | 89 | g_mtx.unlock(); |
| yukisega | 0:c490b05d753e | 90 | motor_P = 0.5; |
| yukisega | 0:c490b05d753e | 91 | motor_N = 0.5; |
| yukisega | 0:c490b05d753e | 92 | } |
| yukisega | 0:c490b05d753e | 93 | |
| yukisega | 0:c490b05d753e | 94 | |
| yukisega | 0:c490b05d753e | 95 | |
| yukisega | 0:c490b05d753e | 96 | /** |
| yukisega | 0:c490b05d753e | 97 | * |
| yukisega | 0:c490b05d753e | 98 | * @brief TCP/IPソケットでコミュニケーションかつデータ受信を行います |
| yukisega | 0:c490b05d753e | 99 | * |
| yukisega | 0:c490b05d753e | 100 | * RTOSによって管理されているスレッドで, |
| yukisega | 0:c490b05d753e | 101 | * TCPSocketの確立前にデータリンク層あたりでコネクションされていないと |
| yukisega | 0:c490b05d753e | 102 | * mbedOSに−1を返します。 |
| yukisega | 0:c490b05d753e | 103 | * \htmlonly |
| yukisega | 0:c490b05d753e | 104 | * <font color="red">mbedの電源を投入する前に,あらかじめクライアントのコンピュータを接続しておく必要があります。</font> |
| yukisega | 0:c490b05d753e | 105 | * \endhtmlonly |
| yukisega | 0:c490b05d753e | 106 | */ |
| yukisega | 0:c490b05d753e | 107 | void ethernet_thread(void){ |
| yukisega | 1:b1c3d31236f8 | 108 | char in_buf[64]; |
| yukisega | 0:c490b05d753e | 109 | |
| yukisega | 1:b1c3d31236f8 | 110 | EthernetInterface eth; |
| yukisega | 1:b1c3d31236f8 | 111 | //eth.init(); |
| yukisega | 1:b1c3d31236f8 | 112 | eth.init(myIpAddr,NetMask,Gateway); |
| yukisega | 1:b1c3d31236f8 | 113 | eth.connect(); |
| yukisega | 1:b1c3d31236f8 | 114 | printf("\nServer IP Address is %s\n", eth.getIPAddress()); |
| yukisega | 1:b1c3d31236f8 | 115 | |
| yukisega | 1:b1c3d31236f8 | 116 | TCPSocketServer server; |
| yukisega | 1:b1c3d31236f8 | 117 | server.bind(myPort); |
| yukisega | 1:b1c3d31236f8 | 118 | server.listen(); |
| yukisega | 1:b1c3d31236f8 | 119 | |
| yukisega | 0:c490b05d753e | 120 | while (true) { |
| yukisega | 1:b1c3d31236f8 | 121 | printf("\nWait for new connection...\n"); |
| yukisega | 1:b1c3d31236f8 | 122 | TCPSocketConnection client; |
| yukisega | 1:b1c3d31236f8 | 123 | server.accept(client); |
| yukisega | 1:b1c3d31236f8 | 124 | //client.set_blocking(false, TIMEOUT_MS); // Timeout after (TIMEOUT_MS)ms |
| yukisega | 1:b1c3d31236f8 | 125 | |
| yukisega | 1:b1c3d31236f8 | 126 | printf("Connection from: %s\n", client.get_address()); |
| yukisega | 1:b1c3d31236f8 | 127 | |
| yukisega | 1:b1c3d31236f8 | 128 | while (client.is_connected()) { |
| yukisega | 1:b1c3d31236f8 | 129 | |
| yukisega | 0:c490b05d753e | 130 | Ethconnect_LED = 1; |
| yukisega | 1:b1c3d31236f8 | 131 | int len = client.receive(in_buf, sizeof(in_buf)); |
| yukisega | 1:b1c3d31236f8 | 132 | EthRecv_LED = !EthRecv_LED; |
| yukisega | 1:b1c3d31236f8 | 133 | // print received message to terminal |
| yukisega | 1:b1c3d31236f8 | 134 | in_buf[len] = '\0'; |
| yukisega | 1:b1c3d31236f8 | 135 | if(len > 0){ |
| yukisega | 1:b1c3d31236f8 | 136 | // Timeout_timer.reset(); |
| yukisega | 1:b1c3d31236f8 | 137 | in_buf[len] = '\0'; |
| yukisega | 1:b1c3d31236f8 | 138 | //Get data |
| yukisega | 1:b1c3d31236f8 | 139 | EthRecv_LED = !EthRecv_LED; |
| yukisega | 1:b1c3d31236f8 | 140 | g_mtx.lock(); |
| yukisega | 1:b1c3d31236f8 | 141 | memcpy(&pipe_pwmduty, &in_buf[0], 8); |
| yukisega | 1:b1c3d31236f8 | 142 | //printf("Received message from Client :'%f'\n",pipe_pwmduty); |
| yukisega | 1:b1c3d31236f8 | 143 | g_mtx.unlock(); |
| yukisega | 1:b1c3d31236f8 | 144 | Timeout_timer.reset(); |
| yukisega | 1:b1c3d31236f8 | 145 | } |
| yukisega | 1:b1c3d31236f8 | 146 | if(Timeout_timer.read_ms() > TIMEOUT_MS){ |
| yukisega | 1:b1c3d31236f8 | 147 | break; |
| yukisega | 0:c490b05d753e | 148 | } |
| yukisega | 0:c490b05d753e | 149 | } |
| yukisega | 1:b1c3d31236f8 | 150 | printf("\nclient closed\n"); |
| yukisega | 1:b1c3d31236f8 | 151 | Ethconnect_LED = 0; |
| yukisega | 1:b1c3d31236f8 | 152 | timeout(); |
| yukisega | 1:b1c3d31236f8 | 153 | client.close(); |
| yukisega | 0:c490b05d753e | 154 | } |
| yukisega | 0:c490b05d753e | 155 | } |
| yukisega | 0:c490b05d753e | 156 | |
| yukisega | 0:c490b05d753e | 157 | /** |
| yukisega | 0:c490b05d753e | 158 | * @brief エントリーポイント |
| yukisega | 0:c490b05d753e | 159 | * |
| yukisega | 0:c490b05d753e | 160 | * @return OSに返します。 |
| yukisega | 0:c490b05d753e | 161 | */ |
| yukisega | 0:c490b05d753e | 162 | int main(){ |
| yukisega | 1:b1c3d31236f8 | 163 | Timeout_timer.start(); |
| yukisega | 0:c490b05d753e | 164 | /* Declaration of Local Variables */ |
| yukisega | 0:c490b05d753e | 165 | float pwmduty; |
| yukisega | 0:c490b05d753e | 166 | |
| yukisega | 0:c490b05d753e | 167 | /* Initializing Variables */ |
| yukisega | 0:c490b05d753e | 168 | EthRecv_LED = 0; |
| yukisega | 0:c490b05d753e | 169 | EthSend_LED = 0; |
| yukisega | 0:c490b05d753e | 170 | |
| yukisega | 0:c490b05d753e | 171 | /* Initializing Interruption */ |
| yukisega | 0:c490b05d753e | 172 | |
| yukisega | 0:c490b05d753e | 173 | |
| yukisega | 0:c490b05d753e | 174 | |
| yukisega | 0:c490b05d753e | 175 | /* Initializing PWM Out */ |
| yukisega | 0:c490b05d753e | 176 | motor_P.period (PWM_FREQ); |
| yukisega | 0:c490b05d753e | 177 | motor_N.period (PWM_FREQ); |
| yukisega | 0:c490b05d753e | 178 | motor_P = 0.5; |
| yukisega | 0:c490b05d753e | 179 | motor_N = 0.5; |
| yukisega | 0:c490b05d753e | 180 | |
| yukisega | 0:c490b05d753e | 181 | /* Start Thread */ |
| yukisega | 0:c490b05d753e | 182 | Thread thread_get(ethernet_thread); |
| yukisega | 1:b1c3d31236f8 | 183 | eth_pri = &thread_get; |
| yukisega | 1:b1c3d31236f8 | 184 | eth_pri -> set_priority(osPriorityHigh); |
| yukisega | 0:c490b05d753e | 185 | |
| yukisega | 0:c490b05d753e | 186 | /* Main Routine */ |
| yukisega | 0:c490b05d753e | 187 | while(true){ |
| yukisega | 0:c490b05d753e | 188 | //disconnect timeout |
| yukisega | 0:c490b05d753e | 189 | if(Timeout_timer.read_ms() > TIMEOUT_MS){ |
| yukisega | 1:b1c3d31236f8 | 190 | g_mtx.lock(); |
| yukisega | 1:b1c3d31236f8 | 191 | pipe_pwmduty = 0.0; |
| yukisega | 1:b1c3d31236f8 | 192 | g_mtx.unlock(); |
| yukisega | 1:b1c3d31236f8 | 193 | timeout(); |
| yukisega | 0:c490b05d753e | 194 | EthRecv_LED = 0; |
| yukisega | 0:c490b05d753e | 195 | EthSend_LED = 0; |
| yukisega | 0:c490b05d753e | 196 | Ethconnect_LED = 0; |
| yukisega | 1:b1c3d31236f8 | 197 | printf("%d\n",Timeout_timer.read_ms()); |
| yukisega | 1:b1c3d31236f8 | 198 | Timeout_timer.reset(); |
| yukisega | 0:c490b05d753e | 199 | //return -1; |
| yukisega | 0:c490b05d753e | 200 | } |
| yukisega | 0:c490b05d753e | 201 | |
| yukisega | 0:c490b05d753e | 202 | /* Calculate PWM Duty */ |
| yukisega | 1:b1c3d31236f8 | 203 | g_mtx.lock(); |
| yukisega | 0:c490b05d753e | 204 | pwmduty = pipe_pwmduty / 2.0; |
| yukisega | 1:b1c3d31236f8 | 205 | g_mtx.unlock(); |
| yukisega | 0:c490b05d753e | 206 | |
| yukisega | 0:c490b05d753e | 207 | /* Output PWM */ |
| yukisega | 0:c490b05d753e | 208 | motor_P = (0.5 + pwmduty); |
| yukisega | 0:c490b05d753e | 209 | motor_N = (0.5 - pwmduty); |
| yukisega | 0:c490b05d753e | 210 | |
| yukisega | 0:c490b05d753e | 211 | |
| yukisega | 0:c490b05d753e | 212 | }//while(1) END |
| yukisega | 0:c490b05d753e | 213 | |
| yukisega | 0:c490b05d753e | 214 | }//main() END |
| yukisega | 0:c490b05d753e | 215 | |
| yukisega | 0:c490b05d753e | 216 |
