// connection NXT program for HSP3 // programed by takuya matsubara // http://nicotak.com // txtファイルをNXTへ送信します #uselib "nxtdll.dll" #cfunc nxtopen "nxtopen" nullptr #cfunc nxtcommand "nxtcommand" var,int,var,int #cfunc nxtwritefile "nxtwritefile" var,var,int #cfunc nxtreadfile "nxtreadfile" var,var,int #cfunc nxtdeletefile "nxtdeletefile" var #cfunc nxtclose "nxtclose" nullptr sdim filedata,1000 sdim filename,13 sdim cr,3 poke cr, 0, 13 // CR poke cr, 1, 10 // LF poke cr, 2, 0 // NULL filename = "program.txt" ret = nxtopen() if(ret < 0){ dialog "connection failed":end } // #define MOTORSPEED (-30) // motorpow = MOTORSPEED // gosub *setmotor // print "PortA Motor ON" // wait 300 // motorpow = 0 // gosub *setmotor // print "PortA Motor Stop" // wait 300 gosub *deletetxtfile gosub *writetxtfile // gosub *readtxtfile ret = nxtclose() end //------------------------- *writetxtfile filedata = "" filedata = filedata + "START" + cr filedata = filedata + "IF_DETECT_LINE 3" + cr filedata = filedata + "FOW" + cr filedata = filedata + "TURN_RIGHT" + cr filedata = filedata + "GOOL" + cr filesize = strlen(filedata) ret = nxtwritefile(filename,filedata,filesize) dialog "write file "+strlen(filedata)+"bytes" return //------------------------- *readtxtfile filedata = "" ret = nxtreadfile(filename,filedata,1000) print filedata dialog "read file "+strlen(filedata)+"bytes" return //------------------------- *deletetxtfile ret = nxtdeletefile(filename) dialog "delete file" return //------------------directcommand *setmotor #define TXCNT 11 #define RXCNT 2 sdim txbuf,TXCNT+1 sdim rxbuf,RXCNT+1 poke txbuf, 0, 4 // SETOUTPUTSTATE poke txbuf, 1, 0 // Output Port(0 - 2) poke txbuf, 2, motorpow // Power set point(-100 - 100) poke txbuf, 3, 1+2 // Mode byte(1=Motor on / 2=Brake / 4=Regulated) poke txbuf, 4, 0 // Regulation mode(0=No ragulation / 1=Control speed / 2=Motor sync) poke txbuf, 5, 0 // Turn ratio(-100 - 100) poke txbuf, 6, 0x20 // Run state(0x00=Idle / 0x10=Ramp up / 0x20=Running / 0x30=Ramp down) poke txbuf, 7, 0 // Tacho Limit poke txbuf, 8, 0 poke txbuf, 9, 0 poke txbuf,10, 0 ret = nxtcommand(txbuf,TXCNT,rxbuf,RXCNT) return