




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、NXT端的程序下载(源代码) import java.io.IOException;import lejos.nxt.*;import m.*;import java.io.*;import javax.bluetooth.*;/ Author: 动力老男孩/ Site: http:/www.diy-/ Location: Beijing/ Update history:/ 2010/01/15 - Motor actions to rotate cube/ 2010/01/16 - Fix position for cube base and color sensor motor/ 2010
2、/01/24 - Add codes to scan 6 sides of cube/ 2010/02/02 - Add bluetooth connect to PC/ 2010/04/01 - Add Error control and playing sound/ 2010/05/01 - Rewrite some codes to make it more beautifulpublic class RubikSolverV2 /Define Sensorsstatic UltrasonicSensor distance=new UltrasonicSensor(SensorPort.
3、S1);static LightSensor light = new LightSensor(SensorPort.S2);static ColorSensor color = new ColorSensor(SensorPort.S3);/Define Motorsstatic Motor paw = Motor.A;static Motor bottom = Motor.C;static Motor monitor = Motor.B;/if the paw has the 3:1 reducer, set it as true static boolean HasReducer = tr
4、ue;/the motor angle for paw to hold the cubestatic int PawHoldPosition = 56;/the motor angle for paw to rotate the cubestatic int PawTurnOverPosition = 110;/the motor angle for base to rotate 90 degreestatic int BaseOneQuarter = 315;/the fix angle for basestatic int BaseRotateFix = 40;/the fix angle
5、 of base position fixstatic int FixBasePositionOffset = 16;/the init position of color sensor motor(this will be set automatically)static int ColorMotorBaseAngle = 0;/add offset positions for color sensor motorstatic int ColorMotorOffset1 = 33;static int ColorMotorOffset2 = 9;static int ColorMotorOf
6、fset3 = 18;static int ColorReadPostion1 = 162;static int ColorReadPostion2 = 154;/A flag to check if the cube is on the basestatic boolean hasError = false; public static void main (String arg) throws Exception light.setFloodlight(false); bottom.setSpeed(1000); BlueTooth.Connect(); boolean hasCube =
7、 true; /flag used to get the distance boolean isChaotic = true; /flag used to record a chaotic cube while(!Button.ESCAPE.isPressed() /Wait for the distance being in the correct range: 1216 int CheckStatusTimes=0;LCD.clear();boolean previousStatus = true;boolean currentStatus = true;while(CheckStatus
8、Times+ < 10)int n = distance.getDistance();LCD.drawString("distance=" + n + " ", 0, 0);currentStatus = (n>=12 && n<=16);if(currentStatus != previousStatus)CheckStatusTimes = 0;previousStatus = currentStatus;Thread.sleep(100);ha
9、sCube = currentStatus;if(!hasCube)/if the cube is take away, we consume it is been upsetisChaotic = true; if(hasCube && isChaotic) /The cube is read, init the error status hasError = false; /Play some sound to notice the "Start"Thread.sleep(1000);Sound.twoBeeps();Th
10、read.sleep(1000);Sound.p laySample(new File("Start.wav");/Fix anglesRobot.FixBasePosition(); Robot.FixColorSensorPosition(); /Read Colors Robot.ReadAllSide(); if(!hasError) /Send 255 to start calculate BlueTooth.WriteBytes(new byte(byte)255); /The first return byte is the length of
11、 steps byte readStepCount = BlueTooth.ReadBytes(); int stepCount = readStepCount0; LCD.drawString("steps=" + stepCount, 1, 1); /Start to action for(int i=0; i<stepCount; i+) if(!Robot.CheckCubeReady() break; /Fix angle every 10 steps if(i % 10 = 0) Robot.FixBasePosition(); /
12、Send request for the i step BlueTooth.WriteBytes(new byte(byte)i); /Get result int step = BlueTooth.ReadBytes()0; if(step=10) /Rotate paw Robot.RotatePaw(); else if(step>=20 && step<30) /Rotate Bottom int count = step - 20; if(count = 3) count = -1; Robot.RotateBottom(c
13、ount); else if(step>=30 && step<40) /Rotate Bottom Side int count = step - 30; if(count = 3) count = -1; Robot.RotateBottomSide(count); /Send 254 to reset the pc data BlueTooth.WriteBytes(new byte(byte)254); if(!hasError) /The cube has been solved isChaotic = false; Sou
14、nd.playSample(new File("End.wav");Thread.sleep(1000);Robot.RotateBottom(8); Thread.sleep(500); BlueTooth.Disconnect(); public static class BlueTooth static BTConnection btc ; static DataInputStream dis; static DataOutputStream dos; public static void Connect() throws Exception LCD.
15、clear(); LCD.drawString("Waiting BTC.",0,0); btc = Bluetooth.waitForConnection(); LCD.drawString("Connected",0,2); LCD.refresh(); dis = btc.openDataInputStream(); dos = btc.openDataOutputStream(); public static void Disconnect() throws Exception if(btc!=null)Write
16、Bytes(new byte(byte)255,(byte)255,(byte)255);Thread.sleep(100);dos.close();dis.close();btc.close(); public static void WriteBytes(byte data) throws Exception for(int i=0;i<data.length;i+) dos.writeByte(datai); dos.flush(); public static byte ReadBytes() throws Exception byte buffer = new byte
17、255; int length = btc.read(buffer, buffer.length); if(length=-2) /lost data, re-sync btc.read(null, 255); return new byte0; else byte data = new bytelength; for(int i=0;i<length;i+) datai = bufferi; return data; public static class Robot /Rotate bottom side of cubepublic static void RotateBot
18、tomSide(int nQuarter) throws Exception paw.setSpeed(400);int nFixAngle = BaseRotateFix * ( nQuarter > 0 ? 1 : -1);int nPawHoldPosition = PawHoldPosition;if(HasReducer) nPawHoldPosition = -3 * nPawHoldPosition;paw.rotateTo(nPawHoldPosition);bottom.rotate(nQuarter * BaseOneQuarter + nFixAngle);
19、bottom.rotate(-nFixAngle);paw.rotateTo(0);/Rotate the whole cube from bottom, without hold the armpublic static void RotateBottom(int nQuarter)throws Exception bottom.rotate(nQuarter * BaseOneQuarter);/Rotate the whole cube from pawpublic static void RotatePaw()throws Exception int nPawHoldPosition
20、= PawHoldPosition - 8;if(HasReducer) nPawHoldPosition = -3 * nPawHoldPosition;int nPawTurnOverPosition = PawTurnOverPosition;if(HasReducer) nPawTurnOverPosition = -3 * nPawTurnOverPosition;paw.setSpeed(1000);paw.rotateTo(nPawHoldPosition);paw.setSpeed(300);paw.rotateTo(nPawTurnOverPosition);paw.setS
21、peed(400);paw.rotateTo(nPawHoldPosition);paw.setSpeed(1000);paw.rotateTo(0);/Fix the position of cube basepublic static void FixBasePosition() throws Exceptionint step = 3;int tolerance = 4;light.setFloodlight(false);bottom.rotate(-50);int angle = 0, minLight = 10000;int realtimeLight = ReadLightDif
22、ference();while(realtimeLight < minLight + tolerance)bottom.rotate(step);realtimeLight = ReadLightDifference();if(realtimeLight < minLight)minLight = realtimeLight;angle = 0;elseangle += step;bottom.rotate(- angle/2 - FixBasePositionOffset);/Read the light difference between light on a
23、nd light offprivate static int ReadLightDifference() throws Exceptionint l1 = 0, l2 = 0;l1 = light.readValue();light.setFloodlight(true);Thread.sleep(20);l2 = light.readValue();light.setFloodlight(false);return l1-l2;/Fix color sensor positionpublic static void FixColorSensorPosition() throws Except
24、ionint tolerance = 5;ColorMotorBaseAngle = -25;monitor.rotateTo(ColorMotorBaseAngle);Thread.sleep(100);monitor.setSpeed(50);int r = color.getRawRed();int g = color.getRawGreen();int b = color.getRawBlue();int baseColor = r + g + b;int TargetExists = 0;while(TargetExists < baseColor + toleranc
25、e && ColorMotorBaseAngle > -50)monitor.rotateTo(ColorMotorBaseAngle-);r = color.getRawRed();g = color.getRawGreen();b = color.getRawBlue();TargetExists = r + g + b;monitor.rotateTo(ColorMotorBaseAngle + 32); /Read each side colors of the cube public static void ReadAllSide() t
26、hrows Exception/Rotate the 6 sides in sequenceint nSideIndex=0;ReadOneSide(nSideIndex+);if(CheckCubeReady() RotatePaw();ReadOneSide(nSideIndex+);if(CheckCubeReady() RotatePaw();ReadOneSide(nSideIndex+);if(CheckCubeReady() RotatePaw();ReadOneSide(nSideIndex+);RotateBottom(-1);if(CheckCubeReady() Rota
27、tePaw();ReadOneSide(nSideIndex+);if(CheckCubeReady() RotatePaw();if(CheckCubeReady() RotatePaw();ReadOneSide(nSideIndex+);/Read one side by the indexpublic static void ReadOneSide(int nSideIndex) throws Exception/Add a delay time for the motor to be stableint delay=120; int idx=4,6,7,8,5,2,1,0,3,4,0
28、,3,6,7,8,5,2,1,4,2,1,0,3,6,7,8,5,4,8,5,2,1,0,3,6,7,4,2,1,0,3,6,7,8,5,4,2,1,0,3,6,7,8,5;int idx2=5,1,4,3,2,0;int i=0;monitor.setSpeed(200);bottom.setSpeed(1000);/Read Center Colormonitor.rotateTo(ColorMotorBaseAngle - ColorMotorOffset1);Thread.sleep(delay);SendColorToPC(idx2nSideIndex, idxnSideIndexi
29、+);/Read Bordersfor(int jj=0;jj<4;jj+)monitor.rotateTo(ColorMotorBaseAngle - ColorMotorOffset2);Thread.sleep(delay);SendColorToPC(idx2nSideIndex, idxnSideIndexi+);monitor.rotateTo(ColorMotorBaseAngle - ColorMotorOffset3);bottom.rotate(-ColorReadPostion1);Thread.sleep(delay);SendColorToPC(idx2nSideIndex, idxnSideIndexi+);bottom.rotate(-ColorReadPostion2);monitor.rotate
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025届山东省日照市高三上学期开学考试-生物试题(含答案)
- 2025年大学物理考试常识学习试题及答案
- 2025年可持续发展与管理专业知识考试试题及答案
- 2025年课程与教学论基础知识测试卷及答案
- 2025年跨文化沟通能力考试试卷及答案
- 2025年金融学专业考研模拟试卷及答案
- 心理设备购买合同协议
- 商场保安服务合同协议
- 正规房产抵押合同协议
- 模具材料供应协议书范本
- 2025-2030年中国不饱和聚酯树脂市场发展现状及前景趋势分析报告
- 儿童心理学常识题单选题100道及答案
- 【课件】科研数据的种类及处理方法
- 风油精产品生产技术现状
- 《电磁场与电磁波》(第4版)谢处方第4-5章
- 供热管网系统运行工国家职业标准
- 七下道法【辨析题】专练18题
- 2024年湖北省中考语文真题(学生版+解析版)
- 古诗词诵读《客至》课件+2023-2024学年统编版高中语文选择性必修下册
- 信号与系统考试试题及答案
- 2024版肿瘤患者静脉血栓防治指南解读
评论
0/150
提交评论