找回密码
 马上注册

QQ登录

只需一步,快速开始

查看: 7644|回复: 3

研究了几天PC对NXT的控制

[复制链接]
发表于 2012-6-4 22:07:51 | 显示全部楼层 |阅读模式
没教程,还真不容易啊 ~!全都是JAVA

NXT端:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import lejos.nxt.Button;
import lejos.nxt.ButtonListener;
import lejos.nxt.LCD;
import lejos.nxt.Motor;
import lejos.nxt.SensorPort;
import lejos.nxt.UltrasonicSensor;
import lejos.nxt.comm.USB;
import lejos.nxt.comm.USBConnection;

public class NxtToPc {
    public static USBConnection USBLink;
    public static DataOutputStream dataOut;
    public static DataInputStream dataIn;
    public static boolean b;   
    public static  int ReceivedData; //接收PC传入的数
    public static int speed=60;   //马达速度
    public static int turnspeed=100;  //转弯速度
    public static int movespeed;   //移动速度
    static byte[] OutData={0,0} ; //输出数组,用于保存几个数值
    /**
     * @param args
     */
   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        final UltrasonicSensor us=new UltrasonicSensor(SensorPort.S4);
        b=false;
        //监听ESCAPE按钮
        Button.ESCAPE.addButtonListener(new ButtonListener()
        {            
            @Override
            public void buttonReleased(Button arg0) {}            
            @Override
            public void buttonPressed(Button arg0) {
                System.exit(0);               
            }
        });
        connect();
        
        Runnable runnable1 = new Runnable(){
            @Override
            public void run() {
                while(true)
                {
                    try
                    {
                        LCD.clearDisplay();
                        LCD.drawString("Ultra: "+us.getDistance(), 0, 4);
                        OutData[0]=(byte) us.getDistance();
                        OutData[1]=(byte) Motor.A.getSpeed();
                        dataOut.write(OutData,0,2);  //写数据方式
                        dataOut.flush();   //刷出数据到PC
                    }
                    catch (IOException e) {e.printStackTrace();}
                }
            }};
            
            Runnable runnable2 = new Runnable(){
                @Override
                public void run() {
                    while(true)
                    {   
                        try {
                            ReceivedData=dataIn.readInt();
                           
                            Motor.C.setSpeed(speed);
                            Motor.A.setSpeed(speed);
                           
                            if(ReceivedData==1)  //向前
                            {
                                Motor.C.forward();
                                Motor.A.forward();                                
                            }
                            if(ReceivedData==5) //停止
                            {                    
                                Motor.A.stop();
                                Motor.C.stop();                    
                            }
                            if(ReceivedData==6) //加速
                            {
                                if(speed>720)
                                    speed=720;
                                else
                                    speed+=60;
                            }
                            if(ReceivedData==7) //减速
                            {
                                if(speed<60)
                                    speed=60;
                                else
                                    speed-=60;               
                            }               

                            if(ReceivedData==2)  //向后
                            {
                                Motor.A.backward();
                                Motor.C.backward();
                            }
                            if(ReceivedData==3)  //转右
                            {
                                Motor.A.setSpeed(turnspeed);
                                Motor.C.setSpeed(turnspeed);
                                Motor.A.backward();
                                Motor.C.forward();
                            }
                            if(ReceivedData==4)  //转左
                            {
                                Motor.A.setSpeed(turnspeed);
                                Motor.C.setSpeed(turnspeed);
                                Motor.A.forward();
                                Motor.C.backward();
                            }
                        } catch (IOException e) {e.printStackTrace();}   
                    }
                }};
        Thread thread1 = new Thread(runnable1);
        Thread thread2 = new Thread(runnable2);
        thread1.start();
        thread2.start();
    }   
    private static void connect() {
        //BTLink = Bluetooth.waitForConnection();   
        //dataOut = BTLink.openDataOutputStream();
        //dataIn = BTLink.openDataInputStream();
        System.out.println("Listening");
        USBLink = USB.waitForConnection();
        dataOut = USBLink.openDataOutputStream();
        dataIn = USBLink.openDataInputStream();
    }
}




PC端:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import lejos.pc.comm.*;
import java.awt.Font;
  
public class PcToNxt {   

    //自定义
    static boolean connected; //连接
    static DataInputStream dis; //接收数据
    static DataOutputStream dos; //发送数据
    public static int getdata; //用于接收dis
    static NXTConnector conn; //连接
    static byte[] InData={0,0};
   
    private static JFrame f;
    private static JTextField text;
    private static JButton btnW;
    private static JTextField txta;
  
    public static void main(String[] args) throws InterruptedException
    {         
        f = new JFrame("控制NXT");
        f.getContentPane().setLayout(null);
        text  = new JTextField();
        text.setBounds(135, 272, 193, 21);
        f.getContentPane().add(text);
        f.setSize(379,335);
        f.setVisible(true);
        text.setText("\u8D85\u58F0\u6CE2\u4F20\u611F\u5668");
     
        JButton btnConnect = new JButton("连接");
        btnConnect.addActionListener(new ActionListener()
        {        
            public void actionPerformed(ActionEvent e)
            {   
                conn = new NXTConnector(); //创建连接
                connected = conn.connectTo("usb://"); //连接方式使用USB
                Runnable runnable = new Runnable()  //实现Runnable()用于获取传感器数据线程            
                {   
                    @Override
                    public void run()
                    {                           
                        while(true) //循环用于检测是否能连接上                           
                        {   
                            if (!connected)
                            {
                                System.err.println("连接NXT失败"); //判断是否连接
                                System.exit(1);  //直接退出程序
                            }
                           
                            dis = conn.getDataIn(); //取值数据类型为INT
                            while(true) //循环接收数据
                            {                                    
                                try{
                                    dis.read(InData, 0,2);//接收数组变量                                                            
                                    text.setText( "超声波传感器距离: "+InData[0]); //接收回来的值赋给text        
                                    txta.setText( "马达A的速度: "+InData[1]);
                                }
                                catch (IOException ioe) {break;}//出错的时候终止循环
                                }
                            }
                        }                     
                 };            
                Thread thread = new Thread(runnable); //创建线程
                thread.start();  //开启线程
            }
        });
     
        btnConnect.setBounds(10, 240, 93, 23);
        f.getContentPane().add(btnConnect);
         
        btnW = new JButton("\u524DW");
        btnW.setFont(new Font("宋体", Font.PLAIN, 10));
        btnW.addMouseListener(new MouseAdapter()
        {
            @Override
             public void mousePressed(MouseEvent e)
            {
                 dos=conn.getDataOut(); //输出数据                 
                try
                {
                    dos.writeInt(1);  //写数据类型
                    dos.flush();    //刷出数据到NXT
                }
                catch (IOException e1) {System.out.println("\n IO Exception writeInt");}   
             }
             @Override
             public void mouseReleased(MouseEvent e)
             {
                 dos=conn.getDataOut();                 
                try
                {
                    dos.writeInt(5);
                    dos.flush();
                }
                catch (IOException e1) {System.out.println("\n IO Exception writeInt");}   
             }
        });         
        btnW.setBounds(64, 10, 55, 55);
        f.getContentPane().add(btnW);
        
        JButton btnS = new JButton("后S");
        btnS.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                dos=conn.getDataOut(); //输出数据                 
                try
                {
                    dos.writeInt(2);  //写数据类型
                    dos.flush();    //刷出数据到NXT
                }
                catch (IOException e1) {System.out.println("\n IO Exception writeInt");}   
            }
            @Override
            public void mouseReleased(MouseEvent e) {
                dos=conn.getDataOut(); //输出数据                 
                try
                {
                    dos.writeInt(5);  //写数据类型
                    dos.flush();    //刷出数据到NXT
                }
                catch (IOException e1) {System.out.println("\n IO Exception writeInt");}   
            }
        });
        btnS.setFont(new Font("宋体", Font.PLAIN, 10));
        btnS.setBounds(64, 64, 55, 55);
        f.getContentPane().add(btnS);
        
        JButton btnA =     new JButton("左A");
        btnA.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                dos=conn.getDataOut(); //输出数据                 
                try
                {
                    dos.writeInt(4);  //写数据类型
                    dos.flush();    //刷出数据到NXT
                }
                catch (IOException e1) {System.out.println("\n IO Exception writeInt");}   
            }
            @Override
            public void mouseReleased(MouseEvent e) {
                dos=conn.getDataOut(); //输出数据                 
                try
                {
                    dos.writeInt(5);  //写数据类型
                    dos.flush();    //刷出数据到NXT
                }
                catch (IOException e1) {System.out.println("\n IO Exception writeInt");}
            }
        });
        btnA.setFont(new Font("宋体", Font.PLAIN, 10));
        btnA.setBounds(10, 64, 55, 55);
        f.getContentPane().add(btnA);
        
        JButton btnD = new JButton("右D");
        btnD.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                dos=conn.getDataOut(); //输出数据                 
                try
                {
                    dos.writeInt(3);  //写数据类型
                    dos.flush();    //刷出数据到NXT
                }
                catch (IOException e1) {System.out.println("\n IO Exception writeInt");}
            }
            @Override
            public void mouseReleased(MouseEvent e) {
                dos=conn.getDataOut(); //输出数据                 
                try
                {
                    dos.writeInt(5);  //写数据类型
                    dos.flush();    //刷出数据到NXT
                }
                catch (IOException e1) {System.out.println("\n IO Exception writeInt");}
            }
        });
        btnD.setFont(new Font("宋体", Font.PLAIN, 10));
        btnD.setBounds(118, 64, 55, 55);
        f.getContentPane().add(btnD);
        
        JButton btnJ = new JButton("\u52A0J");
        btnJ.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dos=conn.getDataOut(); //输出数据                 
                try
                {
                    dos.writeInt(6);  //写数据类型
                    dos.flush();    //刷出数据到NXT
                }
                catch (IOException e1) {System.out.println("\n IO Exception writeInt");}
            }
        });
        btnJ.setFont(new Font("宋体", Font.PLAIN, 10));
        btnJ.setBounds(218, 10, 55, 55);
        f.getContentPane().add(btnJ);
        
        JButton btnK = new JButton("\u51CFK");
        btnK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dos=conn.getDataOut(); //输出数据                 
                try
                {
                    dos.writeInt(7);  //写数据类型
                    dos.flush();    //刷出数据到NXT
                }
                catch (IOException e1) {System.out.println("\n IO Exception writeInt");}
            }
        });
        btnK.setFont(new Font("宋体", Font.PLAIN, 10));
        btnK.setBounds(218, 64, 55, 55);
        f.getContentPane().add(btnK);
        
        txta = new JTextField();
        txta.setText("\u9A6C\u8FBEA\u901F\u5EA6");
        txta.setBounds(135, 241, 193, 21);
        f.getContentPane().add(txta);
        txta.setColumns(10);
        
        JButton btnClose = new JButton("\u9000\u51FA");
        btnClose.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(1);
            }
        });
        btnClose.setBounds(10, 271, 93, 23);
        f.getContentPane().add(btnClose);
    }
}

如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
 楼主| 发表于 2012-6-4 22:08:25 | 显示全部楼层
需要蓝牙就自己修改连接方式
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

头像被屏蔽
发表于 2012-12-5 14:32:31 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2013-6-3 21:15:56 | 显示全部楼层
楼主你好,我目前也在研究lejos的pc控制问题,请问lejos.pc.comm.*这个包在哪里呀,我看了lejos的api没有发现这个包啊,另外如何将lejos的程序在pc端上运行呢,被这个困惑了很久了,希望楼主能够看到啊
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

手机版|中文乐高 ( 桂ICP备13001575号-7 )

GMT+8, 2025-4-30 17:53 , Processed in 0.618660 second(s), 19 queries .

Powered by Discuz! X3.5

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表