遥控车

作者: 招风小妖怪 | 来源:发表于2018-12-17 14:01 被阅读0次
    舵机

    型号s3010

    pin_duo = 8
    pwm.setup(pin_duo,100,100)
    pwm.start(pin_duo)
    
    uart.on("data",function(msg)
      print(msg)
      pwm.setduty(pin_duo, msg)
    end, 0)
    
    
    --频率100HZ
    ---155最左
    ---125最右
    --140中
    左竖  右竖  左横  右横  L      M      R      K      
    1500,1500,1500,1500,1000,1000,1000,0
    
    local fx     =140
    local sd     =0
    local df_sd  =0
    local df_fx  =0
    local df_tsd =0
    ------------------------WIFI setting*----------------------------
    wifi.setmode(wifi.SOFTAP)
    cfg={}
    cfg.ssid="ESP8266"
    cfg.pwd="ESP8266_123"
    wifi.ap.config(cfg)
    print(wifi.ap.getip())
    -----------------------duoji----------------------------
    pin_duo = 8
    pwm.setup(pin_duo,100,fx)
    pwm.start(pin_duo)
    -----------------------Fa dong ji----------------------------
    pin_faDong_z1 = 7
    pin_faDong_z2 = 6
    pwm.setup(pin_faDong_z1,100,sd)
    pwm.start(pin_faDong_z1)
    
    pwm.setup(pin_faDong_z2,100,sd)
    pwm.start(pin_faDong_z2)
    
    pin_faDong_y1 = 5
    pin_faDong_y2 = 3
    pwm.setup(pin_faDong_y1,100,sd)
    pwm.start(pin_faDong_y1)
    
    pwm.setup(pin_faDong_y2,100,sd)
    pwm.start(pin_faDong_y2)
    -------------------------LED ------------------------------------
    pin_led = 4
    gpio.mode(pin_led, gpio.OUTPUT)
    gpio.write(pin_led, gpio.HIGH) 
    -------------------------server ------------------------------------
    
    dog = 0      
    sv=net.createServer(net.TCP, 200)
    sv:listen(6000,function(c)
        c:on("receive", function(c, msg) 
            dog=1
            gpio.write(pin_led, gpio.LOW) 
           -- print(msg)
    
    
            fx    = tonumber(string.sub(msg,16,19))
            sd    = tonumber(string.sub(msg,1,4))       
            df_sd = tonumber(string.sub(msg,21,24))
            df_fx = tonumber(string.sub(msg,26,29))
            df_tsd= tonumber(string.sub(msg,31,34))
            
                
    --        print(fx)
    --        print(sd)
    --        print(df_sd)
    --        print(df_fx)
    --        print(df_tsd)
    --        print("----------------------------")
            function fun()      
                if fx >=1000 and fx <=2000 then
                    local i=(fx/10-100)/10*3+125
    
                    i=i-(i-140)*2
                    --print(i)
                    
                    pwm.setduty(pin_duo, i)
                end
                if sd >=1500 and sd <=2000 then
                    local i=(sd-1500)/5
                    i=i*((df_sd-1000)/10)/100
                   --print(i)
                   --print("----------------------------")
                    
                    pwm.setduty(pin_faDong_z1, i*10)
                    pwm.setduty(pin_faDong_y1, i*10)
    
                    pwm.setduty(pin_faDong_z2, 0)
                    pwm.setduty(pin_faDong_y2, 0)
                elseif sd >=1000 and sd <1500 then
                    local i=(sd-1000)/5
    
                    i=i-(i-50)*2
                    i=i*((df_tsd-1000)/10)/100
                    --print(i)
                    --print("----------------------------")
                    pwm.setduty(pin_faDong_z2, i*10)
                    pwm.setduty(pin_faDong_y2, i*10)
    
                    pwm.setduty(pin_faDong_z1, 0)
                    pwm.setduty(pin_faDong_y1, 0)
                end
            end
            
           ok=pcall(fun,0)
        end)
      c:send("hello world") 
    end)
    tmr.alarm(0, 200,1, function()
        if dog == 1 then
            dog = 0
        elseif dog == 0 then
            pwm.setduty(pin_faDong_z1, 0)
            pwm.setduty(pin_faDong_y1, 0)
            pwm.setduty(pin_faDong_z2, 0)
            pwm.setduty(pin_faDong_y2, 0)
            --print("have man disconnection")
            gpio.write(pin_led, gpio.HIGH) 
        end
    end)
    
    

    相关文章

      网友评论

          本文标题:遥控车

          本文链接:https://www.haomeiwen.com/subject/vwzphqtx.html