firmware for the miniv
Dependencies: mbed EthernetNetIf JSON
main.cpp@2:2eb3f4362288, 2021-05-06 (annotated)
- Committer:
- hakuturu583
- Date:
- Thu May 06 13:41:44 2021 +0000
- Revision:
- 2:2eb3f4362288
- Parent:
- 1:da09e6a94937
- Child:
- 3:a0cd142687d0
enable run pwm
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xshige | 0:c3fbb0514bfc | 1 | // TCP Echo server |
xshige | 1:da09e6a94937 | 2 | // 2010/9/7 |
xshige | 0:c3fbb0514bfc | 3 | |
xshige | 0:c3fbb0514bfc | 4 | |
xshige | 0:c3fbb0514bfc | 5 | /* |
xshige | 0:c3fbb0514bfc | 6 | |
xshige | 0:c3fbb0514bfc | 7 | (Execute Sample) |
xshige | 0:c3fbb0514bfc | 8 | PC side: |
xshige | 0:c3fbb0514bfc | 9 | telnet 192.168.0.25 12345 |
xshige | 0:c3fbb0514bfc | 10 | Trying 192.168.0.25... |
xshige | 0:c3fbb0514bfc | 11 | Connected to 192.168.0.25. |
xshige | 0:c3fbb0514bfc | 12 | Escape character is '^]'. |
xshige | 0:c3fbb0514bfc | 13 | |
xshige | 0:c3fbb0514bfc | 14 | mbed side: |
xshige | 0:c3fbb0514bfc | 15 | Setup OK |
xshige | 0:c3fbb0514bfc | 16 | mbed IP Address is 192.168.0.25 |
xshige | 0:c3fbb0514bfc | 17 | Binding.. |
xshige | 0:c3fbb0514bfc | 18 | Listening... |
xshige | 0:c3fbb0514bfc | 19 | Listening: TCP Socket Accepted |
xshige | 0:c3fbb0514bfc | 20 | Listening: Incoming TCP connection from 192.168.0.7 |
xshige | 0:c3fbb0514bfc | 21 | TCP Socket Readable |
xshige | 0:c3fbb0514bfc | 22 | Received&Wrote:test text |
xshige | 0:c3fbb0514bfc | 23 | |
xshige | 0:c3fbb0514bfc | 24 | TCP Socket Writable |
xshige | 0:c3fbb0514bfc | 25 | |
xshige | 0:c3fbb0514bfc | 26 | |
xshige | 0:c3fbb0514bfc | 27 | */ |
xshige | 0:c3fbb0514bfc | 28 | |
xshige | 0:c3fbb0514bfc | 29 | #include "mbed.h" |
xshige | 0:c3fbb0514bfc | 30 | #include "EthernetNetIf.h" |
xshige | 0:c3fbb0514bfc | 31 | #include "TCPSocket.h" |
hakuturu583 | 2:2eb3f4362288 | 32 | #include "Json.h" |
xshige | 0:c3fbb0514bfc | 33 | |
hakuturu583 | 2:2eb3f4362288 | 34 | PwmOut led1(LED1, "led1"); |
hakuturu583 | 2:2eb3f4362288 | 35 | PwmOut led4(LED4, "led4"); |
hakuturu583 | 2:2eb3f4362288 | 36 | PwmOut left_thrust_pin(p21); |
hakuturu583 | 2:2eb3f4362288 | 37 | PwmOut right_thrust_pin(p22); |
xshige | 0:c3fbb0514bfc | 38 | |
xshige | 0:c3fbb0514bfc | 39 | // EthernetNetIf eth; |
xshige | 0:c3fbb0514bfc | 40 | EthernetNetIf eth( |
xshige | 0:c3fbb0514bfc | 41 | IpAddr(192,168,0,25), //IP Address |
xshige | 0:c3fbb0514bfc | 42 | IpAddr(255,255,255,0), //Network Mask |
xshige | 0:c3fbb0514bfc | 43 | IpAddr(192,168,0,1), //Gateway |
xshige | 0:c3fbb0514bfc | 44 | IpAddr(192,168,0,1) //DNS |
xshige | 0:c3fbb0514bfc | 45 | ); |
xshige | 0:c3fbb0514bfc | 46 | |
xshige | 0:c3fbb0514bfc | 47 | #define TCP_LISTENING_PORT 12345 |
xshige | 0:c3fbb0514bfc | 48 | |
xshige | 0:c3fbb0514bfc | 49 | TCPSocket ListeningSock; |
xshige | 0:c3fbb0514bfc | 50 | TCPSocket* pConnectedSock; // for ConnectedSock |
xshige | 0:c3fbb0514bfc | 51 | Host client; |
xshige | 0:c3fbb0514bfc | 52 | TCPSocketErr err; |
hakuturu583 | 2:2eb3f4362288 | 53 | float left_motor_thrust = 0.0; |
hakuturu583 | 2:2eb3f4362288 | 54 | float right_motor_thrust = 0.0; |
hakuturu583 | 2:2eb3f4362288 | 55 | |
hakuturu583 | 2:2eb3f4362288 | 56 | float clamp(const float & value, const float & min = 0.0, const float & max = 1.0) |
hakuturu583 | 2:2eb3f4362288 | 57 | { |
hakuturu583 | 2:2eb3f4362288 | 58 | if(value <= min) { |
hakuturu583 | 2:2eb3f4362288 | 59 | return min; |
hakuturu583 | 2:2eb3f4362288 | 60 | } |
hakuturu583 | 2:2eb3f4362288 | 61 | if(value >= max) { |
hakuturu583 | 2:2eb3f4362288 | 62 | return max; |
hakuturu583 | 2:2eb3f4362288 | 63 | } |
hakuturu583 | 2:2eb3f4362288 | 64 | return value; |
hakuturu583 | 2:2eb3f4362288 | 65 | } |
xshige | 0:c3fbb0514bfc | 66 | |
xshige | 0:c3fbb0514bfc | 67 | void onConnectedTCPSocketEvent(TCPSocketEvent e) |
xshige | 0:c3fbb0514bfc | 68 | { |
xshige | 0:c3fbb0514bfc | 69 | switch(e) |
xshige | 0:c3fbb0514bfc | 70 | { |
xshige | 0:c3fbb0514bfc | 71 | case TCPSOCKET_CONNECTED: |
xshige | 0:c3fbb0514bfc | 72 | printf("TCP Socket Connected\r\n"); |
xshige | 0:c3fbb0514bfc | 73 | break; |
xshige | 0:c3fbb0514bfc | 74 | case TCPSOCKET_WRITEABLE: |
xshige | 0:c3fbb0514bfc | 75 | //Can now write some data... |
xshige | 0:c3fbb0514bfc | 76 | printf("TCP Socket Writable\r\n"); |
xshige | 0:c3fbb0514bfc | 77 | break; |
xshige | 0:c3fbb0514bfc | 78 | case TCPSOCKET_READABLE: |
xshige | 0:c3fbb0514bfc | 79 | //Can now read dome data... |
xshige | 0:c3fbb0514bfc | 80 | printf("TCP Socket Readable\r\n"); |
xshige | 0:c3fbb0514bfc | 81 | // Read in any available data into the buffer |
xshige | 0:c3fbb0514bfc | 82 | char buff[128]; |
hakuturu583 | 2:2eb3f4362288 | 83 | bool succees = false; |
xshige | 0:c3fbb0514bfc | 84 | while ( int len = pConnectedSock->recv(buff, 128) ) { |
hakuturu583 | 2:2eb3f4362288 | 85 | Json json(buff, len); |
hakuturu583 | 2:2eb3f4362288 | 86 | if (!json.isValidJson()) { |
hakuturu583 | 2:2eb3f4362288 | 87 | succees = false; |
hakuturu583 | 2:2eb3f4362288 | 88 | } |
hakuturu583 | 2:2eb3f4362288 | 89 | else { |
hakuturu583 | 2:2eb3f4362288 | 90 | int left_index = json.findKeyIndexIn ("left", 0 ); |
hakuturu583 | 2:2eb3f4362288 | 91 | int right_index = json.findKeyIndexIn ("right", 0 ); |
hakuturu583 | 2:2eb3f4362288 | 92 | int left_value_index = json.findChildIndexOf(left_index, -1); |
hakuturu583 | 2:2eb3f4362288 | 93 | int right_value_index = json.findChildIndexOf(right_index, -1); |
hakuturu583 | 2:2eb3f4362288 | 94 | if(left_index == -1 || right_index == -1) { |
hakuturu583 | 2:2eb3f4362288 | 95 | succees = false; |
hakuturu583 | 2:2eb3f4362288 | 96 | } |
hakuturu583 | 2:2eb3f4362288 | 97 | else { |
hakuturu583 | 2:2eb3f4362288 | 98 | if(left_value_index > 0 && right_value_index > 0) { |
hakuturu583 | 2:2eb3f4362288 | 99 | if(json.tokenNumberValue(left_value_index, left_motor_thrust) == 0 && |
hakuturu583 | 2:2eb3f4362288 | 100 | json.tokenNumberValue(right_value_index, right_motor_thrust) == 0) { |
hakuturu583 | 2:2eb3f4362288 | 101 | succees = true; |
hakuturu583 | 2:2eb3f4362288 | 102 | } |
hakuturu583 | 2:2eb3f4362288 | 103 | } |
hakuturu583 | 2:2eb3f4362288 | 104 | } |
hakuturu583 | 2:2eb3f4362288 | 105 | } |
hakuturu583 | 2:2eb3f4362288 | 106 | /* |
hakuturu583 | 2:2eb3f4362288 | 107 | left_motor_thrust = (unsigned char)buff[0]; |
hakuturu583 | 2:2eb3f4362288 | 108 | right_motor_thrust = (unsigned char)buff[1]; |
hakuturu583 | 2:2eb3f4362288 | 109 | */ |
hakuturu583 | 2:2eb3f4362288 | 110 | // And send straight back out again |
hakuturu583 | 2:2eb3f4362288 | 111 | // pConnectedSock->send(buff, len); |
xshige | 1:da09e6a94937 | 112 | buff[len]=0; // make terminater |
xshige | 0:c3fbb0514bfc | 113 | printf("Received&Wrote:%s\r\n",buff); |
xshige | 0:c3fbb0514bfc | 114 | } |
hakuturu583 | 2:2eb3f4362288 | 115 | if(!succees) { |
hakuturu583 | 2:2eb3f4362288 | 116 | left_motor_thrust = 0.0; |
hakuturu583 | 2:2eb3f4362288 | 117 | right_motor_thrust = 0.0; |
hakuturu583 | 2:2eb3f4362288 | 118 | } |
xshige | 0:c3fbb0514bfc | 119 | break; |
xshige | 0:c3fbb0514bfc | 120 | case TCPSOCKET_CONTIMEOUT: |
xshige | 0:c3fbb0514bfc | 121 | printf("TCP Socket Timeout\r\n"); |
xshige | 0:c3fbb0514bfc | 122 | break; |
xshige | 0:c3fbb0514bfc | 123 | case TCPSOCKET_CONRST: |
xshige | 0:c3fbb0514bfc | 124 | printf("TCP Socket CONRST\r\n"); |
xshige | 0:c3fbb0514bfc | 125 | break; |
xshige | 0:c3fbb0514bfc | 126 | case TCPSOCKET_CONABRT: |
xshige | 0:c3fbb0514bfc | 127 | printf("TCP Socket CONABRT\r\n"); |
xshige | 0:c3fbb0514bfc | 128 | break; |
xshige | 0:c3fbb0514bfc | 129 | case TCPSOCKET_ERROR: |
xshige | 0:c3fbb0514bfc | 130 | printf("TCP Socket Error\r\n"); |
xshige | 0:c3fbb0514bfc | 131 | break; |
xshige | 0:c3fbb0514bfc | 132 | case TCPSOCKET_DISCONNECTED: |
xshige | 0:c3fbb0514bfc | 133 | //Close socket... |
xshige | 0:c3fbb0514bfc | 134 | printf("TCP Socket Disconnected\r\n"); |
xshige | 0:c3fbb0514bfc | 135 | pConnectedSock->close(); |
xshige | 0:c3fbb0514bfc | 136 | break; |
xshige | 0:c3fbb0514bfc | 137 | default: |
xshige | 0:c3fbb0514bfc | 138 | printf("DEFAULT\r\n"); |
xshige | 0:c3fbb0514bfc | 139 | } |
xshige | 0:c3fbb0514bfc | 140 | } |
xshige | 0:c3fbb0514bfc | 141 | |
xshige | 0:c3fbb0514bfc | 142 | |
xshige | 0:c3fbb0514bfc | 143 | void onListeningTCPSocketEvent(TCPSocketEvent e) |
xshige | 0:c3fbb0514bfc | 144 | { |
xshige | 0:c3fbb0514bfc | 145 | switch(e) |
xshige | 0:c3fbb0514bfc | 146 | { |
xshige | 0:c3fbb0514bfc | 147 | case TCPSOCKET_ACCEPT: |
xshige | 0:c3fbb0514bfc | 148 | printf("Listening: TCP Socket Accepted\r\n"); |
xshige | 0:c3fbb0514bfc | 149 | // Accepts connection from client and gets connected socket. |
xshige | 0:c3fbb0514bfc | 150 | err=ListeningSock.accept(&client, &pConnectedSock); |
xshige | 0:c3fbb0514bfc | 151 | if (err) { |
xshige | 0:c3fbb0514bfc | 152 | printf("onListeningTcpSocketEvent : Could not accept connection.\r\n"); |
xshige | 0:c3fbb0514bfc | 153 | return; //Error in accept, discard connection |
xshige | 0:c3fbb0514bfc | 154 | } |
xshige | 0:c3fbb0514bfc | 155 | // Setup the new socket events |
xshige | 0:c3fbb0514bfc | 156 | pConnectedSock->setOnEvent(&onConnectedTCPSocketEvent); |
xshige | 0:c3fbb0514bfc | 157 | // We can find out from where the connection is coming by looking at the |
xshige | 0:c3fbb0514bfc | 158 | // Host parameter of the accept() method |
xshige | 0:c3fbb0514bfc | 159 | IpAddr clientIp = client.getIp(); |
xshige | 0:c3fbb0514bfc | 160 | printf("Listening: Incoming TCP connection from %d.%d.%d.%d\r\n", |
xshige | 0:c3fbb0514bfc | 161 | clientIp[0], clientIp[1], clientIp[2], clientIp[3]); |
xshige | 0:c3fbb0514bfc | 162 | break; |
xshige | 0:c3fbb0514bfc | 163 | // the following cases will not happen |
xshige | 0:c3fbb0514bfc | 164 | case TCPSOCKET_CONNECTED: |
xshige | 0:c3fbb0514bfc | 165 | printf("Listening: TCP Socket Connected\r\n"); |
xshige | 0:c3fbb0514bfc | 166 | break; |
xshige | 0:c3fbb0514bfc | 167 | case TCPSOCKET_WRITEABLE: |
xshige | 0:c3fbb0514bfc | 168 | printf("Listening: TCP Socket Writable\r\n"); |
xshige | 0:c3fbb0514bfc | 169 | break; |
xshige | 0:c3fbb0514bfc | 170 | case TCPSOCKET_READABLE: |
xshige | 0:c3fbb0514bfc | 171 | printf("Listening: TCP Socket Readable\r\n"); |
xshige | 0:c3fbb0514bfc | 172 | break; |
xshige | 0:c3fbb0514bfc | 173 | case TCPSOCKET_CONTIMEOUT: |
xshige | 0:c3fbb0514bfc | 174 | printf("Listening: TCP Socket Timeout\r\n"); |
xshige | 0:c3fbb0514bfc | 175 | break; |
xshige | 0:c3fbb0514bfc | 176 | case TCPSOCKET_CONRST: |
xshige | 0:c3fbb0514bfc | 177 | printf("Listening: TCP Socket CONRST\r\n"); |
xshige | 0:c3fbb0514bfc | 178 | break; |
xshige | 0:c3fbb0514bfc | 179 | case TCPSOCKET_CONABRT: |
xshige | 0:c3fbb0514bfc | 180 | printf("Listening: TCP Socket CONABRT\r\n"); |
xshige | 0:c3fbb0514bfc | 181 | break; |
xshige | 0:c3fbb0514bfc | 182 | case TCPSOCKET_ERROR: |
xshige | 0:c3fbb0514bfc | 183 | printf("Listening: TCP Socket Error\r\n"); |
xshige | 0:c3fbb0514bfc | 184 | break; |
xshige | 0:c3fbb0514bfc | 185 | case TCPSOCKET_DISCONNECTED: |
xshige | 0:c3fbb0514bfc | 186 | //Close socket... |
xshige | 0:c3fbb0514bfc | 187 | printf("Listening: TCP Socket Disconnected\r\n"); |
xshige | 0:c3fbb0514bfc | 188 | ListeningSock.close(); |
xshige | 0:c3fbb0514bfc | 189 | break; |
xshige | 0:c3fbb0514bfc | 190 | default: |
xshige | 0:c3fbb0514bfc | 191 | printf("DEFAULT\r\n"); |
xshige | 0:c3fbb0514bfc | 192 | }; |
xshige | 0:c3fbb0514bfc | 193 | } |
xshige | 0:c3fbb0514bfc | 194 | |
xshige | 0:c3fbb0514bfc | 195 | int main() { |
hakuturu583 | 2:2eb3f4362288 | 196 | led1.period(0.002); |
hakuturu583 | 2:2eb3f4362288 | 197 | led4.period(0.002); |
hakuturu583 | 2:2eb3f4362288 | 198 | left_thrust_pin.period(0.002); |
hakuturu583 | 2:2eb3f4362288 | 199 | right_thrust_pin.period(0.002); |
xshige | 0:c3fbb0514bfc | 200 | printf("\r\n"); |
xshige | 0:c3fbb0514bfc | 201 | printf("Setting up...\r\n"); |
xshige | 0:c3fbb0514bfc | 202 | EthernetErr ethErr = eth.setup(); |
xshige | 0:c3fbb0514bfc | 203 | if(ethErr) |
xshige | 0:c3fbb0514bfc | 204 | { |
xshige | 0:c3fbb0514bfc | 205 | printf("Error %d in setup.\r\n", ethErr); |
xshige | 0:c3fbb0514bfc | 206 | return -1; |
xshige | 0:c3fbb0514bfc | 207 | } |
xshige | 0:c3fbb0514bfc | 208 | printf("Setup OK\r\n"); |
xshige | 0:c3fbb0514bfc | 209 | |
xshige | 0:c3fbb0514bfc | 210 | IpAddr ip = eth.getIp(); |
xshige | 0:c3fbb0514bfc | 211 | printf("mbed IP Address is %d.%d.%d.%d\r\n", ip[0], ip[1], ip[2], ip[3]); |
xshige | 0:c3fbb0514bfc | 212 | |
xshige | 0:c3fbb0514bfc | 213 | |
xshige | 0:c3fbb0514bfc | 214 | // Set the callbacks for Listening |
xshige | 0:c3fbb0514bfc | 215 | ListeningSock.setOnEvent(&onListeningTCPSocketEvent); |
xshige | 0:c3fbb0514bfc | 216 | |
xshige | 0:c3fbb0514bfc | 217 | // bind and listen on TCP |
xshige | 0:c3fbb0514bfc | 218 | err=ListeningSock.bind(Host(IpAddr(), TCP_LISTENING_PORT)); |
xshige | 0:c3fbb0514bfc | 219 | printf("Binding..\r\n"); |
xshige | 0:c3fbb0514bfc | 220 | if(err) |
xshige | 0:c3fbb0514bfc | 221 | { |
xshige | 0:c3fbb0514bfc | 222 | //Deal with that error... |
xshige | 0:c3fbb0514bfc | 223 | printf("Binding Error\n"); |
xshige | 0:c3fbb0514bfc | 224 | } |
xshige | 0:c3fbb0514bfc | 225 | err=ListeningSock.listen(); // Starts listening |
xshige | 0:c3fbb0514bfc | 226 | printf("Listening...\r\n"); |
xshige | 0:c3fbb0514bfc | 227 | if(err) |
xshige | 0:c3fbb0514bfc | 228 | { |
xshige | 0:c3fbb0514bfc | 229 | printf("Listening Error\r\n"); |
xshige | 0:c3fbb0514bfc | 230 | } |
xshige | 0:c3fbb0514bfc | 231 | |
xshige | 0:c3fbb0514bfc | 232 | Timer tmr; |
xshige | 0:c3fbb0514bfc | 233 | tmr.start(); |
hakuturu583 | 2:2eb3f4362288 | 234 | float timer_count = 0.0; |
xshige | 0:c3fbb0514bfc | 235 | |
xshige | 0:c3fbb0514bfc | 236 | while(true) |
xshige | 0:c3fbb0514bfc | 237 | { |
xshige | 0:c3fbb0514bfc | 238 | Net::poll(); |
hakuturu583 | 2:2eb3f4362288 | 239 | if(tmr.read() > 0.001) // sec |
xshige | 0:c3fbb0514bfc | 240 | { |
hakuturu583 | 2:2eb3f4362288 | 241 | timer_count = timer_count + 0.001; |
hakuturu583 | 2:2eb3f4362288 | 242 | if(timer_count >= 1.0) { |
hakuturu583 | 2:2eb3f4362288 | 243 | timer_count = 0.0; |
hakuturu583 | 2:2eb3f4362288 | 244 | led1 = 0.5 + clamp(left_motor_thrust, -1, 1) * 0.5; |
hakuturu583 | 2:2eb3f4362288 | 245 | led4 = 0.5 + clamp(right_motor_thrust, -1, 1) * 0.5; |
hakuturu583 | 2:2eb3f4362288 | 246 | left_thrust_pin = 0.75 + clamp(left_motor_thrust, -1, 1) * 0.2; |
hakuturu583 | 2:2eb3f4362288 | 247 | right_thrust_pin = 0.75 + clamp(right_motor_thrust, -1, 1) * 0.2; |
hakuturu583 | 2:2eb3f4362288 | 248 | } |
xshige | 0:c3fbb0514bfc | 249 | } |
xshige | 0:c3fbb0514bfc | 250 | } |
xshige | 0:c3fbb0514bfc | 251 | } |