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
main.cpp@0:ecd79ba7c7b2, 2017-11-13 (annotated)
- Committer:
- matohatto
- Date:
- Mon Nov 13 06:58:55 2017 +0000
- Revision:
- 0:ecd79ba7c7b2
Radio Control Propotional receiver to ethernet converter firmware,
; for test purpose of RaccoonII motor driver.
;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| matohatto | 0:ecd79ba7c7b2 | 1 | /****** R/C to Ethernet Signal Converter feat.I2C *******/ |
| matohatto | 0:ecd79ba7c7b2 | 2 | /****** Ver. 1.0 *******/ |
| matohatto | 0:ecd79ba7c7b2 | 3 | /****** Written by Wada Suisei *******/ |
| matohatto | 0:ecd79ba7c7b2 | 4 | /****** 2017 Oct 1 *******/ |
| matohatto | 0:ecd79ba7c7b2 | 5 | // |
| matohatto | 0:ecd79ba7c7b2 | 6 | // Modified on Oct.20th, 2017 by Mato Hattori |
| matohatto | 0:ecd79ba7c7b2 | 7 | // |
| matohatto | 0:ecd79ba7c7b2 | 8 | |
| matohatto | 0:ecd79ba7c7b2 | 9 | /*** Including Header Files***/ |
| matohatto | 0:ecd79ba7c7b2 | 10 | #include "mbed.h" |
| matohatto | 0:ecd79ba7c7b2 | 11 | #include "EthernetInterface.h" |
| matohatto | 0:ecd79ba7c7b2 | 12 | |
| matohatto | 0:ecd79ba7c7b2 | 13 | /*** Ethernet Configuration data***/ |
| matohatto | 0:ecd79ba7c7b2 | 14 | const char IpAddr[] = "192.168.11.241"; |
| matohatto | 0:ecd79ba7c7b2 | 15 | const char NetMask[] = "255.255.255.0"; |
| matohatto | 0:ecd79ba7c7b2 | 16 | const char GatewayAddr[] = "192.168.11.1"; |
| matohatto | 0:ecd79ba7c7b2 | 17 | const int Port_Num = 4001; |
| matohatto | 0:ecd79ba7c7b2 | 18 | |
| matohatto | 0:ecd79ba7c7b2 | 19 | const char MotorDriverIpAddr[] = "192.168.11.242"; // motor driver ip address |
| matohatto | 0:ecd79ba7c7b2 | 20 | const int MotorDriverPortNum = 4000; // motor driver ip port |
| matohatto | 0:ecd79ba7c7b2 | 21 | |
| matohatto | 0:ecd79ba7c7b2 | 22 | /*** I2C Configure***/ |
| matohatto | 0:ecd79ba7c7b2 | 23 | #define SDA_PIN p28 |
| matohatto | 0:ecd79ba7c7b2 | 24 | #define SCL_PIN p27 |
| matohatto | 0:ecd79ba7c7b2 | 25 | #define SLAVE_NUMBER 3 |
| matohatto | 0:ecd79ba7c7b2 | 26 | #define MSEC /1000.0 |
| matohatto | 0:ecd79ba7c7b2 | 27 | #define LOOP_INTERVAL_MILISEC 500 // loop interval in mili-seconds |
| matohatto | 0:ecd79ba7c7b2 | 28 | const int SlaveAddr[SLAVE_NUMBER] = {0x0b, 0x0c, 0x0d}; |
| matohatto | 0:ecd79ba7c7b2 | 29 | const int min_cnt[SLAVE_NUMBER] ={1119,1650,1650};/*1ch:SW, 2ch:Rudder, 5ch:Thrustor*/ |
| matohatto | 0:ecd79ba7c7b2 | 30 | const int max_cnt[SLAVE_NUMBER] ={1960, 2915,2915};/*1ch:SW, 2ch:Rudder, 5ch:Thrustor*/ |
| matohatto | 0:ecd79ba7c7b2 | 31 | |
| matohatto | 0:ecd79ba7c7b2 | 32 | /***Pin Mode Initializing***/ |
| matohatto | 0:ecd79ba7c7b2 | 33 | int pin_init(void){ |
| matohatto | 0:ecd79ba7c7b2 | 34 | return 1; |
| matohatto | 0:ecd79ba7c7b2 | 35 | } |
| matohatto | 0:ecd79ba7c7b2 | 36 | |
| matohatto | 0:ecd79ba7c7b2 | 37 | |
| matohatto | 0:ecd79ba7c7b2 | 38 | int main(){ |
| matohatto | 0:ecd79ba7c7b2 | 39 | |
| matohatto | 0:ecd79ba7c7b2 | 40 | /*Pin Init*/ |
| matohatto | 0:ecd79ba7c7b2 | 41 | pin_init(); |
| matohatto | 0:ecd79ba7c7b2 | 42 | printf("Pin Initialize Complete!\n"); |
| matohatto | 0:ecd79ba7c7b2 | 43 | |
| matohatto | 0:ecd79ba7c7b2 | 44 | /*I2C Init*/ |
| matohatto | 0:ecd79ba7c7b2 | 45 | I2C i2c_master(SDA_PIN, SCL_PIN); |
| matohatto | 0:ecd79ba7c7b2 | 46 | printf("I2C Initialize Complete!\n"); |
| matohatto | 0:ecd79ba7c7b2 | 47 | |
| matohatto | 0:ecd79ba7c7b2 | 48 | /*Ethernet Init*/ |
| matohatto | 0:ecd79ba7c7b2 | 49 | EthernetInterface eth; |
| matohatto | 0:ecd79ba7c7b2 | 50 | if(eth.init(IpAddr, NetMask, GatewayAddr)){ |
| matohatto | 0:ecd79ba7c7b2 | 51 | return -1; |
| matohatto | 0:ecd79ba7c7b2 | 52 | } |
| matohatto | 0:ecd79ba7c7b2 | 53 | eth.connect(); |
| matohatto | 0:ecd79ba7c7b2 | 54 | printf("Ethernet Connected\n"); |
| matohatto | 0:ecd79ba7c7b2 | 55 | printf("------------------------------\n"); |
| matohatto | 0:ecd79ba7c7b2 | 56 | printf("IP Address is %s\n",eth.getIPAddress()); |
| matohatto | 0:ecd79ba7c7b2 | 57 | printf("Net Mask is %s\n",eth.getNetworkMask()); |
| matohatto | 0:ecd79ba7c7b2 | 58 | printf("------------------------------\n"); |
| matohatto | 0:ecd79ba7c7b2 | 59 | |
| matohatto | 0:ecd79ba7c7b2 | 60 | /*UDP Init*/ |
| matohatto | 0:ecd79ba7c7b2 | 61 | UDPSocket sock; |
| matohatto | 0:ecd79ba7c7b2 | 62 | sock.bind(Port_Num); |
| matohatto | 0:ecd79ba7c7b2 | 63 | |
| matohatto | 0:ecd79ba7c7b2 | 64 | /*Endpoint Init*/ |
| matohatto | 0:ecd79ba7c7b2 | 65 | Endpoint md; |
| matohatto | 0:ecd79ba7c7b2 | 66 | md.set_address (MotorDriverIpAddr, MotorDriverPortNum); |
| matohatto | 0:ecd79ba7c7b2 | 67 | |
| matohatto | 0:ecd79ba7c7b2 | 68 | |
| matohatto | 0:ecd79ba7c7b2 | 69 | /**Main Routine**/ |
| matohatto | 0:ecd79ba7c7b2 | 70 | int udpout_buffer[3]; |
| matohatto | 0:ecd79ba7c7b2 | 71 | int udpin_buffer[3]; |
| matohatto | 0:ecd79ba7c7b2 | 72 | int data_int[SLAVE_NUMBER]; |
| matohatto | 0:ecd79ba7c7b2 | 73 | int data_ratio[SLAVE_NUMBER]; |
| matohatto | 0:ecd79ba7c7b2 | 74 | int seq; |
| matohatto | 0:ecd79ba7c7b2 | 75 | long tick = 0; |
| matohatto | 0:ecd79ba7c7b2 | 76 | DigitalOut myled (LED1); |
| matohatto | 0:ecd79ba7c7b2 | 77 | |
| matohatto | 0:ecd79ba7c7b2 | 78 | myled = 0; // turn off LED1 |
| matohatto | 0:ecd79ba7c7b2 | 79 | |
| matohatto | 0:ecd79ba7c7b2 | 80 | while(1){ |
| matohatto | 0:ecd79ba7c7b2 | 81 | |
| matohatto | 0:ecd79ba7c7b2 | 82 | /*i2c Initialize*/ |
| matohatto | 0:ecd79ba7c7b2 | 83 | printf("I2C Data fetch..... "); |
| matohatto | 0:ecd79ba7c7b2 | 84 | |
| matohatto | 0:ecd79ba7c7b2 | 85 | /*i2c communication sequence*/ |
| matohatto | 0:ecd79ba7c7b2 | 86 | for(seq=0; seq<SLAVE_NUMBER; seq++) |
| matohatto | 0:ecd79ba7c7b2 | 87 | { |
| matohatto | 0:ecd79ba7c7b2 | 88 | data_int[seq] = 0; |
| matohatto | 0:ecd79ba7c7b2 | 89 | /*data_frac[seq] = 0;*/ |
| matohatto | 0:ecd79ba7c7b2 | 90 | #if 0 |
| matohatto | 0:ecd79ba7c7b2 | 91 | i2c_master.start(); /*Send start condition*/ |
| matohatto | 0:ecd79ba7c7b2 | 92 | while(i2c_master.read( ((SlaveAddr[seq]<<1)|0x01), receive_data, sizeof(receive_data) )); /*Wait ACK --Master Receive*/ |
| matohatto | 0:ecd79ba7c7b2 | 93 | i2c_master.stop(); |
| matohatto | 0:ecd79ba7c7b2 | 94 | |
| matohatto | 0:ecd79ba7c7b2 | 95 | /*Value Modification Sequence*/ |
| matohatto | 0:ecd79ba7c7b2 | 96 | data_int[seq] = ( (receive_data[0]<<8)| receive_data[1] ); |
| matohatto | 0:ecd79ba7c7b2 | 97 | data_ratio[seq] = (data_int[seq] - min_cnt[seq])*200/(max_cnt[seq] - min_cnt[seq])-100; |
| matohatto | 0:ecd79ba7c7b2 | 98 | #else |
| matohatto | 0:ecd79ba7c7b2 | 99 | AnalogIn vr (p18); // DEBUG for testing |
| matohatto | 0:ecd79ba7c7b2 | 100 | if (! seq) |
| matohatto | 0:ecd79ba7c7b2 | 101 | printf ("ain=%f ", (float) vr); |
| matohatto | 0:ecd79ba7c7b2 | 102 | data_ratio[seq] = (int)(( (float) vr - 0.527) * 277.0); |
| matohatto | 0:ecd79ba7c7b2 | 103 | #endif |
| matohatto | 0:ecd79ba7c7b2 | 104 | |
| matohatto | 0:ecd79ba7c7b2 | 105 | if(-5<=data_ratio[seq] && data_ratio[seq]<=5){ |
| matohatto | 0:ecd79ba7c7b2 | 106 | data_ratio[seq] = 0; |
| matohatto | 0:ecd79ba7c7b2 | 107 | }else if(data_ratio[seq]<=-96){ |
| matohatto | 0:ecd79ba7c7b2 | 108 | data_ratio[seq] = -100; |
| matohatto | 0:ecd79ba7c7b2 | 109 | }else if(data_ratio[seq]>=96){ |
| matohatto | 0:ecd79ba7c7b2 | 110 | data_ratio[seq] = 100; |
| matohatto | 0:ecd79ba7c7b2 | 111 | } |
| matohatto | 0:ecd79ba7c7b2 | 112 | |
| matohatto | 0:ecd79ba7c7b2 | 113 | printf("%d%% (0x%02x) ", data_ratio[seq], SlaveAddr[seq]); |
| matohatto | 0:ecd79ba7c7b2 | 114 | } |
| matohatto | 0:ecd79ba7c7b2 | 115 | printf("\n"); |
| matohatto | 0:ecd79ba7c7b2 | 116 | |
| matohatto | 0:ecd79ba7c7b2 | 117 | // UDP Communication Sequence |
| matohatto | 0:ecd79ba7c7b2 | 118 | //for(seq=0; seq<SLAVE_NUMBER; seq++) |
| matohatto | 0:ecd79ba7c7b2 | 119 | // udpout_buffer[seq] = data_ratio[seq]; |
| matohatto | 0:ecd79ba7c7b2 | 120 | udpout_buffer[0] = data_ratio[0]; |
| matohatto | 0:ecd79ba7c7b2 | 121 | udpout_buffer[1] = 0; |
| matohatto | 0:ecd79ba7c7b2 | 122 | |
| matohatto | 0:ecd79ba7c7b2 | 123 | printf("tx -> #%ld : %d %d\n", tick, udpout_buffer[0], udpout_buffer[1]); |
| matohatto | 0:ecd79ba7c7b2 | 124 | sock.sendTo(md, (char *)udpout_buffer, 4); |
| matohatto | 0:ecd79ba7c7b2 | 125 | |
| matohatto | 0:ecd79ba7c7b2 | 126 | // memset (udpin_buffer, sizeof (udpin_buffer), 0); |
| matohatto | 0:ecd79ba7c7b2 | 127 | // sock.receiveFrom (md, (char *)udpin_buffer, sizeof (udpin_buffer)); |
| matohatto | 0:ecd79ba7c7b2 | 128 | // printf("rx <- #%ld : %d %d %d\n", tick, udpin_buffer[0], udpin_buffer[1], udpin_buffer[2]); |
| matohatto | 0:ecd79ba7c7b2 | 129 | |
| matohatto | 0:ecd79ba7c7b2 | 130 | // end of main loop |
| matohatto | 0:ecd79ba7c7b2 | 131 | myled = ! myled; // toggle LED1 |
| matohatto | 0:ecd79ba7c7b2 | 132 | wait (LOOP_INTERVAL_MILISEC MSEC); |
| matohatto | 0:ecd79ba7c7b2 | 133 | } |
| matohatto | 0:ecd79ba7c7b2 | 134 | |
| matohatto | 0:ecd79ba7c7b2 | 135 | sock.close(); |
| matohatto | 0:ecd79ba7c7b2 | 136 | eth.disconnect(); |
| matohatto | 0:ecd79ba7c7b2 | 137 | myled = 0; // turn off LED1 |
| matohatto | 0:ecd79ba7c7b2 | 138 | } |
| matohatto | 0:ecd79ba7c7b2 | 139 | |
| matohatto | 0:ecd79ba7c7b2 | 140 | |
| matohatto | 0:ecd79ba7c7b2 | 141 | |
| matohatto | 0:ecd79ba7c7b2 | 142 | |
| matohatto | 0:ecd79ba7c7b2 | 143 | |
| matohatto | 0:ecd79ba7c7b2 | 144 | |
| matohatto | 0:ecd79ba7c7b2 | 145 | |
| matohatto | 0:ecd79ba7c7b2 | 146 |