1、Java and LEGO Robots齐琨(Kun Qi)GC Senior Sales CCopyright 2013, Oracle and/or its affiliates. All rights reserved.2拥有一个机器人是每个孩子的梦想拥有一个机器人是每个孩子的梦想Copyright 2013, Oracle and/or its affiliates. All rights reserved.3梦想可以成真梦想可以成真Copyright 2013, Oracle and/or its affiliates. All rights reserved.4Program Ag
2、endaLEGO(乐高) NXTleJOSJava控制NXT机器人演示Copyright 2013, Oracle and/or its affiliates. All rights reserved.5Lego NXT是什么?是什么?硬件ARM7(32位) AT91SAM7S256(48Mhz)256K的闪存,64K的内存蓝牙、USB2.04个输入、3个输出64*100像素单色LCD编程NXT-G/LeJOS/NXC/RobotC/可编程组合机器人玩具Copyright 2013, Oracle and/or its affiliates. All rights reserved.6LEGO
3、设计工具设计工具LEGO Digital DesignerCopyright 2013, Oracle and/or its affiliates. All rights reserved.7LEGO编程语言编程语言NXT-GCopyright 2013, Oracle and/or its affiliates. All rights reserved.8Copyright 2013, Oracle and/or its affiliates. All rights reserved.9是一个微型的Java VM,替代LEGO操作系统最早来源于TinyVM项目,一个基于Java的用于Lego
4、 Mindstorm RCX固件2000年移到到leJOS项目使用C和ARM的汇编指令编写一次只能运行一个Java程序leJOS包括两个项目leJOS NXJleJOS RCXleJOS: 运行在运行在LEGO上的上的Java 操作系统操作系统http:/Copyright 2013, Oracle and/or its affiliates. All rights reserved.10leJOS NXJJavaVM的NXT固件,替换原有NXT的LEGO固件Java 类库,classes.jar实现NXJ API提供替代的Java Runtime (java.*)工具链接工具,链接java类
5、和classes.jar为二进制文件格式,上传至NXT运行PC工具,烧录固件到NXT中,上传程序,调试工具等功能PC API,开发与NXJ通讯的程序,在蓝牙和USB连接上使用Java Stream,或使用LCP(LEGO通讯协议)LEGO NXT的Java环境Copyright 2013, Oracle and/or its affiliates. All rights reserved.11为什么要用为什么要用leJOS?使用行业标准的Java语言支持面向对象编程开源项目,有很多贡献者使用专业的IDE,Eclipse/Netbean跨平台支持,Windows/Linux/Mac比NXT-G更
6、快(主要指开发效率)全面支持蓝牙、USB、 IC和RS485协议提供精确的马达控制支持Java 7语言特性Copyright 2013, Oracle and/or its affiliates. All rights reserved.12为什么要用为什么要用leJOS?支持高级导航设备支持蒙特卡罗定位支持卡尔曼滤波提供行为类来支持复杂机器人行为支持大量第三方传感器支持通过蓝牙或USB的远程事件记录支持PC远程控制和跟踪NXJ程序 提供了浮点运算等数学运算函数Copyright 2013, Oracle and/or its affiliates. All rights reserved.1
7、3为什么要用为什么要用leJOS?支持java.io访问flash文件系统支持多线程支持监听和事件支持gc安全内存管理支持8位WAV文件播放提供了大量的样例支持NXT到NXT的通讯支持GPS设备Copyright 2013, Oracle and/or its affiliates. All rights reserved.14准备准备leJOS环境环境Copyright 2013, Oracle and/or its affiliates. All rights reserved.15环境准备环境准备WindowsJDK 5/6/7要使用32bit的http:/ Driver 1.1.3ht
8、tp:/ 2013, Oracle and/or its affiliates. All rights reserved.16leJOS NXJ软件0.9.1 beta版http:/ (21.9 MB)IDEEclipse/Netbean必须使用32bitCopyright 2013, Oracle and/or its affiliates. All rights reserved.17安装安装leJOS安装步骤Copyright 2013, Oracle and/or its affiliates. All rights reserved.18建立建立NXT的的Java运行环境运行环境NX
9、T刷固件,将NXJ固件使用leJOS的Flash工具写入到NXT中如果刷机失败,可以从LEGO官网中找到固件刷回,NXT不会变砖环境变量NXJ_HOMELEJOS_NXT_JAVA_HOMEPATH=%NXJ%bin;%PATH%写入leJOS到LEGO NXTCopyright 2013, Oracle and/or its affiliates. All rights reserved.19选择选择IDE,连接你的,连接你的NXT安装新软件lejos - http:/ 2013, Oracle and/or its affiliates. All rights reserved.20第一个
10、程序第一个程序与新建类的方式相同屏幕输出的两种方式LCD类System.out.printlnHello,WorldCopyright 2013, Oracle and/or its affiliates. All rights reserved.21编译和上传程序编译和上传程序运行-LeJOS NXT Program插件完成Link、上传和运行的工作Eclipse方式Copyright 2013, Oracle and/or its affiliates. All rights reserved.22编译和上传程序编译和上传程序nxjc.bat,使用javac编译源码,生成class文件nx
11、jc HelloWorld.javanxjlink.bat,生成LE格式的nxj文件nxjlink o HelloWorld.nxj HelloWorldnxjupload.bat,上传nxj文件到NXT,执行程序 (r参数)nxjupload r HelloWorld.nxjnxj.bat,集成link和上传功能nxj r o HelloWorld.nxj HelloWorld手工方式Copyright 2013, Oracle and/or its affiliates. All rights reserved.23按键按键名称名称说明说明回车/开机菜单选择左右退出/关机当位于顶层菜单时,
12、关机leJOS的按键的按键Copyright 2013, Oracle and/or its affiliates. All rights reserved.24leJOS的菜单的菜单Copyright 2013, Oracle and/or its affiliates. All rights reserved.25控制轮式小车控制轮式小车Copyright 2013, Oracle and/or its affiliates. All rights reserved.26控制伺服电机控制伺服电机Motor类是对NXT马达的抽象马达需要连接A/D端口A端口B端口C端口Motor类类类方法方法
13、说明说明Motor.Aforward()向前转动backward()向后转动stop()快速停止getTachoCount()返回马达角度setSpeed(int speed)设置每秒的角度值rorate(int angle)旋转angle角度rorateTo(int angle)旋转到angle角度rorate(int angle,boolean immediateReturn)如果是true,则方法立即返回rorateTo(int angle,boolean immediateReturn)如果是true,则方法立即返回Copyright 2013, Oracle and/or its a
14、ffiliates. All rights reserved.27轮式小车轮式小车控制两轮驱动的小车高层类,每个轮子都有自己的马达两个轮子的连接端口轮子的直径两个轮子间的距离DifferentialPilot类Copyright 2013, Oracle and/or its affiliates. All rights reserved.28演示演示两轮小车运行两轮小车运行Copyright 2013, Oracle and/or its affiliates. All rights reserved.29I/O和传感器和传感器LCD、按键、声音、电池、光线传感器、声音传感器Copyrigh
15、t 2013, Oracle and/or its affiliates. All rights reserved.30I/OLCD,显示8行16列,图片模式,10064像素LCD.drawString(String str, int x, int y)LCD.drawInt(int i, int x, int y)LCD.drawInt(int i, int places, int x, int y)LCD.clear()按键实例Button.ENTER(1)Button.LEFT(2)Button.RIGHT(4)Button.ESCAPE(8)Button.isDown()LCD和按键C
16、opyright 2013, Oracle and/or its affiliates. All rights reserved.31I/O播放声音lejos.nxj.SoundplayTone(int aFrequency, int aDuration),播放音频systemSound (boolean aQueued, int aCode),播放系统声音playSample(File aWAVfile),播放8bit的wav文件电池lejos.nxj.BatterygetVoltage(),获取电压声音和电池Copyright 2013, Oracle and/or its affilia
17、tes. All rights reserved.32传感器传感器NXT提供了四个传感器,光线传感器、声音传感器、超声传感器和触碰传感器光线传感器,需要指定端口LightSensor(SensorPort port)声音传感器,接收不同的频率响应SoundSensor(SensorPort.S1)光线传感器和声音传感器Copyright 2013, Oracle and/or its affiliates. All rights reserved.33物体探测物体探测机器人探测路上的对象,做出特定行为超声传感器和触碰传感器FeatureDetectorAdapterRangeFeatureDe
18、tectectorTouchFeatureDetectectorFeatureDetectorCopyright 2013, Oracle and/or its affiliates. All rights reserved.34物体探测物体探测自动检测和数据报告通过监听器接口分离“活动响应”代码一个代码段响应多个传感器超声传感器int MAX_DISTANCE = 50; / 最大探测距离,单位为厘米int PERIOD = 500; / 探测周期,单位为毫秒UltrasonicSensor us = new UltrasonicSensor(SensorPort.S4); FeatureD
19、etector fd = new RangeFeatureDetector(us, MAX_DISTANCE, PERIOD);Feature result = fd.scan(); if(result != null) System.out.println(Range: + result.getRangeReading().getRange();Copyright 2013, Oracle and/or its affiliates. All rights reserved.35物体探测物体探测超声传感器和触碰传感器组合UltrasonicSensor us = new Ultrasonic
20、Sensor(SensorPort.S4); / 端口4连接超声传感器FeatureDetector detector1 = new RangeFeatureDetector(us, MAX_DETECT,RANGE_READING_DELAY); / 端口2连接左触控传感器Touch leftBump = new TouchSensor(SensorPort.S2); FeatureDetector detector2 = new TouchFeatureDetector(leftBump, 10, TOUCH_Y_OFFSET); / 端口3连接右触控传感器Touch rightBump
21、= new TouchSensor(SensorPort.S3); FeatureDetector detector3 = new TouchFeatureDetector(rightBump, -10, TOUCH_Y_OFFSET); FusorDetector fusion = new FusorDetector(); fusion.addDetector(detector1); fusion.addDetector(detector2); fusion.addDetector(detector3); fusion.addListener(myFeatureListener);Copyr
22、ight 2013, Oracle and/or its affiliates. All rights reserved.36NXJ支持的第三方硬件支持的第三方硬件Copyright 2013, Oracle and/or its affiliates. All rights reserved.37演示演示物体探测和声音播放物体探测和声音播放Copyright 2013, Oracle and/or its affiliates. All rights reserved.38线程线程leJOS在下面的情况下会单独开线程如果监听在使用,那么按钮和传感器端口会开始一个监听线程每个电机都会开始一个调
23、节器线程蓝牙类会开始一个可以与分开的蓝牙芯片对话的线程每个定时器会打开一个定时器线程Class Indicators extends Thread Indicators id = new Indicators();id.setDaemon(true);id.start();Copyright 2013, Oracle and/or its affiliates. All rights reserved.39监听监听leJOS实现了监听器线程来侦听特定事件按键侦听传感器端口侦听按键侦听,无论程序在做什么,发生按键事件时都会被检测到特定按键的侦听,需要注册单独的事件 addButtonListen
24、erCopyright 2013, Oracle and/or its affiliates. All rights reserved.40错误处理和调试错误处理和调试异常支持绝大多数的Java标准异常也可以自定义数据终止NXJ崩溃,屏幕会显示指针和错误信息有问题的nxj文件远程调试PC使用Rconsole,USB或蓝牙RConsole.println()Copyright 2013, Oracle and/or its affiliates. All rights reserved.41几个工具几个工具NXJ File Browser,查看和操作NXJ上的文件NXJ FlashGNXJ Mo
25、nitor,监控线程运行情况Copyright 2013, Oracle and/or its affiliates. All rights reserved.42leJOS和和AndroidCopyright 2013, Oracle and/or its affiliates. All rights reserved.43leJOS和和Android下载Android SDKhttp:/ path中样例工程TachoCount,获取电机转数BTSend,蓝牙发送RCNavigationControl,蓝牙控制小车行动Copyright 2013, Oracle and/or its affiliates. All rights reserved.44演示演示两轮驱动小车两轮驱动小车Copyright 2013, Oracle and/or its affiliates. All rights reserved.45Graphic Section DividerCopyright 2013, Oracle and/or its affiliates. All rights reserved.46
侵权处理QQ:3464097650--上传资料QQ:3464097650
【声明】本站为“文档C2C交易模式”,即用户上传的文档直接卖给(下载)用户,本站只是网络空间服务平台,本站所有原创文档下载所得归上传人所有,如您发现上传作品侵犯了您的版权,请立刻联系我们并提供证据,我们将在3个工作日内予以改正。