找回密码
 马上注册

QQ登录

只需一步,快速开始

查看: 5935|回复: 7

贴两个跟线走的程序,希望对大家有点帮助

[复制链接]
发表于 2009-2-6 14:07:39 | 显示全部楼层 |阅读模式
跟线走程序一
  1. #define SpeedSlow 50
  2. #define SpeedFast 100

  3. int SV;
  4. int LoopCount;

  5. sub FollowLine(int loopTime)
  6. {
  7. int Threshold1=600;
  8. int Threshold2=630;
  9. int theSpeed;
  10. long t;

  11. // set sensor type and mode
  12. SetSensorType(IN_3, IN_TYPE_LIGHT_ACTIVE);
  13. SetSensorMode(IN_3, IN_MODE_RAW);

  14. // start looping
  15. t = CurrentTick() + loopTime;
  16. while (t > CurrentTick())
  17. {
  18. // read the light sensor value
  19. SV = SensorRaw(IN_3);

  20. // set speed for motor 1
  21. if (SV < Threshold2)
  22. OnFwd(OUT_A, SpeedFast);
  23. else
  24. OnFwd(OUT_A, SpeedSlow);

  25. // set speed for motor 2
  26. if (SV > Threshold1)
  27. OnFwd(OUT_B, SpeedFast);
  28. else
  29. OnFwd(OUT_B, SpeedSlow);

  30. // display sensor value
  31. // NumOut(0, LCD_LINE1, false, SV);

  32. LoopCount++;
  33. }
  34. // 10 second loop is done
  35. return
  36. }

  37. task main()
  38. {
  39. string lcStr;
  40. string svStr;
  41. string msg;

  42. // call subroutine
  43. FollowLine(10000);

  44. // output results
  45. lcStr = NumToStr(LoopCount);
  46. svStr = NumToStr(SV);
  47. msg = svStr + " - " + lcStr;
  48. TextOut(0, LCD_LINE1, msg);

  49. // stop both motors
  50. Off(OUT_AB);

  51. // let user see the last message
  52. Wait(10000);
  53. }

复制代码


跟线走程序二
  1. #define SpeedSlow 50
  2. #define SpeedFast 100

  3. int SV;
  4. int LoopCount;

  5. #define GetRawValue(port, v) \
  6. asm { \
  7. getin v, port, RawValue \
  8. }

  9. #define SetMotorSpeed(port, cc, thresh, fast, slow) \
  10. asm { \
  11. set theSpeed, fast \
  12. brcmp cc, EndIfOut__I__, SV, thresh \
  13. set theSpeed, slow \
  14. EndIfOut__I__: \
  15. OnFwd(port, theSpeed) \
  16. __IncI__ \
  17. }

  18. #define DisplayNum(v) \
  19. asm { \
  20. dseg segment \
  21. dtArgs__I__ TDrawText \
  22. dseg ends \
  23. numtostr dtArgs__I__.Text, v \
  24. syscall DrawText, dtArgs__I__ \
  25. __IncI__ \
  26. }

  27. #define Inc(v) \
  28. asm { \
  29. add v, v, 1 \
  30. }

  31. sub FollowLine(int loopTime)
  32. {
  33. int Threshold1=600;
  34. int Threshold2=630;
  35. int theSpeed;
  36. long t;

  37. // set sensor type and mode
  38. SetSensorType(IN_3, IN_TYPE_LIGHT_ACTIVE);
  39. SetSensorMode(IN_3, IN_MODE_RAW);

  40. // start looping
  41. t = CurrentTick() + loopTime;
  42. while (t > CurrentTick())
  43. {
  44. // read the light sensor value
  45. GetRawValue(IN_3, SV);

  46. // set speed for motor 1
  47. SetMotorSpeed(OUT_A, LT, Threshold2, SpeedFast, SpeedSlow);

  48. // set speed for motor 2
  49. SetMotorSpeed(OUT_B, GT, Threshold1, SpeedFast, SpeedSlow);

  50. // display sensor value
  51. DisplayNum(SV);

  52. // increment loop count
  53. Inc(LoopCount);
  54. }

  55. // 10 second loop is done
  56. return
  57. }

  58. task main()
  59. {
  60. string lcStr;
  61. string svStr;
  62. string msg;

  63. // call subroutine
  64. FollowLine(10000);

  65. // output results
  66. lcStr = NumToStr(LoopCount);
  67. svStr = NumToStr(SV);
  68. msg = svStr + " - " + lcStr;
  69. TextOut(0, LCD_LINE1, msg);

  70. // stop both motors
  71. Off(OUT_AB);

  72. // let user see the last message
  73. Wait(10000);
  74. }

复制代码
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
发表于 2009-7-23 09:20:17 | 显示全部楼层
600-630是什么颜色的线?
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

 楼主| 发表于 2009-7-23 09:53:54 | 显示全部楼层
600-630应该是在黑和白的交界,低于600是黑色,高于630是白色
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2009-10-16 22:25:54 | 显示全部楼层
我怎么看着像JAVA又像C.当然,两个我都不懂. 但是最近在研究JAVA.
所以LZ这个到底是什么语言呢.
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2009-10-16 23:07:20 | 显示全部楼层
本帖最后由 Programus 于 2009-10-16 23:08 编辑

LZ这个是NXC,就是Not eXactly C的缩写。
是一个类C语言的NXT编程语言。
很多语法很像C,但是不支持指针,据说连递归都不支持。

楼上如果正在研究Java,正好我最近在翻译Java写NXT程序的指南,后续可能还会补充一些Java基础教程。赶快来LeJOS子版面跟我混吧。哈哈……
LeJOS可比NXC强大多了哟。

糖某大叫:臭小子,刚任命你为版主,就开始拉帮结派!小心我毙了你!
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2010-4-24 20:06:56 | 显示全部楼层
好啊好啊!
唐大大真是好人!
这个程序比我一直自己写的好多了!
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2011-8-12 11:21:30 | 显示全部楼层
厉害啊。
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|中文乐高 ( 桂ICP备13001575号-7 )

GMT+8, 2024-6-4 18:51 , Processed in 0.087608 second(s), 20 queries .

Powered by Discuz! X3.5

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表