四博智联产品售后

 找回密码
 立即注册
搜索
查看: 9436|回复: 0

DoitCar STA模式底层控制第二版(Lua源码)

[复制链接]

0

主题

0

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2015-8-19 19:03:42 | 显示全部楼层 |阅读模式
底层控制程序第二版本(STA)
改进:将左/右转弯由原来的连续转弯修改为点动转弯。即启动转弯后,转动一小段时间(代码示例为100ms)随即停止。
详细情况见代码:
  1. --DoitCar Ctronl Demo
  2. --sta mode
  3. --Created @ 2015/05/14 by Doit Studio
  4. --Modified: null
  5. --http://www.doit.am/
  6. --http://www.smartarduino.com/
  7. --http://szdoit.taobao.com/
  8. --bbs: bbs.doit.am

  9. --GPIO Define
  10. function initGPIO()
  11. --1,2EN         D1 GPIO5
  12. --3,4EN         D2 GPIO4
  13. --1A  ~2A   D3 GPIO0
  14. --3A  ~4A   D4 GPIO2

  15. gpio.mode(0,gpio.OUTPUT);--LED Light on
  16. gpio.write(0,gpio.LOW);

  17. gpio.mode(1,gpio.OUTPUT);gpio.write(1,gpio.LOW);
  18. gpio.mode(2,gpio.OUTPUT);gpio.write(2,gpio.LOW);

  19. gpio.mode(3,gpio.OUTPUT);gpio.write(3,gpio.HIGH);
  20. gpio.mode(4,gpio.OUTPUT);gpio.write(4,gpio.HIGH);

  21. pwm.setup(1,1000,1023);--PWM 1KHz, Duty 1023
  22. pwm.start(1);pwm.setduty(1,0);
  23. pwm.setup(2,1000,1023);
  24. pwm.start(2);pwm.setduty(2,0);
  25. end

  26. --Control Program
  27. print("Start DoitRobo Control");
  28. initGPIO();

  29. spdTargetA=1023;--target Speed
  30. spdCurrentA=0;--current speed
  31. spdTargetB=1023;--target Speed
  32. spdCurrentB=0;--current speed
  33. stopFlag=true;

  34. tmr.alarm(1, 200, 1, function()
  35.         if stopFlag==false then
  36.                 spdCurrentA=spdTargetA;
  37.                 spdCurrentB=spdTargetB;
  38.                 pwm.setduty(1,spdCurrentA);
  39.                 pwm.setduty(2,spdCurrentB);
  40.         else
  41.                 pwm.setduty(1,0);
  42.                 pwm.setduty(2,0);
  43.         end
  44. end)

  45. local flagClientTcpConnected=false;
  46. print("Start TCP Client");
  47. tmr.alarm(0, 5000, 1, function()
  48.         if flagClientTcpConnected==false then
  49.         print("Try connect Server");
  50.         local conn=net.createConnection(net.TCP, false)
  51.         conn:connect(6005,"182.92.178.210");
  52.         conn:on("connection",function(c)
  53.                 print("TCPClient:conneted to server");
  54.                 flagClientTcpConnected = true;
  55.                 end)
  56.         conn:on("disconnection",function(c)
  57.                 flagClientTcpConnected = false;
  58.                 conn=nil;
  59.                 collectgarbage();
  60.     end)
  61.         conn:on("receive", function(conn, m)
  62.                 print("TCPClient:"..m);
  63.                 if string.sub(m,1,1)=="b" then
  64.                         conn:send("cmd=subscribe&topic=".."car".."\r\n");
  65.                 elseif string.sub(m,1,1)=="0" then --stop
  66.                         pwm.setduty(1,0)
  67.                         pwm.setduty(2,0)
  68.                         stopFlag = true;
  69.                         conn:send("ok\r\n");
  70.                 elseif string.sub(m,1,1)=="1" then --forward
  71.                         gpio.write(3,gpio.HIGH)
  72.                         gpio.write(4,gpio.HIGH)
  73.                         stopFlag = false;
  74.                         conn:send("ok\r\n");
  75.                 elseif string.sub(m,1,1)=="2" then --backward
  76.                         gpio.write(3,gpio.LOW)
  77.                         gpio.write(4,gpio.LOW)
  78.                         stopFlag = false;
  79.                         conn:send("ok\r\n");
  80.                 elseif string.sub(m,1,1)=="3" then --left
  81.                         gpio.write(3,gpio.LOW)
  82.                         gpio.write(4,gpio.HIGH)
  83.                         stopFlag = false;
  84.                         conn:send("ok\r\n");
  85.                 elseif string.sub(m,1,1)=="4" then --right
  86.                         gpio.write(3,gpio.HIGH);
  87.                         gpio.write(4,gpio.LOW);
  88.                         stopFlag = false;
  89.                         conn:send("ok\r\n");
  90.                 elseif string.sub(m,1,1)=="6" then --A spdUp
  91.                         spdTargetA = spdTargetA+50;if(spdTargetA>1023) then spdTargetA=1023;end
  92.                         conn:send("ok\r\n");
  93.                 elseif string.sub(m,1,1)=="7" then --A spdDown
  94.                         spdTargetA = spdTargetA-50;if(spdTargetA<0) then spdTargetA=0;end
  95.                         conn:send("ok\r\n");        
  96.                 elseif string.sub(m,1,1)=="8" then --B spdUp
  97.                         spdTargetB = spdTargetB+50;if(spdTargetB>1023) then spdTargetB=1023;end
  98.                         conn:send(spdTargetA.." "..spdTargetB.."\r\n");
  99.                 elseif string.sub(m,1,1)=="9" then --B spdDown
  100.                         spdTargetB = spdTargetB-50;if(spdTargetB<0) then spdTargetB=0;end
  101.                         conn:send(spdTargetA.." "..spdTargetB.."\r\n");
  102.                 else  print("Invalid Command:"..m);end;
  103.                         collectgarbage();
  104.                 end)
  105.         end
  106. end)
复制代码
完整代码下载:https://github.com/SmartArduino/DoitCar


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|四博智联 Inc. ( 粤ICP备15034758号-1

GMT+8, 2024-3-29 02:20 , Processed in 0.061989 second(s), 25 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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