Program the control the fischertechnik robo interface or intelligent interface via tcp socket or via a java gui.

Dependencies:   mbed ConfigFile

Committer:
networker
Date:
Fri Dec 31 14:01:14 2010 +0000
Revision:
0:7f26f0680202
Child:
1:2c9d412ad471
initial release: comprises ftlib (no usb), ft-over-ip socket server, and the http server (the html page and java jar I still have to publish somewhere)

Who changed what in which revision?

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