文字をエコーするプログラム |
---|
import stamp.core.*; public class echo { final static int SERIAL_TX_PIN = CPU.pin1; final static int SERIAL_RTS_PIN = CPU.pin2; final static int SERIAL_CTS_PIN = CPU.pin3; final static int SERIAL_RX_PIN = CPU.pin0; static Uart rxUart = new Uart( Uart.dirReceive, SERIAL_RX_PIN, Uart.dontInvert, SERIAL_CTS_PIN,Uart.speed9600, Uart.stop1 ); static Uart txUart = new Uart( Uart.dirTransmit, SERIAL_TX_PIN,Uart.dontInvert, SERIAL_RTS_PIN, Uart.speed9600, Uart.stop1 ); public static void main() { int ch; while(true) { ch = rxUart.receiveByte(); txUart.sendByte(ch); } } } |
Bluetoothのインターフェースのアドレスを取得するプログラム |
---|
import stamp.core.*; import stamp.util.text.*; public class getAddress { final static int SERIAL_TX_PIN = CPU.pin1; final static int SERIAL_RTS_PIN = CPU.pin2; final static int SERIAL_CTS_PIN = CPU.pin3; final static int SERIAL_RX_PIN = CPU.pin0; static Uart rxUart = new Uart( Uart.dirReceive, SERIAL_RX_PIN, Uart.dontInvert, SERIAL_CTS_PIN,Uart.speed9600, Uart.stop1 ); static Uart txUart = new Uart( Uart.dirTransmit, SERIAL_TX_PIN,Uart.dontInvert, SERIAL_RTS_PIN, Uart.speed9600, Uart.stop1 ); public static void main() { int ch; txUart.sendString("get address\r"); waitString("ACK\r"); int n = 17; char[] adr = getString(n); waitString(">"); for(int i=0; i%lt;n; i++) Format.printf("%c",adr[i]); System.out.println(""); } public static void waitString(String str){ int i=0; while(i<str.length()) { if( rxUart.receiveByte() == str.charAt(i)) i++; else i=0; } } public static char [] getString(int n){ char [] tmp = new char[n]; for(int i=0; i<n; i++) tmp[i] = (char)rxUart.receiveByte(); return tmp; } } |
Blootoothに関係するプログラム |
---|
package JBot; import stamp.core.*; import stamp.util.text.*; public class Bluetooth { final static int SERIAL_TX_PIN = CPU.pin1; final static int SERIAL_RTS_PIN = CPU.pin2; final static int SERIAL_CTS_PIN = CPU.pin3; final static int SERIAL_RX_PIN = CPU.pin0; protected final static int CONNECTION = CPU.pin6; static Uart rxUart = new Uart( Uart.dirReceive, SERIAL_RX_PIN, Uart.dontInvert, SERIAL_CTS_PIN,Uart.speed9600, Uart.stop1 ); static Uart txUart = new Uart( Uart.dirTransmit, SERIAL_TX_PIN,Uart.dontInvert, SERIAL_RTS_PIN, Uart.speed9600, Uart.stop1 ); public static void waitString(String str){ int i=0; while(i<str.length()) { if( rxUart.receiveByte() == str.charAt(i)) i++; else i=0; } } // n文字受信する public static char [] getString(int n){ char [] tmp = new char[n]; for(int i=0; i<n; i++) tmp[i] = (char)rxUart.receiveByte(); return tmp; } // アドレスを得る public static String getAddress(){ int ch; txUart.sendString("get address\r"); waitString("ACK\r"); int n = 17; char[] adr = getString(n); waitString(">"); return new String(adr); } // EB500が接続されたのを待つ public static void waitConnection() { while(!CPU.readPin(CPU.pin5)); } public static char getChar() { return (char)rxUart.receiveByte(); } // 文字列を送る public static void sendString(String str) { rxUart.sendString(str); } } |
メインプログラム |
---|
import stamp.core.*; import JBot.*; public class addressGet2 extends Bluetooth{ public static void main() { String address = getAddress(); System.out.println(address); } } |
サービスプログラムからのコマンドを表示する |
---|
import stamp.core.*; import stamp.util.text.*; /** * Microsoft Robot Studio用 */ public class JBotControlForMsrs { static byte ResetOnOff = 0; static int[] RequestConnect = {0xFF, 0, 1, 0, 0}; static int[] ConnectionGranted = {0xFF, 0, 2, 0, 0}; static int[] RequestCommand = {0xFF, 0, 3, 0, 0}; static int[] ServoSpeeds = {0xFF, 0, 4, 0, 0}; static int tLeft; // Servo control pulse durations static int tRight; static int [] buffer = {0xff,0,0,0,0}; static byte duration; static byte frequency; static int msgIndex=0; // message index static boolean Error=false; final static int SERIAL_TX_PIN = CPU.pin1; final static int SERIAL_RTS_PIN = CPU.pin2; final static int SERIAL_CTS_PIN = CPU.pin3; final static int SERIAL_RX_PIN = CPU.pin0; static Uart rxUart = new Uart( Uart.dirReceive, SERIAL_RX_PIN, Uart.dontInvert, SERIAL_CTS_PIN,Uart.speed9600, Uart.stop1 ); static Uart txUart = new Uart( Uart.dirTransmit, SERIAL_TX_PIN,Uart.dontInvert, SERIAL_RTS_PIN, Uart.speed9600, Uart.stop1 ); static byte routine; // Routine selector static byte counter; static boolean flagWhiskers; // Act on Whisker values static boolean flagDigSens; // Send back digital sensors 1/0s // with every reply static boolean flagIr; static boolean tempBit; final static int LED = CPU.pin15; // To control the L.E.D. final static int LED2 = CPU.pin14; // To control the L.E.D. final static int SPEAKER = CPU.pin4; // To speaker final static boolean ONSTATE = true; // Button Pressed Down final static boolean OFFSTATE = false; // Button Normal State static Freqout freqout = new Freqout ( SPEAKER ) ; public static void main() { CPU.writePin(LED,ONSTATE); freqout.freqout ( 450, 750 ) ; // 4.5kHz, 75ms CPU.writePin(LED,OFFSTATE); do { init(); //---------------------- Main Routine --------------------------; do { getNextCommand(); // エラーがあった場合スピーカーを鳴らす if(Error) freqout.freqout ( 400, 500 ); //リクエストコマンド RequestCommand[1] = ++msgIndex; for(int i=0; i<5; i++) txUart.sendByte(RequestCommand[i]); } while (CPU.readPin(CPU.pin5)); } while(true); } public static void init() { do { // eb500が準備できたのを待つ CPU.writePin(LED,ONSTATE); CPU.delay(1000); // 1秒 CPU.writePin(LED,OFFSTATE); // EB500が接続されたのを待つ while(!CPU.readPin(CPU.pin5)); // 接続リクエスト for(int i=0; i<5; i++) txUart.sendByte(RequestConnect[i]); // 接続確認 buffer[0] = 0xff; Confirm_Connect: do { for(int i=0; i<5; i++) txUart.sendByte(ConnectionGranted[i]); while( (rxUart.receiveByte()& 0xff) != 0xff); for(int i=1; i<5; i++) buffer[i] = rxUart.receiveByte()&0xff; for(int i=0; i<5; i++) Format.printf("%02x ",buffer[i]); System.out.println(" "); if((buffer[0]==0xff)&&(buffer[2]==2)) break Confirm_Connect; } while(true); //リクエストコマンド RequestCommand[1] = (++msgIndex)&0xff; for(int i=0; i<5; i++) txUart.sendByte(RequestCommand[i]); } while (!CPU.readPin(CPU.pin5)); } public static int getNextCommand(){ int oldIndex = buffer[1]; Error = false;; buffer[0] = 0xff; do { do { while( (rxUart.receiveByte()& 0xff) != 0xff); buffer[1] = rxUart.receiveByte()& 0xff; if(oldIndex == buffer[1]) continue; for(int i=2; i<5; i++) buffer[i] = rxUart.receiveByte()&0xff; } while(oldIndex == buffer[1]); if(((oldIndex+1)&0xff) != buffer[1]) { Error = true; } for(int i=0; i<5; i++) Format.printf("%02x ",buffer[i]); Format.printf("\n"); if(buffer[2] == 192 ) { freqout.freqout ( 100, 900 ) ; System.out.println("reset command"); msgIndex=0; init(); } else break; }while(true); return buffer[2]; } } |
触角・IRのデータを送る部分 |
---|
//リクエストコマンド RequestCommand[1] = ++msgIndex; RequestCommand[3] = 0xff; if(CPU.readPin(CPU.pin7) == false) RequestCommand[3]&=0xf7; if(CPU.readPin(CPU.pin8) == false) RequestCommand[3]&=0xfb; if(CPU.readPin(CPU.pin9) == false) RequestCommand[3]&=0xfd; if(CPU.readPin(CPU.pin10) == false) RequestCommand[3]&=0xfe; for(int i=0; i<5; i++) txUart.sendByte(RequestCommand[i]); |
Start Byte | Message Index | Command | Data1 | Data2 |
---|---|---|---|---|
0 ---- PCにハンドシェークを繰り返すように指示する。(PCからの指示の場合は192) | ||||
255 | index | 0 | don't care | don't care |
1 ---- Javelinがハンドシェークを開始する。 | ||||
255 | index | 1 | don't care | don't care |
2 ---- PCがハンドシェークの開始の受け取りを確認する。 | ||||
255 | index | 2 | don't care | don't care |
3 ---- JavelinがPCに次のコマンドを要求する。 | ||||
255 | index | 3 | don't care | don't care |
左右のサーボモーターの数字の範囲は0から200である。 | ||||
255 | index | 32 | left speed | right speed |
| ||||
255 | index | 33 | character | don't care |
| ||||
255 | index | 64 | xxxxxxLR | don't care |
| ||||
255 | index | 65 | xxxxLRxx | don't care |
| ||||
255 | index | 96 | duration | frequency |
各々のbyteの上位nibbleには命令(0~4)、下位nibbleにはピン番号(0~15)を保持している。 | ||||
255 | index | 97 | command/pin | command/pin |
| ||||
255 | index | 98 | low byte | high byte |
| ||||
255 | index | 128 | don't care | don't care |
| ||||
255 | index | 129 | don't care | don't care |
| ||||
255 | index | 130 | don't care | don't care |
160-162 ---- 128-130の機能を無効にする。 | ||||
255 | index | 160, 161 or 162 | don't care | don't care |
192 --- メッセージインデックスをリセットし、ハンドシェークを開始する。 | ||||
255 | index | 192 | don't care | don't care |