Download presentation
Presentation is loading. Please wait.
Published byMeryl Wilkinson Modified 6年之前
1
Java and LEGO Robots 齐琨(Kun Qi) GC Senior Sales Consultant
2
拥有一个机器人是每个孩子的梦想
3
梦想可以成真
4
Program Agenda LEGO(乐高) NXT leJOS Java控制NXT机器人 演示
5
Lego NXT是什么? 可编程组合机器人玩具 硬件 编程 ARM7(32位) AT91SAM7S256(48Mhz)
256K的闪存,64K的内存 蓝牙、USB2.0 4个输入、3个输出 64*100像素单色LCD 编程 NXT-G/LeJOS/NXC/RobotC/…
6
LEGO设计工具 LEGO Digital Designer
7
LEGO编程语言 NXT-G
8
To fill a shape with an image.
Use existing picture box, DO NOT delete and create new picture box. Right click on the shape. At the bottom of the submenu select “Format Shape” Select “Fill” at the top of the “Format Shape” dialog box. Select “Picture or Texture fill” from the options. And select “File” under the “Insert from” option. Navigate to the file you want to use and select “Insert” 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 DELETE THIS INSTRUCTION NOTE WHEN NOT IN USE
9
leJOS: 运行在LEGO上的Java 操作系统
是一个微型的Java VM,替代LEGO操作系统 最早来源于TinyVM项目,一个基于Java的用于Lego Mindstorm RCX固件 2000年移到到leJOS项目 使用C和ARM的汇编指令编写 一次只能运行一个Java程序 leJOS包括两个项目 leJOS NXJ leJOS RCX
10
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通讯协议)
11
为什么要用leJOS? 使用行业标准的Java语言 支持面向对象编程 开源项目,有很多贡献者
使用专业的IDE,Eclipse/Netbean 跨平台支持,Windows/Linux/Mac 比NXT-G更快(主要指开发效率) 全面支持蓝牙、USB、 I²C和RS485协议 提供精确的马达控制 支持Java 7语言特性
12
为什么要用leJOS? 支持高级导航设备 支持蒙特卡罗定位 支持卡尔曼滤波 提供行为类来支持复杂机器人行为 支持大量第三方传感器
支持通过蓝牙或USB的远程事件记录 支持PC远程控制和跟踪NXJ程序 提供了浮点运算等数学运算函数
13
为什么要用leJOS? 支持java.io访问flash文件系统 支持多线程 支持监听和事件 支持gc安全内存管理 支持8位WAV文件播放
提供了大量的样例 支持NXT到NXT的通讯 支持GPS设备 ……
14
准备leJOS环境 To fill a shape with an image.
Use existing picture box, DO NOT delete and create new picture box. Right click on the shape. At the bottom of the submenu select “Format Shape” Select “Fill” at the top of the “Format Shape” dialog box. Select “Picture or Texture fill” from the options. And select “File” under the “Insert from” option. Navigate to the file you want to use and select “Insert” 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 DELETE THIS INSTRUCTION NOTE WHEN NOT IN USE 准备leJOS环境
15
环境准备—Windows 获取介质 JDK 5/6/7 USB驱动 蓝牙模块(可选) 要使用32bit的
USB驱动 最新的Fantom Driver 1.1.3 Win7专业版+(或setup.ini->AllowWindowsStarter=1) 蓝牙模块(可选) 蓝牙设备及驱动
16
leJOS NXJ软件 0.9.1 beta版 leJOS_NXJ_0.9.1beta-3_win32_setup.exe (21.9 MB) IDE Eclipse/Netbean 必须使用32bit
17
安装leJOS 安装步骤
18
建立NXT的Java运行环境 写入leJOS到LEGO NXT NXT刷固件,将NXJ固件使用leJOS的Flash工具写入到NXT中
环境变量 NXJ_HOME LEJOS_NXT_JAVA_HOME PATH=%NXJ%\bin;%PATH%
19
选择IDE,连接你的NXT 以Eclipse为例 安装新软件
lejos -
20
第一个程序 Hello,World 与新建类的方式相同 屏幕输出的两种方式 LCD类 System.out.println
21
编译和上传程序 Eclipse方式 运行->LeJOS NXT Program 插件完成Link、上传和运行的工作
22
编译和上传程序 手工方式 nxjc.bat,使用javac编译源码,生成class文件 nxjlink.bat,生成LE格式的nxj文件
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
23
leJOS的按键 按键 名称 说明 回车/开机 菜单选择 左 右 退出/关机 当位于顶层菜单时,关机
24
leJOS的菜单
25
控制轮式小车 To fill a shape with an image.
Use existing picture box, DO NOT delete and create new picture box. Right click on the shape. At the bottom of the submenu select “Format Shape” Select “Fill” at the top of the “Format Shape” dialog box. Select “Picture or Texture fill” from the options. And select “File” under the “Insert from” option. Navigate to the file you want to use and select “Insert” 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 DELETE THIS INSTRUCTION NOTE WHEN NOT IN USE 控制轮式小车
26
控制伺服电机 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) …… Motor类 Motor类是对NXT 马达的抽象 马达需要连接A/D 端口 A端口 B端口 C端口
27
轮式小车 DifferentialPilot类 控制两轮驱动的小车高层类,每个轮子都有自己的马达 两个轮子的连接端口 轮子的直径
两个轮子间的距离
28
演示—两轮小车运行 To fill a shape with an image.
Use existing picture box, DO NOT delete and create new picture box. Right click on the shape. At the bottom of the submenu select “Format Shape” Select “Fill” at the top of the “Format Shape” dialog box. Select “Picture or Texture fill” from the options. And select “File” under the “Insert from” option. Navigate to the file you want to use and select “Insert” 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 DELETE THIS INSTRUCTION NOTE WHEN NOT IN USE 演示—两轮小车运行
29
I/O和传感器 LCD、按键、声音、电池、光线传感器、声音传感器
30
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()
31
I/O 声音和电池 播放声音lejos.nxj.Sound 电池lejos.nxj.Battery
playTone(int aFrequency, int aDuration),播放音频 systemSound (boolean aQueued, int aCode),播放系统声音 playSample(File aWAVfile),播放8bit的wav文件 电池lejos.nxj.Battery getVoltage(),获取电压
32
传感器 光线传感器和声音传感器 NXT提供了四个传感器,光线传感器、声音传感器、超声传感器和触碰 传感器
光线传感器,需要指定端口LightSensor(SensorPort port) 声音传感器,接收不同的频率响应SoundSensor(SensorPort.S1)
33
物体探测 超声传感器和触碰传感器 机器人探测路上的对象,做出特定行为 <<interface>>
FeatureDetector FeatureDetectorAdapter RangeFeatureDetectector TouchFeatureDetectector
34
物体探测 超声传感器 自动检测和数据报告 通过监听器接口分离“活动—响应”代码 一个代码段响应多个传感器
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()); }
35
物体探测 超声传感器和触碰传感器组合 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);
36
NXJ支持的第三方硬件
37
演示—物体探测和声音播放 To fill a shape with an image.
Use existing picture box, DO NOT delete and create new picture box. Right click on the shape. At the bottom of the submenu select “Format Shape” Select “Fill” at the top of the “Format Shape” dialog box. Select “Picture or Texture fill” from the options. And select “File” under the “Insert from” option. Navigate to the file you want to use and select “Insert” 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 DELETE THIS INSTRUCTION NOTE WHEN NOT IN USE 演示—物体探测和声音播放
38
线程 leJOS在下面的情况下会单独开线程 如果监听在使用,那么按钮和传感器端口会开始一个监听线程 每个电机都会开始一个调节器线程
蓝牙类会开始一个可以与分开的蓝牙芯片对话的线程 每个定时器会打开一个定时器线程 Class Indicators extends Thread{ … … } … Indicators id = new Indicators(); id.setDaemon(true); id.start();
39
监听 leJOS实现了监听器线程来侦听特定事件 按键侦听,无论程序在做什么,发生按键事件时都会被检测到 按键侦听 传感器端口侦听
特定按键的侦听,需要注册单独的事件 addButtonListener
40
错误处理和调试 异常 数据终止 远程调试 支持绝大多数的Java标准异常 也可以自定义 NXJ崩溃,屏幕会显示指针和错误信息
PC使用Rconsole,USB或蓝牙 RConsole.println()
41
几个工具 NXJ File Browser,查看和操作NXJ上的文件 NXJ FlashG NXJ Monitor,监控线程运行情况
42
leJOS和Android To fill a shape with an image.
Use existing picture box, DO NOT delete and create new picture box. Right click on the shape. At the bottom of the submenu select “Format Shape” Select “Fill” at the top of the “Format Shape” dialog box. Select “Picture or Texture fill” from the options. And select “File” under the “Insert from” option. Navigate to the file you want to use and select “Insert” 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 DELETE THIS INSTRUCTION NOTE WHEN NOT IN USE leJOS和Android
43
leJOS和Android 下载Android SDK 安装Eclipse插件
安装Eclipse插件 配置LeJOS-Droid工程,确保pccomms.jar在build path中 样例工程 TachoCount,获取电机转数 BTSend,蓝牙发送 RCNavigationControl,蓝牙控制小车行动
44
演示—两轮驱动小车 To fill a shape with an image.
Use existing picture box, DO NOT delete and create new picture box. Right click on the shape. At the bottom of the submenu select “Format Shape” Select “Fill” at the top of the “Format Shape” dialog box. Select “Picture or Texture fill” from the options. And select “File” under the “Insert from” option. Navigate to the file you want to use and select “Insert” 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 DELETE THIS INSTRUCTION NOTE WHEN NOT IN USE 演示—两轮驱动小车
45
Graphic Section Divider
Similar presentations