Commit

Dependencies:   EthernetInterface NetworkAPI SDC21XX_Motor mbed-rtos mbed

Fork of TCP_Server_Example by Roy van Dam

Committer:
kkoichy
Date:
Thu Jul 21 13:13:21 2016 +0000
Revision:
13:b9bcffe7e649
Parent:
12:5e342c364d61
Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:bb128f0e952f 1 #include "mbed.h"
kkoichy 12:5e342c364d61 2 #include "SDC21XX_Motor.h"
kkoichy 12:5e342c364d61 3 #include "rtos.h"
donatien 0:bb128f0e952f 4 #include "EthernetInterface.h"
NegativeBlack 9:a4c85bea2d77 5 #include "NetworkAPI/buffer.hpp"
NegativeBlack 11:90554d22ade5 6 #include "NetworkAPI/select.hpp"
NegativeBlack 11:90554d22ade5 7 #include "NetworkAPI/ip/address.hpp"
NegativeBlack 9:a4c85bea2d77 8 #include "NetworkAPI/tcp/socket.hpp"
NegativeBlack 9:a4c85bea2d77 9 using namespace network;
NegativeBlack 11:90554d22ade5 10
kkoichy 12:5e342c364d61 11 #define MAX_CLIENTS 1
kkoichy 12:5e342c364d61 12 #define SERVER_PORT 11556
kkoichy 12:5e342c364d61 13 #define PERIODE_ENVOI 2.0
kkoichy 12:5e342c364d61 14
kkoichy 12:5e342c364d61 15 #define REDUC_M1 1
kkoichy 12:5e342c364d61 16 #define REDUC_M2 1
kkoichy 12:5e342c364d61 17 #define REDUC_M3 1
kkoichy 12:5e342c364d61 18 #define REDUC_M4 1
kkoichy 12:5e342c364d61 19 #define REDUC_M5 1
kkoichy 12:5e342c364d61 20 #define REDUC_M6 1
kkoichy 12:5e342c364d61 21
kkoichy 12:5e342c364d61 22 Timer t, t2;
kkoichy 12:5e342c364d61 23 Mutex mutex1;
kkoichy 12:5e342c364d61 24
kkoichy 12:5e342c364d61 25 CAN CanBus(P0_4,P0_5);
kkoichy 12:5e342c364d61 26
kkoichy 12:5e342c364d61 27 SDC21XX_Motor Motor1(1, &CanBus, 1, REDUC_M1, 1000);
kkoichy 12:5e342c364d61 28 SDC21XX_Motor Motor2(1, &CanBus, 2, REDUC_M2, 1000);
kkoichy 12:5e342c364d61 29 SDC21XX_Motor Motor3(2, &CanBus, 1, REDUC_M3, 1000);
kkoichy 12:5e342c364d61 30 SDC21XX_Motor Motor4(2, &CanBus, 2, REDUC_M4, 1000);
kkoichy 12:5e342c364d61 31 SDC21XX_Motor Motor5(3, &CanBus, 1, REDUC_M5, 1000);
kkoichy 12:5e342c364d61 32 SDC21XX_Motor Motor6(3, &CanBus, 2, REDUC_M6, 1000);
kkoichy 12:5e342c364d61 33
kkoichy 12:5e342c364d61 34 SDC21XX_Motor Moteur[6] = {Motor1, Motor2, Motor3, Motor4, Motor5, Motor6};
kkoichy 12:5e342c364d61 35
kkoichy 12:5e342c364d61 36 uint8_t client_connected = 0, move_increment = 0, move_increment_val = 0;
kkoichy 12:5e342c364d61 37
kkoichy 12:5e342c364d61 38 tcp::Socket server;
kkoichy 12:5e342c364d61 39 tcp::Socket client[MAX_CLIENTS];
kkoichy 12:5e342c364d61 40 tcp::Socket *socket = NULL;
kkoichy 12:5e342c364d61 41
kkoichy 12:5e342c364d61 42
kkoichy 12:5e342c364d61 43 void periodic_encoder_isr(void const *args) {
kkoichy 12:5e342c364d61 44 char reply[45], i = 0;
kkoichy 12:5e342c364d61 45 long dummy;
kkoichy 12:5e342c364d61 46
kkoichy 12:5e342c364d61 47 while(1)
kkoichy 12:5e342c364d61 48 {
kkoichy 12:5e342c364d61 49 mutex1.lock();
kkoichy 12:5e342c364d61 50 printf("Ticker\n\r");
kkoichy 12:5e342c364d61 51
kkoichy 12:5e342c364d61 52 reply[0] = 0xa5;
kkoichy 12:5e342c364d61 53 for(i = 1;i<6;i++)
kkoichy 12:5e342c364d61 54 reply[7*i] = 0xa6;
kkoichy 12:5e342c364d61 55 for(i=0;i<6;i++)
kkoichy 12:5e342c364d61 56 {
kkoichy 12:5e342c364d61 57 reply[1+i*7] = 0x11;
kkoichy 12:5e342c364d61 58 reply[2+i*7] = i+1;
kkoichy 12:5e342c364d61 59 dummy = Moteur[i].GetRB()->ReadAbsoluteEncoderCount(Moteur[i].GetChannel());
kkoichy 12:5e342c364d61 60 memcpy(&reply[3+i*7], &dummy, 4);
kkoichy 12:5e342c364d61 61 }
kkoichy 12:5e342c364d61 62 reply[42] = 0xa7;
kkoichy 12:5e342c364d61 63
kkoichy 12:5e342c364d61 64 client[0].write((void *)reply, 43);
kkoichy 12:5e342c364d61 65 Thread::wait(200);
kkoichy 12:5e342c364d61 66 mutex1.unlock();
kkoichy 12:5e342c364d61 67 }
kkoichy 12:5e342c364d61 68 }
kkoichy 12:5e342c364d61 69
NegativeBlack 11:90554d22ade5 70
kkoichy 12:5e342c364d61 71 int Process_Data_TCP_IP(uint8_t * data, tcp::Socket * client);
kkoichy 12:5e342c364d61 72
kkoichy 12:5e342c364d61 73
kkoichy 12:5e342c364d61 74
NegativeBlack 6:33b57f606f2b 75 int
NegativeBlack 6:33b57f606f2b 76 main()
NegativeBlack 6:33b57f606f2b 77 {
kkoichy 12:5e342c364d61 78 mutex1.lock();
kkoichy 12:5e342c364d61 79 Thread thread1(periodic_encoder_isr);
kkoichy 12:5e342c364d61 80
NegativeBlack 6:33b57f606f2b 81 EthernetInterface interface;
NegativeBlack 6:33b57f606f2b 82 interface.init();
NegativeBlack 6:33b57f606f2b 83 interface.connect();
NegativeBlack 6:33b57f606f2b 84 printf("IP Address is %s\n\r", interface.getIPAddress());
NegativeBlack 11:90554d22ade5 85
NegativeBlack 11:90554d22ade5 86 Select select;
NegativeBlack 11:90554d22ade5 87
NegativeBlack 11:90554d22ade5 88 int result = 0;
NegativeBlack 11:90554d22ade5 89 int index = 0;
NegativeBlack 11:90554d22ade5 90
NegativeBlack 11:90554d22ade5 91 network::Buffer buffer(256);
NegativeBlack 11:90554d22ade5 92 std::string message("Hello world!");
NegativeBlack 11:90554d22ade5 93
NegativeBlack 11:90554d22ade5 94 // Configure the server socket (assume everty thing works)
NegativeBlack 11:90554d22ade5 95 server.open();
kkoichy 12:5e342c364d61 96 server.bind(SERVER_PORT);
NegativeBlack 11:90554d22ade5 97 server.listen(MAX_CLIENTS);
NegativeBlack 11:90554d22ade5 98
NegativeBlack 11:90554d22ade5 99 // Add sockets to the select api
NegativeBlack 11:90554d22ade5 100 select.set(&server, Select::Read);
NegativeBlack 11:90554d22ade5 101 for (index = 0; index < MAX_CLIENTS; index++) {
NegativeBlack 11:90554d22ade5 102 select.set(&client[index], Select::Read);
NegativeBlack 10:9e8d5928537a 103 }
NegativeBlack 11:90554d22ade5 104
NegativeBlack 11:90554d22ade5 105 do {
NegativeBlack 11:90554d22ade5 106 // Wait for activity
NegativeBlack 11:90554d22ade5 107 result = select.wait();
NegativeBlack 11:90554d22ade5 108 if (result < -1) {
NegativeBlack 11:90554d22ade5 109 printf("Failed to select\n\r");
NegativeBlack 11:90554d22ade5 110 break;
NegativeBlack 9:a4c85bea2d77 111 }
NegativeBlack 11:90554d22ade5 112
NegativeBlack 11:90554d22ade5 113 // Get the first socket
NegativeBlack 11:90554d22ade5 114 socket = (tcp::Socket *)select.getReadable();
NegativeBlack 11:90554d22ade5 115
NegativeBlack 11:90554d22ade5 116 for (; socket != NULL; socket = (tcp::Socket *)select.getReadable()) {
NegativeBlack 11:90554d22ade5 117 // Check if there was a connection request.
NegativeBlack 11:90554d22ade5 118 if (socket->getHandle() == server.getHandle()) {
NegativeBlack 11:90554d22ade5 119 // Find an unused client
NegativeBlack 11:90554d22ade5 120 for (index = 0; index < MAX_CLIENTS; index++) {
NegativeBlack 11:90554d22ade5 121 if (client[index].getStatus() == network::Socket::Closed) {
NegativeBlack 11:90554d22ade5 122 break;
NegativeBlack 11:90554d22ade5 123 }
NegativeBlack 11:90554d22ade5 124 }
NegativeBlack 11:90554d22ade5 125
NegativeBlack 11:90554d22ade5 126 // Maximum connections reached
NegativeBlack 11:90554d22ade5 127 if (index == MAX_CLIENTS) {
NegativeBlack 11:90554d22ade5 128 printf("Maximum connections reached\n\r");
NegativeBlack 11:90554d22ade5 129 continue;
NegativeBlack 11:90554d22ade5 130 }
NegativeBlack 11:90554d22ade5 131
NegativeBlack 11:90554d22ade5 132 // Accept the client
NegativeBlack 11:90554d22ade5 133 socket->accept(client[index]);
NegativeBlack 11:90554d22ade5 134 printf("Client connected %s:%d\n\r",
NegativeBlack 11:90554d22ade5 135 client[index].getRemoteEndpoint().getAddress().toString().c_str(),
NegativeBlack 11:90554d22ade5 136 client[index].getRemoteEndpoint().getPort());
NegativeBlack 11:90554d22ade5 137
NegativeBlack 11:90554d22ade5 138 // Send a nice message to the client
NegativeBlack 11:90554d22ade5 139 client[index].write((void *)message.data(), message.size());
kkoichy 12:5e342c364d61 140 client_connected = 1;
kkoichy 12:5e342c364d61 141 mutex1.unlock();
kkoichy 12:5e342c364d61 142
NegativeBlack 11:90554d22ade5 143 continue;
NegativeBlack 11:90554d22ade5 144 }
NegativeBlack 11:90554d22ade5 145
NegativeBlack 11:90554d22ade5 146 // It was not the server socket, so it must be a client talking to us.
NegativeBlack 11:90554d22ade5 147 switch (socket->read(buffer)) {
NegativeBlack 10:9e8d5928537a 148 case 0:
NegativeBlack 11:90554d22ade5 149 // Remote end disconnected
NegativeBlack 11:90554d22ade5 150 printf("Client disconnected %s:%d\n\r",
NegativeBlack 11:90554d22ade5 151 socket->getRemoteEndpoint().getAddress().toString().c_str(),
NegativeBlack 11:90554d22ade5 152 socket->getRemoteEndpoint().getPort());
kkoichy 12:5e342c364d61 153 client_connected = 0;
kkoichy 12:5e342c364d61 154 mutex1.lock();
NegativeBlack 11:90554d22ade5 155 // Close socket
NegativeBlack 11:90554d22ade5 156 socket->close();
NegativeBlack 9:a4c85bea2d77 157 break;
NegativeBlack 11:90554d22ade5 158
NegativeBlack 11:90554d22ade5 159 case -1:
NegativeBlack 11:90554d22ade5 160 printf("Error while reading data from socket\n\r");
NegativeBlack 11:90554d22ade5 161 socket->close();
NegativeBlack 11:90554d22ade5 162 break;
NegativeBlack 11:90554d22ade5 163
NegativeBlack 9:a4c85bea2d77 164 default:
NegativeBlack 11:90554d22ade5 165 printf("Message from %s:%d\n\r",
NegativeBlack 11:90554d22ade5 166 socket->getRemoteEndpoint().getAddress().toString().c_str(),
NegativeBlack 11:90554d22ade5 167 socket->getRemoteEndpoint().getPort());
NegativeBlack 11:90554d22ade5 168
NegativeBlack 11:90554d22ade5 169 printf("%s\n\r", (char *)buffer.data());
kkoichy 12:5e342c364d61 170 Process_Data_TCP_IP((uint8_t *)buffer.data(), client);
NegativeBlack 11:90554d22ade5 171 break;
NegativeBlack 9:a4c85bea2d77 172 }
NegativeBlack 9:a4c85bea2d77 173 }
kkoichy 12:5e342c364d61 174 if(move_increment)
kkoichy 12:5e342c364d61 175 {
kkoichy 12:5e342c364d61 176 if(t.read_ms() > 100)
kkoichy 12:5e342c364d61 177 {
kkoichy 12:5e342c364d61 178 Moteur[move_increment].GetRB()->SetPosRelative(move_increment_val, Moteur[move_increment].GetChannel());
kkoichy 12:5e342c364d61 179 t.reset();
kkoichy 12:5e342c364d61 180 t.start();
kkoichy 12:5e342c364d61 181 }
kkoichy 12:5e342c364d61 182 else if(t2.read_ms() > 500)
kkoichy 12:5e342c364d61 183 {
kkoichy 12:5e342c364d61 184 move_increment = 0;
kkoichy 12:5e342c364d61 185 move_increment_val = 0;
kkoichy 12:5e342c364d61 186 }
kkoichy 12:5e342c364d61 187 }
kkoichy 12:5e342c364d61 188
NegativeBlack 11:90554d22ade5 189 } while (server.getStatus() == network::Socket::Listening);
kkoichy 12:5e342c364d61 190 }
kkoichy 12:5e342c364d61 191
kkoichy 12:5e342c364d61 192
kkoichy 12:5e342c364d61 193
kkoichy 12:5e342c364d61 194
kkoichy 12:5e342c364d61 195 int Process_Data_TCP_IP(uint8_t * data, tcp::Socket * client)
kkoichy 12:5e342c364d61 196 {
kkoichy 12:5e342c364d61 197 uint8_t nb_com = 0, cpt_com = 0, cpt_reply = 0;
kkoichy 12:5e342c364d61 198 int32_t param[3], dummy;
kkoichy 12:5e342c364d61 199 uint8_t reply[46];
kkoichy 12:5e342c364d61 200
kkoichy 12:5e342c364d61 201 reply[0] = 0xa5;
kkoichy 12:5e342c364d61 202 cpt_reply ++;
kkoichy 12:5e342c364d61 203
kkoichy 12:5e342c364d61 204 //Check Start byte
kkoichy 12:5e342c364d61 205 if(data[0] != 0xa5)
kkoichy 12:5e342c364d61 206 return -1;
kkoichy 12:5e342c364d61 207
kkoichy 12:5e342c364d61 208 switch(data[1 + cpt_com])
kkoichy 12:5e342c364d61 209 {
kkoichy 12:5e342c364d61 210 case 0x01 :
kkoichy 12:5e342c364d61 211 param[0] = data[cpt_com + 3] | data[cpt_com + 4] << 8 | data[cpt_com + 5] << 16 | data[cpt_com + 6] << 24;
kkoichy 12:5e342c364d61 212 param[1] = data[cpt_com + 7] | data[cpt_com + 8] << 8 | data[cpt_com + 9] << 16 | data[cpt_com + 10] << 24;
kkoichy 12:5e342c364d61 213 param[2] = data[cpt_com + 11] | data[cpt_com + 12] << 8 | data[cpt_com + 13] << 16 | data[cpt_com + 14] << 24;
kkoichy 12:5e342c364d61 214 Moteur[data[cpt_com + 2] - 1].Setparameters(param[0], param[1], param[2]);
kkoichy 12:5e342c364d61 215
kkoichy 12:5e342c364d61 216 break;
kkoichy 12:5e342c364d61 217
kkoichy 12:5e342c364d61 218 case 0x02 :
kkoichy 12:5e342c364d61 219 param[0] = data[cpt_com + 3] | data[cpt_com + 4] << 8 | data[cpt_com + 5] << 16 | data[cpt_com + 6] << 24;
kkoichy 12:5e342c364d61 220 Moteur[data[cpt_com + 2] - 1].GetRB()->SetPosition(param[0], Moteur[data[cpt_com + 2]].GetChannel());
kkoichy 12:5e342c364d61 221
kkoichy 12:5e342c364d61 222 break;
kkoichy 12:5e342c364d61 223
kkoichy 12:5e342c364d61 224 case 0x03 :
kkoichy 12:5e342c364d61 225 move_increment = data[cpt_com + 2] - 1;
kkoichy 12:5e342c364d61 226 t.reset();
kkoichy 12:5e342c364d61 227 t.start();
kkoichy 12:5e342c364d61 228 t2.reset();
kkoichy 12:5e342c364d61 229 t2.start();
kkoichy 12:5e342c364d61 230 move_increment_val = data[cpt_com + 3] | data[cpt_com + 4] << 8 | data[cpt_com + 5] << 16 | data[cpt_com + 6] << 24;
kkoichy 12:5e342c364d61 231
kkoichy 12:5e342c364d61 232 break;
kkoichy 12:5e342c364d61 233
kkoichy 12:5e342c364d61 234 case 0x11 :
kkoichy 12:5e342c364d61 235 if(cpt_reply > 1)
kkoichy 12:5e342c364d61 236 {
kkoichy 12:5e342c364d61 237 reply[cpt_reply] = 0xa6;
kkoichy 12:5e342c364d61 238 cpt_reply++;
kkoichy 12:5e342c364d61 239 }
kkoichy 12:5e342c364d61 240 reply[cpt_reply] = data[cpt_com + 2];
kkoichy 12:5e342c364d61 241 cpt_reply++;
kkoichy 12:5e342c364d61 242 dummy = Moteur[data[cpt_com + 2] - 1].GetRB()->ReadAbsoluteEncoderCount(Moteur[data[cpt_com + 2]].GetChannel());
kkoichy 12:5e342c364d61 243 memcpy(&reply[cpt_reply], &dummy, 4);
kkoichy 12:5e342c364d61 244 cpt_reply += 4;
kkoichy 12:5e342c364d61 245
kkoichy 12:5e342c364d61 246 break;
kkoichy 12:5e342c364d61 247
kkoichy 12:5e342c364d61 248 case 0x80 :
kkoichy 12:5e342c364d61 249 // Moteur[0]
kkoichy 12:5e342c364d61 250
kkoichy 12:5e342c364d61 251 break;
kkoichy 12:5e342c364d61 252 }//end switch
kkoichy 12:5e342c364d61 253 reply[cpt_reply] = 0xa7;
kkoichy 12:5e342c364d61 254 reply[cpt_reply + 1] = '\0';
kkoichy 12:5e342c364d61 255 client->write((void *)reply, cpt_reply + 1);
kkoichy 12:5e342c364d61 256
kkoichy 12:5e342c364d61 257 return 1;
kkoichy 12:5e342c364d61 258
kkoichy 12:5e342c364d61 259 }//end function
kkoichy 12:5e342c364d61 260
kkoichy 12:5e342c364d61 261 /********************/
kkoichy 12:5e342c364d61 262 // Fonction qui permet de modifier l'adresse MAC de la carte
kkoichy 12:5e342c364d61 263 // /!\ /!\ /!\ Le routeur doit être paramétré de façon adequate, pour attribuer une adresse IP définie à cette adresse MAC /!\ /!\ /!\
kkoichy 12:5e342c364d61 264 //(Dans un souci de simplicité, principalement, mais aussi pour différencier la carte des autres appareils se connectant au réseau)
kkoichy 12:5e342c364d61 265 extern "C" void mbed_mac_address(char *mac)
kkoichy 12:5e342c364d61 266 {
kkoichy 12:5e342c364d61 267 mac[0] = 0x12;
kkoichy 12:5e342c364d61 268 mac[1] = 0x34;
kkoichy 12:5e342c364d61 269 mac[2] = 0x56;
kkoichy 12:5e342c364d61 270 mac[3] = 0x78;
kkoichy 12:5e342c364d61 271 mac[4] = 0x9A;
kkoichy 12:5e342c364d61 272 mac[5] = 0xBC;
kkoichy 12:5e342c364d61 273 };
kkoichy 12:5e342c364d61 274