Java and LEGO Robots
齐琨(Kun Qi)
GC Senior Sales Consultant
Copyright © 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. 4
Program Agenda
LEGO(乐高) NXT
leJOS
Java控制NXT机器人
演示
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 5
Lego NXT是什么?
硬件
– ARM7(32位) AT91SAM7S256(48Mhz)
– 256K的闪存,64K的内存
– 蓝牙、
– 4个输入、3个输出
– 64*100像素单色LCD
编程
– NXT-G/LeJOS/NXC/RobotC/…
可编程组合机器人玩具
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 6
LEGO设计工具
LEGO Digital Designer
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 7
LEGO编程语言
NXT-G
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 8
To fill a shape with an image.
1. Use existing picture box, DO NOT delete and
create new picture box.
2. Right click on the shape.
3. At the bottom of the submenu select
―Format Shape‖
4. Select ―Fill‖ at the top of the ―Format Shape‖ dialog
box.
5. Select ―Picture or Texture fill‖ from the options.
6. And select ―File‖ under the ―Insert from‖ option.
7. Navigate to the file you want to use and
select ―Insert‖
8. On the ―Format‖ tab, in the Size group, click on
―Crop to Fill‖ in the Crop tool and drag the image
bounding box to the desired size
9. DELETE THIS INSTRUCTION NOTE WHEN NOT
IN USE
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 9
是一个微型的Java VM,替代LEGO操作系统
– 最早来源于TinyVM项目,一个基于Java的用于Lego Mindstorm RCX固件
– 2000年移到到leJOS项目
– 使用C和ARM的汇编指令编写
– 一次只能运行一个Java程序
leJOS包括两个项目
– leJOS NXJ
– leJOS RCX
leJOS: 运行在LEGO上的Java 操作系统
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 10
leJOS NXJ
JavaVM的NXT固件,替换原有NXT的LEGO固件
Java 类库,
– 实现NXJ API
– 提供替代的Java Runtime (java.*)
工具
– 链接工具,链接java类和为二进制文件格式,上传至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更快(主要指开发效率)
全面支持蓝牙、USB、 I²C和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. 13
为什么要用leJOS?
支持访问flash文件系统
支持多线程
支持监听和事件
支持gc安全内存管理
支持8位WAV文件播放
提供了大量的样例
支持NXT到NXT的通讯
支持GPS设备
……
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 14
准备leJOS环境
To fill a shape with an image.
1. Use existing picture box, DO NOT delete and
create new picture box.
2. Right click on the shape.
3. At the bottom of the submenu select
―Format Shape‖
4. Select ―Fill‖ at the top of the ―Format Shape‖ dialog
box.
5. Select ―Picture or Texture fill‖ from the options.
6. And select ―File‖ under the ―Insert from‖ option.
7. Navigate to the file you want to use and
select ―Insert‖
8. On the ―Format‖ tab, in the Size group, click on
―Crop to Fill‖ in the Crop tool and drag the image
bounding box to the desired size
9. DELETE THIS INSTRUCTION NOTE WHEN NOT
IN USE
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 15
环境准备—Windows
JDK 5/6/7
– 要使用32bit的
–
USB驱动
– 最新的Fantom Driver
–
– Win7专业版+(或->AllowWindowsStarter=1)
蓝牙模块(可选)
– 蓝牙设备及驱动
获取介质
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 16
leJOS NXJ软件
– beta版
–
– ( MB)
IDE
– Eclipse/Netbean
– 必须使用32bit
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 17
安装leJOS
安装步骤
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 18
建立NXT的Java运行环境
NXT刷固件,将NXJ固件使用leJOS的Flash工具写入到NXT中
– 如果刷机失败,可以从LEGO官网中找到固件刷回,NXT不会变砖
环境变量
– NXJ_HOME
– LEJOS_NXT_JAVA_HOME
– PATH=%NXJ%\bin;%PATH%
写入leJOS到LEGO NXT
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 19
选择IDE,连接你的NXT
安装新软件
– lejos -
以Eclipse为例
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 20
第一个程序
与新建类的方式相同
屏幕输出的两种方式
– LCD类
–
Hello,World
Copyright © 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
编译和上传程序
,使用javac编译源码,生成class文件
– nxjc
,生成LE格式的nxj文件
– nxjlink –o HelloWorld
,上传nxj文件到NXT,执行程序 (–r参数)
– nxjupload –r
,集成link和上传功能
– nxj –r –o HelloWorld
手工方式
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 23
按键 名称 说明
回车/开机 菜单选择
左
右
退出/关机 当位于顶层菜单时,
关机
leJOS的按键
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 24
leJOS的菜单
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 25
控制轮式小车
To fill a shape with an image.
1. Use existing picture box, DO NOT delete and
create new picture box.
2. Right click on the shape.
3. At the bottom of the submenu select
―Format Shape‖
4. Select ―Fill‖ at the top of the ―Format Shape‖ dialog
box.
5. Select ―Picture or Texture fill‖ from the options.
6. And select ―File‖ under the ―Insert from‖ option.
7. Navigate to the file you want to use and
select ―Insert‖
8. On the ―Format‖ tab, in the Size group, click on
―Crop to Fill‖ in the Crop tool and drag the image
bounding box to the desired size
9. DELETE THIS INSTRUCTION NOTE WHEN NOT
IN USE
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 26
控制伺服电机
Motor类是对NXT
马达的抽象
马达需要连接A/D
端口
– A端口
– B端口
– C端口
Motor类
类 方法 说明
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,则方法立
即返回
……
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 27
轮式小车
控制两轮驱动的小车高层类,每个轮子都有自己的马达
– 两个轮子的连接端口
– 轮子的直径
– 两个轮子间的距离
DifferentialPilot类
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 28
演示—两轮小车运行
To fill a shape with an image.
1. Use existing picture box, DO NOT delete and
create new picture box.
2. Right click on the shape.
3. At the bottom of the submenu select
―Format Shape‖
4. Select ―Fill‖ at the top of the ―Format Shape‖ dialog
box.
5. Select ―Picture or Texture fill‖ from the options.
6. And select ―File‖ under the ―Insert from‖ option.
7. Navigate to the file you want to use and
select ―Insert‖
8. On the ―Format‖ tab, in the Size group, click on
―Crop to Fill‖ in the Crop tool and drag the image
bounding box to the desired size
9. DELETE THIS INSTRUCTION NOTE WHEN NOT
IN USE
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 29
I/O和传感器
LCD、按键、声音、电池、光线传感器、声音传感器
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 30
I/O
LCD,显示8行16列,图片模式,100×64像素
– (String str, int x, int y)
– (int i, int x, int y)
– (int i, int places, int x, int y)
– ()
按键实例
– (1)
– (2)
– (4)
– (8)
– ()
LCD和按键
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 31
I/O
播放声音
– playTone(int aFrequency, int aDuration),播放音频
– systemSound (boolean aQueued, int aCode),播放系统声音
– playSample(File aWAVfile),播放8bit的wav文件
电池
– getVoltage(),获取电压
声音和电池
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 32
传感器
NXT提供了四个传感器,光线传感器、声音传感器、超声传感器和触碰
传感器
光线传感器,需要指定端口LightSensor(SensorPort port)
声音传感器,接收不同的频率响应SoundSensor()
光线传感器和声音传感器
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 33
物体探测
机器人探测路上的对象,做出特定行为
超声传感器和触碰传感器
FeatureDetectorAdapter
RangeFeatureDetectector TouchFeatureDetectector
<<interface>>
FeatureDetector
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 34
物体探测
自动检测和数据报告
通过监听器接口分离“活动—响应”代码
一个代码段响应多个传感器
超声传感器
int MAX_DISTANCE = 50; // 最大探测距离,单位为厘米
int PERIOD = 500; // 探测周期,单位为毫秒
UltrasonicSensor us = new UltrasonicSensor();
FeatureDetector fd = new RangeFeatureDetector(us, MAX_DISTANCE, PERIOD);
Feature result = ();
if(result != null) {
("Range: " + ().getRange());
}
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 35
物体探测
超声传感器和触碰传感器组合
UltrasonicSensor us = new UltrasonicSensor(); // 端口4连接超声传感器
FeatureDetector detector1 = new RangeFeatureDetector(us,
MAX_DETECT,RANGE_READING_DELAY);
// 端口2连接左触控传感器
Touch leftBump = new TouchSensor();
FeatureDetector detector2 = new TouchFeatureDetector(leftBump, 10, TOUCH_Y_OFFSET);
// 端口3连接右触控传感器
Touch rightBump = new TouchSensor();
FeatureDetector detector3 = new TouchFeatureDetector(rightBump, -10, TOUCH_Y_OFFSET);
FusorDetector fusion = new FusorDetector();
(detector1);
(detector2);
(detector3);
(myFeatureListener);
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 36
NXJ支持的第三方硬件
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 37
演示—物体探测和声音播放
To fill a shape with an image.
1. Use existing picture box, DO NOT delete and
create new picture box.
2. Right click on the shape.
3. At the bottom of the submenu select
―Format Shape‖
4. Select ―Fill‖ at the top of the ―Format Shape‖ dialog
box.
5. Select ―Picture or Texture fill‖ from the options.
6. And select ―File‖ under the ―Insert from‖ option.
7. Navigate to the file you want to use and
select ―Insert‖
8. On the ―Format‖ tab, in the Size group, click on
―Crop to Fill‖ in the Crop tool and drag the image
bounding box to the desired size
9. DELETE THIS INSTRUCTION NOTE WHEN NOT
IN USE
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 38
线程
leJOS在下面的情况下会单独开线程
– 如果监听在使用,那么按钮和传感器端口会开始一个监听线程
– 每个电机都会开始一个调节器线程
– 蓝牙类会开始一个可以与分开的蓝牙芯片对话的线程
– 每个定时器会打开一个定时器线程
Class Indicators extends Thread{
… …
}
…
Indicators id = new Indicators();
(true);
();
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 39
监听
leJOS实现了监听器线程来侦听特定事件
– 按键侦听
– 传感器端口侦听
按键侦听,无论程序在做什么,发生按键事件时都会被检测到
– 特定按键的侦听,需要注册单独的事件 addButtonListener
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 40
错误处理和调试
异常
– 支持绝大多数的Java标准异常
– 也可以自定义
数据终止
– NXJ崩溃,屏幕会显示指针和错误信息
– 有问题的nxj文件
远程调试
– PC使用Rconsole,USB或蓝牙
– ()
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 41
几个工具
NXJ File Browser,查看和操作NXJ上的文件
NXJ FlashG
NXJ Monitor,监控线程运行情况
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 42
leJOS和Android
To fill a shape with an image.
1. Use existing picture box, DO NOT delete and
create new picture box.
2. Right click on the shape.
3. At the bottom of the submenu select
―Format Shape‖
4. Select ―Fill‖ at the top of the ―Format Shape‖ dialog
box.
5. Select ―Picture or Texture fill‖ from the options.
6. And select ―File‖ under the ―Insert from‖ option.
7. Navigate to the file you want to use and
select ―Insert‖
8. On the ―Format‖ tab, in the Size group, click on
―Crop to Fill‖ in the Crop tool and drag the image
bounding box to the desired size
9. DELETE THIS INSTRUCTION NOTE WHEN NOT
IN USE
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 43
leJOS和Android
下载Android SDK
–
安装Eclipse插件
–
配置LeJOS-Droid工程,确保在build path中
样例工程
– TachoCount,获取电机转数
– BTSend,蓝牙发送
– RCNavigationControl,蓝牙控制小车行动
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 44
演示—两轮驱动小车
To fill a shape with an image.
1. Use existing picture box, DO NOT delete and
create new picture box.
2. Right click on the shape.
3. At the bottom of the submenu select
―Format Shape‖
4. Select ―Fill‖ at the top of the ―Format Shape‖ dialog
box.
5. Select ―Picture or Texture fill‖ from the options.
6. And select ―File‖ under the ―Insert from‖ option.
7. Navigate to the file you want to use and
select ―Insert‖
8. On the ―Format‖ tab, in the Size group, click on
―Crop to Fill‖ in the Crop tool and drag the image
bounding box to the desired size
9. DELETE THIS INSTRUCTION NOTE WHEN NOT
IN USE
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 45
Graphic Section Divider
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 46