舵机防抖代码

舵机防抖控制

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
signal = 18
GPIO.setup(signal, GPIO.OUT)
frequency = 50
pwm = GPIO.PWM(signal, frequency)
def get_duty(direction):
    duty = (1/18)*direction + 2.5
    return duty

if __name__ == '__main__':
    try:
        pwm.start(0)
        while True:
            direction = float(input("Please input a direction between 0 and 180:"))
            duty = get_duty(direction)
            pwm.ChangeDutyCycle(duty)
            time.sleep(0.04)     #防抖
            pwm.ChangeDutyCycle(0)     #防抖
    except Exception as e:
        print('An exception has happened',e)        
    finally:
        pwm.stop()
        GPIO.cleanup()

修正角度

import RPi.GPIO as GPIO
import random
import time
GPIO.setmode(GPIO.BCM)
signal = 18
GPIO.setup(signal, GPIO.OUT)
frequency = 50
pwm = GPIO.PWM(signal, frequency)
def get_duty(direction):
    duty = (1/18)*direction + 2.5
    return duty

if __name__ == '__main__':
    try:
        pwm.start(0)
        while True:
            direction = float(input("Please input a direction between 0 and 180:"))
#            direction =  random.randint(0,0)
            print(direction)
            duty = get_duty(direction)
            for i in range(1,20):
                pwm.ChangeDutyCycle(duty)
                time.sleep(0.04)
            pwm.ChangeDutyCycle(0)
    except Exception as e:
        print('An exception has happened',e)        
    finally:
        pwm.stop()
        GPIO.cleanup()

更改pip镜像源

一、国内常用pip镜像源
http://pypi.douban.com/simple/ 豆瓣
http://mirrors.aliyun.com/pypi/simple/ 阿里
http://pypi.hustunique.com/simple/ 华中理工大学
http://pypi.sdutlinux.org/simple/ 山东理工大学
http://pypi.mirrors.ustc.edu.cn/simple/ 中国科学技术大学
https://pypi.tuna.tsinghua.edu.cn/simple/ 清华大学
http://pypi.hustunique.com/ 华中科技大学
http://mirrors.cloud.tencent.com/pypi/simple 腾讯
https://repo.huaweicloud.com/repository/pypi/simple/ 华为
二、使用方法(pip安装):
示例代码(安装requests库):

pip install -i http://pypi.douban.com/simple/ requests

若遇到信任问题可添加参数,如

pip install requests -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

永久修改pip源方法
Windows:新建 pip.ini 文件存放在 User\pip\pip.ini
Linux:新建pip.conf文件存放在 ~/.pip/pip.conf
内容如下

[global]
trusted-host=mirrors.aliyun.com
index-url=http://mirrors.aliyun.com/pypi/simple/

ubuntu更换国内源

查看版本号

lsb_release -a

codename就是你的Ubuntu版本的代号,需将以下bionic更改为自己系统的版本

sudo gedit /etc/apt/sources.list
#清华源
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

再执行sudo apt update及sudo apt upgrade

然后就可以sudo dpkg -i ipscan_3.6.2_amd64.deb了

树莓派局域网扫描工具

使用sudo apt-get install arp-scan 安装(确保网络链接正常)

利用ifconfig 或ip addr 查看本机的ip地址,一般有线在interface en0/eth0, 无线在wlan0上.

ifconfig

然后就可以查看同一局域网的其他设备

sudo arp-scan -I eth0 --localnet

linux快速搜索历史命令

ctr+r快捷键(推荐)

   一个比较快速的方法是使用"ctrl+r"快捷键,在命令行使用ctrl+r,ctrl+r是反向搜索 (reverse-i-search )


  输入要查找命令的关键字,会显示在第二个红色标记位置,如果找到对应的命令,会显示在第三个标记位置,  如果不是自己期望的命令,可以多次使用“ctrl+r”切换显示命令,然后按键enter或者->即执行命令。

mqtt服务器的软看门狗

import os

def getpidinfo():
    p=os.popen('pidof chrome')
    line=p.readline()
    return(line.split())

if __name__ == '__main__':
    if getpidinfo() == []:
        print('none')
    else:
        for i in getpidinfo():
            print(i)

可部署版本

import os
import time
def getpidinfo():
    p=os.popen('pidof mosquitto')
    line=p.readline()
    return(line.split())

if __name__ == '__main__':
    while True:
        if getpidinfo() == []:
           print(getpidinfo())
           os.system('sudo systemctl restart mosquitto.service')
           time.sleep(5)
        else:
           continue