智慧垃圾桶

功能模块

  • 树莓派主控
  • oled显示屏
    • 显示温度
    • 时间
    • 树莓派IP
  • 超声波模块
    • 识别丢垃圾的动作
    • 识别障碍物
  • 温度传感器
    • 读取当前环境的温度
  • SG90舵机
    • 打开清洁桶的盖子
  • 马达及轮子
  • L9110s
    • 控制马达正转反转,实现转弯
  • 清洁桶

架构

  • 远程服务端
    • 树莓派
  • 远程控制客户端
    • linux虚拟机
  • 多线程及锁
  • tcp ip网络编程
  • 文件操作——日志模块

编程

超声波模块

#include 
#include 
#include 
#include "chaosbo.h"
#define echo 2
#define trig 0

void initchaosbo() {

    int a_init = wiringPiSetup();
    if (a_init == 0) {
        printf("init sucess\n");
    }

    pinMode(echo, INPUT);
    pinMode(trig, OUTPUT);
}

float getdis() {

    struct timeval t1;
    struct timeval t2;
    long start;
    long stop;
    float dis;

    digitalWrite(trig,0);
    digitalWrite(trig,0);
    delay(5);

    digitalWrite(trig, 1);
    delayMicroseconds(10);  // 10us
    digitalWrite(trig, 0);

    while (!(digitalRead(echo)) == 1) {
        gettimeofday(&t1, NULL);
    }
    while (!(digitalRead(echo)) == 0) {
        gettimeofday(&t2, NULL);
    }

    start = t1.tv_sec * 1000000 + t1.tv_usec;
    stop = t2.tv_sec * 1000000 + t2.tv_usec;

    dis = (float)(stop - start)/1000000*(34000/2);
    return dis;
}

日志模块

#include "log.h"
#include 
#include 
#include 

FILE *g_log = NULL;

void log_create(const char *filename) {
    g_log = fopen(filename, "a+");
    if (NULL == g_log) {
        printf("fopen %s failed\n", filename);
    }
}

void log_destroy() {
    fclose(g_log);

    g_log = NULL;
}

void log_write(const char *format, ...) {

    va_list args;

    va_start(args, format);

    vfprintf(g_log, format, args);

    va_end(args);

    fflush(g_log);
}

马达模块

#include 

#define A1 23
#define B1 22
#define BA1 28
#define BA2 29

void initmada()
{
	wiringPiSetup();

	pinMode(A1,OUTPUT);
	pinMode(B1,OUTPUT);
	pinMode(BA1,OUTPUT);
	pinMode(BA2,OUTPUT);
	digitalWrite(23,0);
	digitalWrite(22,0);

	digitalWrite(28,0);
	digitalWrite(29,0);

}

void left()
{		
	digitalWrite(23,1);
	digitalWrite(22,0);

	digitalWrite(28,0);
	digitalWrite(29,1);

}
void right()
{
	digitalWrite(23,0);
	digitalWrite(22,1);

	digitalWrite(28,1);
	digitalWrite(29,0);


}
void back()
{
	digitalWrite(23,1);
	digitalWrite(22,0);

	digitalWrite(28,1);
	digitalWrite(29,0);
}
void forword()
{
	digitalWrite(23,0);
	digitalWrite(22,1);

	digitalWrite(28,0);
	digitalWrite(29,1);	

}

显示屏模块,设计点阵,代码存放在码云中。

服务端

#include 
#include  /* See NOTES */
#include 
#include "log.h"
#include 
#include "chaosbo.h"
#include 
#include "sg900.h"
#include 
#include "mada.h"
#include 
#include "wendu.h"
#include "oled.h"
void *controldir(void *arg) {
	
    char buf[32];
    int ret_recv;
    int sock = *(int *)arg;
    while (1) {
        ret_recv = recv(sock, buf, 32, 0);
        if (ret_recv > 0) {
            log_write("recv success\n");
            log_write("neirong:%s\n", buf);
        }
        if (strncmp(buf, "w", 1) == 0) {
            initmada();
            forword();
        } else if (strncmp(buf, "s", 1) == 0) {
            initmada();
            back();
        } else if (strncmp(buf, "d", 1) == 0) {
            initmada();
            right();
        } else if (strncmp(buf, "a", 1) == 0) {
            initmada();
            left();
        } else if(strncmp(buf,"q",1)==0){
	    initmada();
	}
        memset(buf, 0, sizeof(buf));
    }
}
void *dispalydata(void *arg){
    float wd;
    char msg[4][16];
    while(1){
    	memset(msg,0,sizeof(msg));
	wd = wendudisplay();
	sprintf(msg[0],"t = %f c",wd);
	oleddisplay(msg);
    }

}

int main() {

    pthread_t t1;
    pthread_t t2;
    wiringPiSetup();
    log_create("serve.txt");
    int fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd != -1) {
        log_write("creat socket sucess\n");
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = 8888;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);

    int ret_bind = bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
    if (ret_bind == 0) {
        log_write("bind sucess\n");
    }

    int ret_listen = listen(fd, 10);
    if (ret_listen == 0) {
        log_write("listen sucess\n");
    }

    int sock = accept(fd, NULL, 0);
    if (sock != -1) {
        log_write("sock success\n");
    }
    initchaosbo();// init chaosbo

    pthread_create(&t1, NULL, controldir, &sock);
    pthread_create(&t2,NULL,dispalydata,NULL);

    while (1) {
        if(getdis() <= 20){
            duoji();
	    initmada();
            while(getdis()<=20);
            duojik();
        }

    }

    log_destroy();
}

sg900模块

#include 
#include 
#include 
#include 
#include 
#include 
#include "sg900.h"

#define RANGE 200  // 1 means 100 us , 200 means 20 ms 1等于100微妙,200等于20毫秒


void duoji(){
    //wiringPiSetup();
    softPwmCreate(3, 15, RANGE);  //创建一个使舵机转到90的pwm输出信号
    delay(100);
    softPwmWrite(3, 5);  //将pwm输出复写为使舵机转到0
    delay(100);

    /*softPwmWrite(3, 15);  //再次复写pwm输出
    delay(100);
    softPwmWrite(3, 15);*/
}
void duojik(){

   // wiringPiSetup();
    softPwmCreate(3, 15, RANGE);  //创建一个使舵机转到90的pwm输出信号

    softPwmWrite(3, 15);  //再次复写pwm输出
    delay(100);
    softPwmWrite(3, 15);




}


温度模块

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "oled.h"
#include "wendu.h"
float wendudisplay(){

    int fd;
    int ret;
    float wd;
    char *tmp;
    char buf[128];
    char msg[4][16];

    while (1) {
	memset(msg,0,sizeof(msg));
        fd = open("/sys/bus/w1/devices/28-031597792ce7/w1_slave",
                  O_RDONLY);  //以只读方式打开ds18b20设备文件

        if (-1 == fd) {                        //打开设备文件出错
            perror("open device file error");  //打印出错信息(原因)
        }
        ret = read(fd, buf, 128);  //将设备文件中的内容读入buf中
        if (-1 == ret) {           //返回0表示读文件结束
            break;                 //跳出循环体
        }

        if (-1 == ret) {           //读文件出错
            if (errno == EINTR) {  //出错是由中断引起的
                continue;          //继续读文件
            }
            perror("read()");  //打印出错信息(原因)
            close(fd);         //关闭文件
        }
        tmp = strstr(buf, "t=");
        tmp = tmp + 2;
        wd = atof(tmp) / 1000;
        //sprintf(msg[0],"t=%0.2f",wd);
	//oleddisplay(msg);
	return wd;
        sleep(2);
        close(fd);  //关闭文件
    }
}


你可能感兴趣的:(智慧垃圾桶)