Transcript leJOS

Java and LEGO Robots
齐琨(Kun Qi)
GC Senior Sales Consultant
[email protected]
拥有一个机器人是每个孩子的梦想
2
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
梦想可以成真
3
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Program Agenda
 LEGO(乐高) NXT
 leJOS
 Java控制NXT机器人
 演示
4
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Lego NXT是什么?
可编程组合机器人玩具
 硬件
– ARM7(32位) AT91SAM7S256(48Mhz)
– 256K的闪存,64K的内存
– 蓝牙、USB2.0
– 4个输入、3个输出
– 64*100像素单色LCD
 编程
– NXT-G/LeJOS/NXC/RobotC/…
5
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
LEGO设计工具
LEGO Digital Designer
6
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
LEGO编程语言
NXT-G
7
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
8
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
leJOS: 运行在LEGO上的Java 操作系统
http://lejos.sourceforge.net
 是一个微型的Java VM,替代LEGO操作系统
– 最早来源于TinyVM项目,一个基于Java的用于Lego Mindstorm RCX固件
– 2000年移到到leJOS项目
– 使用C和ARM的汇编指令编写
– 一次只能运行一个Java程序
 leJOS包括两个项目
– leJOS NXJ
– leJOS RCX
9
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
leJOS NXJ
LEGO NXT的Java环境
 JavaVM的NXT固件,替换原有NXT的LEGO固件
 Java 类库,classes.jar
– 实现NXJ API
– 提供替代的Java Runtime (java.*)
 工具
– 链接工具,链接java类和classes.jar为二进制文件格式,上传至NXT运行
– PC工具,烧录固件到NXT中,上传程序,调试工具等功能
 PC API,开发与NXJ通讯的程序,在蓝牙和USB连接上使用Java
Stream,或使用LCP(LEGO通讯协议)
10
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
为什么要用leJOS?
 使用行业标准的Java语言
 支持面向对象编程
 开源项目,有很多贡献者
 使用专业的IDE,Eclipse/Netbean
 跨平台支持,Windows/Linux/Mac
 比NXT-G更快(主要指开发效率)
 全面支持蓝牙、USB、 I²C和RS485协议
 提供精确的马达控制
 支持Java 7语言特性
11
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
为什么要用leJOS?
 支持高级导航设备
 支持蒙特卡罗定位
 支持卡尔曼滤波
 提供行为类来支持复杂机器人行为
 支持大量第三方传感器
 支持通过蓝牙或USB的远程事件记录
 支持PC远程控制和跟踪NXJ程序
 提供了浮点运算等数学运算函数
12
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
为什么要用leJOS?
 支持java.io访问flash文件系统
 支持多线程
 支持监听和事件
 支持gc安全内存管理
 支持8位WAV文件播放
 提供了大量的样例
 支持NXT到NXT的通讯
 支持GPS设备
 ……
13
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
准备leJOS环境
14
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
环境准备—Windows
获取介质
 JDK 5/6/7
– 要使用32bit的
– http://www.oracle.com/technetwork/java/javase/downloads/
 USB驱动
– 最新的Fantom Driver 1.1.3
– http://mindstorms.lego.com/en-us/support/files/Driver.aspx
– Win7专业版+(或setup.ini->AllowWindowsStarter=1)
 蓝牙模块(可选)
– 蓝牙设备及驱动
15
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
 leJOS NXJ软件
– 0.9.1 beta版
– http://lejos.sourceforge.net/nxj-downloads.php
– leJOS_NXJ_0.9.1beta-3_win32_setup.exe (21.9 MB)
 IDE
– Eclipse/Netbean
– 必须使用32bit
16
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
安装leJOS
安装步骤
17
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
建立NXT的Java运行环境
写入leJOS到LEGO NXT
 NXT刷固件,将NXJ固件使用leJOS的Flash工具写入到NXT中
– 如果刷机失败,可以从LEGO官网中找到固件刷回,NXT不会变砖
 环境变量
– NXJ_HOME
– LEJOS_NXT_JAVA_HOME
– PATH=%NXJ%\bin;%PATH%
18
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
选择IDE,连接你的NXT
以Eclipse为例
 安装新软件
– lejos - http://lejos.sourceforge.net/tools/eclipse/plugin/nxj/
19
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
第一个程序
Hello,World
 与新建类的方式相同
 屏幕输出的两种方式
– LCD类
– System.out.println
20
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
编译和上传程序
Eclipse方式
 运行->LeJOS NXT Program
 插件完成Link、上传和运行的工作
21
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
编译和上传程序
手工方式
 nxjc.bat,使用javac编译源码,生成class文件
– nxjc HelloWorld.java
 nxjlink.bat,生成LE格式的nxj文件
– nxjlink –o HelloWorld.nxj HelloWorld
 nxjupload.bat,上传nxj文件到NXT,执行程序 (–r参数)
– nxjupload –r HelloWorld.nxj
 nxj.bat,集成link和上传功能
– nxj –r –o HelloWorld.nxj HelloWorld
22
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
leJOS的按键
按键
名称
回车/开机
说明
菜单选择
左
右
退出/关机
23
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
当位于顶层菜单时,
关机
leJOS的菜单
24
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
控制轮式小车
25
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
控制伺服电机
Motor类
类
 Motor类是对NXT
马达的抽象
 马达需要连接A/D
端口
– A端口
– B端口
– C端口
Motor.A
方法
forward()
向前转动
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,则方法立
即返回
……
26
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
说明
轮式小车
DifferentialPilot类
 控制两轮驱动的小车高层类,每个轮子都有自己的马达
– 两个轮子的连接端口
– 轮子的直径
– 两个轮子间的距离
27
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
演示—两轮小车运行
28
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
I/O和传感器
LCD、按键、声音、电池、光线传感器、声音传感器
29
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
I/O
LCD和按键
 LCD,显示8行16列,图片模式,100×64像素
– 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()
30
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
I/O
声音和电池
 播放声音lejos.nxj.Sound
– playTone(int aFrequency, int aDuration),播放音频
– systemSound (boolean aQueued, int aCode),播放系统声音
– playSample(File aWAVfile),播放8bit的wav文件
 电池lejos.nxj.Battery
– getVoltage(),获取电压
31
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
传感器
光线传感器和声音传感器
 NXT提供了四个传感器,光线传感器、声音传感器、超声传感器和触碰
传感器
 光线传感器,需要指定端口LightSensor(SensorPort port)
 声音传感器,接收不同的频率响应SoundSensor(SensorPort.S1)
32
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
物体探测
超声传感器和触碰传感器
 机器人探测路上的对象,做出特定行为
<<interface>>
FeatureDetector
FeatureDetectorAdapter
RangeFeatureDetectector
33
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
TouchFeatureDetectector
物体探测
超声传感器
 自动检测和数据报告
 通过监听器接口分离“活动—响应”代码
 一个代码段响应多个传感器
int MAX_DISTANCE = 50; // 最大探测距离,单位为厘米
int PERIOD = 500; // 探测周期,单位为毫秒
UltrasonicSensor us = new UltrasonicSensor(SensorPort.S4);
FeatureDetector fd = new RangeFeatureDetector(us, MAX_DISTANCE, PERIOD);
Feature result = fd.scan();
if(result != null) {
System.out.println("Range: " + result.getRangeReading().getRange());
}
34
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
物体探测
超声传感器和触碰传感器组合
UltrasonicSensor us = new UltrasonicSensor(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 = 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);
35
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
NXJ支持的第三方硬件
36
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
演示—物体探测和声音播放
37
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
线程
 leJOS在下面的情况下会单独开线程
– 如果监听在使用,那么按钮和传感器端口会开始一个监听线程
– 每个电机都会开始一个调节器线程
– 蓝牙类会开始一个可以与分开的蓝牙芯片对话的线程
– 每个定时器会打开一个定时器线程
Class Indicators extends Thread{
……
}
…
Indicators id = new Indicators();
id.setDaemon(true);
id.start();
38
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
监听
 leJOS实现了监听器线程来侦听特定事件
– 按键侦听
– 传感器端口侦听
 按键侦听,无论程序在做什么,发生按键事件时都会被检测到
– 特定按键的侦听,需要注册单独的事件 addButtonListener
39
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
错误处理和调试
 异常
– 支持绝大多数的Java标准异常
– 也可以自定义
 数据终止
– NXJ崩溃,屏幕会显示指针和错误信息
– 有问题的nxj文件
 远程调试
– PC使用Rconsole,USB或蓝牙
– RConsole.println()
40
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
几个工具
 NXJ File Browser,查看和操作NXJ上的文件
 NXJ FlashG
 NXJ Monitor,监控线程运行情况
41
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
leJOS和Android
42
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
leJOS和Android
 下载Android SDK
– http://developer.android.com/sdk/index.html
 安装Eclipse插件
– http://developer.android.com/sdk/eclipse-adt.html
 配置LeJOS-Droid工程,确保pccomms.jar在build path中
 样例工程
– TachoCount,获取电机转数
– BTSend,蓝牙发送
– RCNavigationControl,蓝牙控制小车行动
43
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
演示—两轮驱动小车
44
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
Graphic Section Divider
45
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.
46
Copyright © 2013, Oracle and/or its affiliates. All rights reserved.