前端机器人开发基础

内容纲要

web embedded

随着技术的发展,越来越多的企业开始招聘前端硬件,其开发效率和性能没有任何一种技术可以媲美,前景越来越好。这里给开发人员一个机器人开发示例,由于机器人开发后期需要大量算法支持,这里只做硬件层的讲解。

硬件要求和基础配置详讯相关专业人士,这里将所有需要文件下载到本地,并使用npm作为开发配置工具。

创建项目及相关文件

首先使用创建项目并用npm初始化:

mkdir && cd robot-demo
npm init -y

这里在初始化时使用了npm默认的配置,根据需求修改,也可以创建完成后修改:

app name: robot-demo
version: 0.1.0
description: Hello, Robot!
author: indeex

由于是前端的一些常用文件,创建其他文件略。

添加开关外设

添加开关和led外设,这里将开关型号烧成Btn001,开关名烧成button:

添加开关:

npm add --dev device button

添加led:

npm add --dev device led

填入型号并确认:

model: Btn001
select a driver for device "button"(Btn001): (Use arrow keys)

接线

获取接线布局(图):

npm get device layout

也可以使用可视化接线图:

npm get device layout --visual

根据接线图将相应的线接上就可以开发了。

驱动

开发驱动的时候注意对内存的控制,驱动开发略。

开关控制

这里需要对开关的按下和释放添加事件:

class Button{
    public button!: any;
    public led!: any;
    constructor(){
        super();
        this.ready();
        this.end();
    }

    private ready(error: Error, device: any): void{
        if (error) {
            console.log(error);
            throw error;
        }

        this.button = device.getByName('button');
        this.led = device.getByName('led-red');

        this.button.on('push', () => {
            console.log('btn down');
            this.led.turnOn();
        });

        this.button.on('release', () => {
            console.log('btn up');
            this.led.turnOff();
        });
    }

    public end(): void{
        this.led.turnOff();
    }
}

添加距离传感器

npm add --dev device distance

获取距离传感器数据

this.distance = device.getByName('distance');

//...

this.distance.getDistance( (error: Error, distance: any) => {
  console.log('distance: ' + distance);
});

//...


this.distance.getTemperature( (error: Error, temperature: any) => {
  console.log('temperature: ' + temperature);
});


由于在硬件运行一段时间后,温度都会上升,这里在烧制时对距离传感器添加了温度传感,可以测量传感器自身温度。

网络

将所有日志进行网络传输,就可以在任何地方查看各硬件的运行情况:

public http!: Http;


//...

const options = {
    host: 'http://indeex.cc',
    path: '/post',
    method: 'POST',
    headers: {
    }
};

//...


public postState(state: any) :void{
    options.headers['Content-Length'] = state.length;
    let req = http.request(options, (res) => {
        res.on('data', (chunk) => {
            console.log('chunk: ' + chunk);
        });
    });

    req.write(state);
    req.end();
}


//...

this.button.on('push', () => {
    console.log('btn down');
    this.led.turnOn(() => {
        this.postState('turn on');
    });
});

this.button.on('release', () => {
    console.log('btn up');
    this.led.turnOff(() => {
        this.postState('turn off');
    });
});

测试

重新部署后开始测试:

npm get device layout -S
npm test

然后按下开关测试相应操作的响应、延迟等数据,可以启用log。

log

为了减少查找bug的时间,需要得到运行日志,所以这里部署log:

npm get log --all

重新部署后就可以使用了。

code enjoy! 🤪🤪🤪

作者:indeex

链接:http://indeex.cc

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


发表评论

您的电子邮箱地址不会被公开。