Program the control the fischertechnik robo interface or intelligent interface via tcp socket or via a java gui.
main.cpp@1:2c9d412ad471, 2011-05-04 (annotated)
- Committer:
- networker
- Date:
- Wed May 04 15:42:13 2011 +0000
- Revision:
- 1:2c9d412ad471
- Parent:
- 0:7f26f0680202
first publication (mbed challenge submission)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
networker | 0:7f26f0680202 | 1 | #include "mbed.h" |
networker | 0:7f26f0680202 | 2 | #include "EthernetNetIf.h" |
networker | 0:7f26f0680202 | 3 | #include "HTTPServer.h" |
networker | 0:7f26f0680202 | 4 | #include "myconfig.h" |
networker | 0:7f26f0680202 | 5 | #define MBED |
networker | 0:7f26f0680202 | 6 | #include "ft.h" |
networker | 0:7f26f0680202 | 7 | #include "ftlib.h" |
networker | 0:7f26f0680202 | 8 | #include "ftserver.h" |
networker | 0:7f26f0680202 | 9 | #include "msgSerial.h" |
networker | 0:7f26f0680202 | 10 | |
networker | 0:7f26f0680202 | 11 | /**************************************************** |
networker | 0:7f26f0680202 | 12 | * pin configuration for ft test board |
networker | 0:7f26f0680202 | 13 | * |
networker | 0:7f26f0680202 | 14 | * 5 mosi for external SD card pin3 pin2=3.3V pin1=GND |
networker | 0:7f26f0680202 | 15 | * 6 miso for external SD card pin4 |
networker | 0:7f26f0680202 | 16 | * 7 sck for external SD card pin5 |
networker | 0:7f26f0680202 | 17 | * 11 cs for external SD card pin6 |
networker | 0:7f26f0680202 | 18 | * |
networker | 0:7f26f0680202 | 19 | * 8 dir for RS485 |
networker | 0:7f26f0680202 | 20 | * 9 TX for RS485/TTL/RS232(D) (COM1) |
networker | 0:7f26f0680202 | 21 | * 10 RX for RS485/TTL/RS232(D) |
networker | 0:7f26f0680202 | 22 | * |
networker | 0:7f26f0680202 | 23 | * 13 TX for RS232/MIDI (COM2) |
networker | 0:7f26f0680202 | 24 | * 14 RX for RS232/MIDI |
networker | 0:7f26f0680202 | 25 | * |
networker | 0:7f26f0680202 | 26 | * 12 spare6 |
networker | 0:7f26f0680202 | 27 | * 15 spare5 |
networker | 0:7f26f0680202 | 28 | * 16 spare4 |
networker | 0:7f26f0680202 | 29 | * 17 spare3 |
networker | 0:7f26f0680202 | 30 | * 18 spare2 (spare1 is GND) |
networker | 0:7f26f0680202 | 31 | * |
networker | 0:7f26f0680202 | 32 | * 19 analog1 (analog3 is +3.3V) |
networker | 0:7f26f0680202 | 33 | * 20 analog2 (analog4 is GND) |
networker | 0:7f26f0680202 | 34 | * |
networker | 0:7f26f0680202 | 35 | * 21 motor en3-4 |
networker | 0:7f26f0680202 | 36 | * 22 motor 3 |
networker | 0:7f26f0680202 | 37 | * 23 motor 4 |
networker | 0:7f26f0680202 | 38 | * 24 motor 2 |
networker | 0:7f26f0680202 | 39 | * 25 motor 1 |
networker | 0:7f26f0680202 | 40 | * 26 motor en1-2 |
networker | 0:7f26f0680202 | 41 | * |
networker | 0:7f26f0680202 | 42 | * 27 SCL |
networker | 0:7f26f0680202 | 43 | * 28 SDA |
networker | 0:7f26f0680202 | 44 | * |
networker | 0:7f26f0680202 | 45 | * 29 yellow LED on MAGJACK |
networker | 0:7f26f0680202 | 46 | * 30 green LED on MAGJACK |
networker | 0:7f26f0680202 | 47 | *****************************************************/ |
networker | 0:7f26f0680202 | 48 | //#define FIO1PIN (*(unsigned long*)0x2009C034UL) |
networker | 0:7f26f0680202 | 49 | #define FIO1PIN (LPC_GPIO1->FIOPIN) |
networker | 0:7f26f0680202 | 50 | |
networker | 0:7f26f0680202 | 51 | #define HWID 1 |
networker | 0:7f26f0680202 | 52 | #define SUBID 100 |
networker | 0:7f26f0680202 | 53 | |
networker | 0:7f26f0680202 | 54 | EthernetNetIf eth; |
networker | 1:2c9d412ad471 | 55 | HTTPServer svr; |
networker | 0:7f26f0680202 | 56 | ftServer *server; |
networker | 0:7f26f0680202 | 57 | myConfig config; |
networker | 0:7f26f0680202 | 58 | |
networker | 0:7f26f0680202 | 59 | LocalFileSystem local("local"); |
networker | 1:2c9d412ad471 | 60 | LocalFileSystem fs("webfs"); |
networker | 0:7f26f0680202 | 61 | |
networker | 0:7f26f0680202 | 62 | DigitalOut Green(p30), Yellow(p29); |
networker | 0:7f26f0680202 | 63 | |
networker | 0:7f26f0680202 | 64 | msgSerial *serialport=0; |
networker | 0:7f26f0680202 | 65 | |
networker | 0:7f26f0680202 | 66 | FT_TRANSFER_AREA *ta = 0; |
networker | 0:7f26f0680202 | 67 | FT_HANDLE h = 0; |
networker | 0:7f26f0680202 | 68 | int trigger_interface = 0; |
networker | 0:7f26f0680202 | 69 | |
networker | 0:7f26f0680202 | 70 | char *names[] = {"NO_FT_DEVICE","FT_AUTO_TYPE","FT_INTELLIGENT_IF","FT_INTELLIGENT_IF_SLAVE","FT_ROBO_IF_IIM", |
networker | 0:7f26f0680202 | 71 | "FT_ROBO_IF_USB","FT_ROBO_IF_COM","FT_ROBO_IF_OVER_RF","FT_ROBO_IO_EXTENSION","FT_ROBO_RF_DATA_LINK" |
networker | 0:7f26f0680202 | 72 | }; |
networker | 0:7f26f0680202 | 73 | int types[] = {NO_FT_DEVICE,FT_AUTO_TYPE,FT_INTELLIGENT_IF,FT_INTELLIGENT_IF_SLAVE,FT_ROBO_IF_IIM, |
networker | 0:7f26f0680202 | 74 | FT_ROBO_IF_USB,FT_ROBO_IF_COM,FT_ROBO_IF_OVER_RF,FT_ROBO_IO_EXTENSION,FT_ROBO_RF_DATA_LINK,NO_FT_DEVICE//this last one is the default |
networker | 0:7f26f0680202 | 75 | }; |
networker | 0:7f26f0680202 | 76 | char *modes[] = {"cable","ftlib","serial"}; |
networker | 0:7f26f0680202 | 77 | int type = NO_FT_DEVICE; |
networker | 0:7f26f0680202 | 78 | int mode = 0; |
networker | 0:7f26f0680202 | 79 | |
networker | 0:7f26f0680202 | 80 | unsigned short encode(char*); |
networker | 0:7f26f0680202 | 81 | void callback(void*ctx); |
networker | 0:7f26f0680202 | 82 | void *context = 0; |
networker | 0:7f26f0680202 | 83 | void messageCB(SMESSAGE*); |
networker | 0:7f26f0680202 | 84 | NOTIFICATION_EVENTS ne = {callback, context, 0, 0, 0, messageCB}; |
networker | 0:7f26f0680202 | 85 | unsigned short msgID = encode("set"); |
networker | 0:7f26f0680202 | 86 | |
networker | 1:2c9d412ad471 | 87 | void http_server() { |
networker | 1:2c9d412ad471 | 88 | FSHandler::mount("/webfs", "/files"); //Mount /webfs path on /files web path |
networker | 1:2c9d412ad471 | 89 | svr.addHandler<FSHandler>("/files"); |
networker | 1:2c9d412ad471 | 90 | svr.bind(80); |
networker | 1:2c9d412ad471 | 91 | printf("Listening for http requests...\n"); |
networker | 1:2c9d412ad471 | 92 | } |
networker | 0:7f26f0680202 | 93 | |
networker | 0:7f26f0680202 | 94 | unsigned short encode(char* s) { //convert 3 character command to 16bit value |
networker | 0:7f26f0680202 | 95 | unsigned short r = 0; |
networker | 0:7f26f0680202 | 96 | for (int i = 0; i < 3 && *s != '\0'; i++) { |
networker | 0:7f26f0680202 | 97 | r *= 40; |
networker | 0:7f26f0680202 | 98 | if (s[i] >= '0' && s[i] <= '9') { |
networker | 0:7f26f0680202 | 99 | r += s[i]; |
networker | 0:7f26f0680202 | 100 | r -= '0' - 27; |
networker | 0:7f26f0680202 | 101 | } else if (s[i] >= 'A' && s[i] <= 'Z') { |
networker | 0:7f26f0680202 | 102 | r += s[i]; |
networker | 0:7f26f0680202 | 103 | r -= 'A' - 1; |
networker | 0:7f26f0680202 | 104 | } else if (s[i] >= 'a' && s[i] <= 'z') { |
networker | 0:7f26f0680202 | 105 | r += s[i]; |
networker | 0:7f26f0680202 | 106 | r -= 'a' - 1; |
networker | 0:7f26f0680202 | 107 | } else |
networker | 0:7f26f0680202 | 108 | switch (s[i]) { |
networker | 0:7f26f0680202 | 109 | case '+': |
networker | 0:7f26f0680202 | 110 | r += 37; |
networker | 0:7f26f0680202 | 111 | break; |
networker | 0:7f26f0680202 | 112 | case '-': |
networker | 0:7f26f0680202 | 113 | r += 38; |
networker | 0:7f26f0680202 | 114 | break; |
networker | 0:7f26f0680202 | 115 | case '!': |
networker | 0:7f26f0680202 | 116 | r += 39; |
networker | 0:7f26f0680202 | 117 | break; |
networker | 0:7f26f0680202 | 118 | } |
networker | 0:7f26f0680202 | 119 | } |
networker | 0:7f26f0680202 | 120 | return r; |
networker | 0:7f26f0680202 | 121 | } |
networker | 0:7f26f0680202 | 122 | |
networker | 0:7f26f0680202 | 123 | void handleMessage(char m[7]) { //called when a valid tcpip message is received |
networker | 0:7f26f0680202 | 124 | SMESSAGE *msg = (SMESSAGE*)(m+1); |
networker | 0:7f26f0680202 | 125 | switch (mode) { |
networker | 0:7f26f0680202 | 126 | case 0://cable |
networker | 0:7f26f0680202 | 127 | if (msg->ucHwId == HWID && msg->ucSubId == SUBID && msg->uiMsgId == msgID && ta != 0) { //check message values |
networker | 0:7f26f0680202 | 128 | ta->M_Main = msg->ucB2; //long cable semantics, copy the message value to the motor outputs (16 bit only!) |
networker | 0:7f26f0680202 | 129 | ta->M_Sub1 = msg->ucB3; |
networker | 0:7f26f0680202 | 130 | } |
networker | 0:7f26f0680202 | 131 | break; |
networker | 0:7f26f0680202 | 132 | case 1://ftlib, not (yet) implemented |
networker | 0:7f26f0680202 | 133 | if (h) |
networker | 0:7f26f0680202 | 134 | SendFtMessage(h, msg->ucHwId, msg->ucSubId, msg->dw, 0, MSG_SEND_NORMAL); //use the ftlib function to forward the message to the interface |
networker | 0:7f26f0680202 | 135 | break; |
networker | 0:7f26f0680202 | 136 | case 2://serial |
networker | 0:7f26f0680202 | 137 | if (serialport) |
networker | 0:7f26f0680202 | 138 | serialport->write(m); |
networker | 0:7f26f0680202 | 139 | break; |
networker | 0:7f26f0680202 | 140 | } |
networker | 0:7f26f0680202 | 141 | } |
networker | 0:7f26f0680202 | 142 | |
networker | 0:7f26f0680202 | 143 | void OnSerMsg(char *m) { |
networker | 0:7f26f0680202 | 144 | if (server && mode==2)//serial |
networker | 0:7f26f0680202 | 145 | server->writeStream((SMESSAGE*)(m+1)); |
networker | 0:7f26f0680202 | 146 | } |
networker | 0:7f26f0680202 | 147 | |
networker | 0:7f26f0680202 | 148 | void callback(void*ctx) { //new data from interface has arrived |
networker | 0:7f26f0680202 | 149 | if (mode==0 && ta->ChangeEg) {//cable |
networker | 0:7f26f0680202 | 150 | SMESSAGE m = { {HWID, SUBID} }; |
networker | 0:7f26f0680202 | 151 | m.uiMsgId = msgID; |
networker | 0:7f26f0680202 | 152 | m.ucB2 = ta->E_Main; |
networker | 0:7f26f0680202 | 153 | m.ucB3 = ta->E_Sub1; |
networker | 0:7f26f0680202 | 154 | if (server) |
networker | 0:7f26f0680202 | 155 | server->writeStream(&m); |
networker | 0:7f26f0680202 | 156 | // printf("%02X\n", ta->E_Main); |
networker | 0:7f26f0680202 | 157 | } |
networker | 0:7f26f0680202 | 158 | } |
networker | 0:7f26f0680202 | 159 | |
networker | 0:7f26f0680202 | 160 | void messageCB(SMESSAGE *msg) {// not (yet) implemented |
networker | 0:7f26f0680202 | 161 | if (server && mode==1)//ftlib |
networker | 0:7f26f0680202 | 162 | server->writeStream(msg); |
networker | 0:7f26f0680202 | 163 | } |
networker | 0:7f26f0680202 | 164 | |
networker | 0:7f26f0680202 | 165 | void init_ft_interface() { |
networker | 0:7f26f0680202 | 166 | char comport[5]= "COM1"; |
networker | 0:7f26f0680202 | 167 | InitFtLib(); |
networker | 0:7f26f0680202 | 168 | unsigned ver = GetLibVersion(); |
networker | 0:7f26f0680202 | 169 | printf("libver = %08X\n", ver); |
networker | 0:7f26f0680202 | 170 | |
networker | 0:7f26f0680202 | 171 | if (!config.read("/local/ft.ini")) |
networker | 1:2c9d412ad471 | 172 | printf("reading of configfile failed!\n"); |
networker | 0:7f26f0680202 | 173 | mode = config.getLookup("mode", modes, 3); |
networker | 0:7f26f0680202 | 174 | switch (mode) { |
networker | 0:7f26f0680202 | 175 | case 0://cable (requires ftlib too) |
networker | 0:7f26f0680202 | 176 | case 1://ftlib |
networker | 0:7f26f0680202 | 177 | type = types[config.getLookup("FtInterface", names, sizeof(names)/sizeof(char*))]; |
networker | 0:7f26f0680202 | 178 | if (type==FT_INTELLIGENT_IF || type==FT_INTELLIGENT_IF_SLAVE || type==FT_ROBO_IF_IIM || type==FT_ROBO_IF_COM) {//serial device |
networker | 0:7f26f0680202 | 179 | if (!config.getValue("FtCommPort", comport, sizeof(comport))) |
networker | 0:7f26f0680202 | 180 | printf("FtCommPort not found, using default\n"); |
networker | 0:7f26f0680202 | 181 | printf("opening port %s by Ftlib\n", comport); |
networker | 0:7f26f0680202 | 182 | h = OpenFtCommDevice(comport, type, 5); |
networker | 0:7f26f0680202 | 183 | if (h) { |
networker | 0:7f26f0680202 | 184 | printf("OpenFtCommDevice on %s succeeded\n", comport); |
networker | 0:7f26f0680202 | 185 | //long v = GetFtFirmware(h); |
networker | 0:7f26f0680202 | 186 | //printf("FW: %08lX\n", v); |
networker | 0:7f26f0680202 | 187 | ta = GetFtTransferAreaAddress(h);//always get before starting otherwise not imm avail in callback |
networker | 0:7f26f0680202 | 188 | int err = StartFtTransferAreaWithCommunication(h, &ne); |
networker | 0:7f26f0680202 | 189 | if (err==0) { |
networker | 0:7f26f0680202 | 190 | printf("transfer started\n"); |
networker | 0:7f26f0680202 | 191 | for (int i = 0; i < 8; i++) { |
networker | 0:7f26f0680202 | 192 | ta->MPWM_Main[i] = 7; |
networker | 0:7f26f0680202 | 193 | ta->MPWM_Sub1[i] = 7; |
networker | 0:7f26f0680202 | 194 | } |
networker | 0:7f26f0680202 | 195 | } else |
networker | 0:7f26f0680202 | 196 | printf("Error %08X: %s\n", err, GetFtLibErrorString(err,1)); |
networker | 0:7f26f0680202 | 197 | } |
networker | 1:2c9d412ad471 | 198 | printf("Opening Ft device failed\n"); |
networker | 0:7f26f0680202 | 199 | } else { |
networker | 0:7f26f0680202 | 200 | printf("devicetype %d is not supported\n", type); |
networker | 0:7f26f0680202 | 201 | } |
networker | 0:7f26f0680202 | 202 | break; |
networker | 0:7f26f0680202 | 203 | case 2://serial |
networker | 0:7f26f0680202 | 204 | if (!config.getValue("FtCommPort", comport, sizeof(comport))) |
networker | 0:7f26f0680202 | 205 | printf("FtCommPort not found, using default\n"); |
networker | 0:7f26f0680202 | 206 | printf("opening port %s for messaging\n", comport); |
networker | 0:7f26f0680202 | 207 | switch (comport[3]) { |
networker | 0:7f26f0680202 | 208 | case '1': |
networker | 0:7f26f0680202 | 209 | serialport = new msgSerial(p9, p10, OnSerMsg); |
networker | 0:7f26f0680202 | 210 | break; |
networker | 0:7f26f0680202 | 211 | case '2': |
networker | 0:7f26f0680202 | 212 | serialport = new msgSerial(p13, p14, OnSerMsg); |
networker | 0:7f26f0680202 | 213 | break; |
networker | 0:7f26f0680202 | 214 | case '3': |
networker | 0:7f26f0680202 | 215 | serialport = new msgSerial(p28, p27, OnSerMsg); |
networker | 0:7f26f0680202 | 216 | break; |
networker | 0:7f26f0680202 | 217 | default: |
networker | 0:7f26f0680202 | 218 | // serialport = &viaUsb; |
networker | 0:7f26f0680202 | 219 | printf("USB testoutput not supported for messages\n"); |
networker | 0:7f26f0680202 | 220 | break; |
networker | 0:7f26f0680202 | 221 | } |
networker | 0:7f26f0680202 | 222 | break; |
networker | 0:7f26f0680202 | 223 | default: |
networker | 0:7f26f0680202 | 224 | printf("Mode %d is not supported or unknown\n",mode); |
networker | 0:7f26f0680202 | 225 | } |
networker | 0:7f26f0680202 | 226 | } |
networker | 0:7f26f0680202 | 227 | |
networker | 0:7f26f0680202 | 228 | int main() { |
networker | 0:7f26f0680202 | 229 | init_ft_interface(); |
networker | 0:7f26f0680202 | 230 | |
networker | 1:2c9d412ad471 | 231 | printf("Setting up ethernet...\n"); |
networker | 0:7f26f0680202 | 232 | EthernetErr ethErr = eth.setup(); |
networker | 0:7f26f0680202 | 233 | if (ethErr) { |
networker | 1:2c9d412ad471 | 234 | printf("Error %d in ethernet setup.\n", ethErr); |
networker | 0:7f26f0680202 | 235 | return -1; |
networker | 0:7f26f0680202 | 236 | } |
networker | 1:2c9d412ad471 | 237 | printf("Setup of ethernet OK\n"); |
networker | 0:7f26f0680202 | 238 | |
networker | 1:2c9d412ad471 | 239 | printf("starting http server\n"); |
networker | 0:7f26f0680202 | 240 | http_server(); |
networker | 0:7f26f0680202 | 241 | |
networker | 1:2c9d412ad471 | 242 | printf("creating ft socket server\n"); |
networker | 0:7f26f0680202 | 243 | server = new ftServer(IpAddr(),config.getInt("port",5000)); |
networker | 0:7f26f0680202 | 244 | |
networker | 1:2c9d412ad471 | 245 | printf("starting ft socket server\n"); |
networker | 0:7f26f0680202 | 246 | if (server->startServer()) { |
networker | 0:7f26f0680202 | 247 | for (;;) {//main loop |
networker | 0:7f26f0680202 | 248 | Net::poll(); |
networker | 0:7f26f0680202 | 249 | // server->pollServer(); |
networker | 0:7f26f0680202 | 250 | if (trigger_interface) { |
networker | 0:7f26f0680202 | 251 | if (h->guardedFtThreadBegin()) |
networker | 0:7f26f0680202 | 252 | trigger_interface = 0; |
networker | 0:7f26f0680202 | 253 | } |
networker | 0:7f26f0680202 | 254 | Green = !(FIO1PIN&(1<<25));//link |
networker | 0:7f26f0680202 | 255 | Yellow= !(FIO1PIN&(1<<26));//speed |
networker | 0:7f26f0680202 | 256 | } |
networker | 0:7f26f0680202 | 257 | } else |
networker | 0:7f26f0680202 | 258 | printf("server could not be started\n"); |
networker | 0:7f26f0680202 | 259 | |
networker | 0:7f26f0680202 | 260 | if (server) |
networker | 0:7f26f0680202 | 261 | delete server; |
networker | 0:7f26f0680202 | 262 | if (serialport) |
networker | 0:7f26f0680202 | 263 | delete serialport; |
networker | 0:7f26f0680202 | 264 | if (h) { |
networker | 0:7f26f0680202 | 265 | StopFtTransferArea(h); |
networker | 0:7f26f0680202 | 266 | CloseFtDevice(h); |
networker | 0:7f26f0680202 | 267 | } |
networker | 0:7f26f0680202 | 268 | CloseFtLib(); |
networker | 1:2c9d412ad471 | 269 | printf("end of program"); |
networker | 0:7f26f0680202 | 270 | return 0; |
networker | 0:7f26f0680202 | 271 | } |