Commit d382ffa5 authored by 957dd's avatar 957dd

Merge branch 'feature/update_device_c' into 'master'

优化了文件管理 See merge request !6
parents f386b8c5 0a832f17
#include "common.h"
#include "thread_main.h"
#include "gpio_pwm_carship.h"
#include "gpio_pwm_ptz.h"
#include "mqtt.h"
#include "car.h"
void car_Init_0101() {
pwm_speed(); //pwm初始化,车为停止
pin_init();
AppExit_pin_pwm=1;//车的异常停止值
}
\ No newline at end of file
#ifndef __CAR_H__
#define __CAR_H__
void car_Init_0101();
#endif
\ No newline at end of file
#include "common.h"
#include "thread_main.h"
#include "gpio_pwm_carship.h"
#include "gpio_pwm_ptz.h"
#include "mqtt.h"
#include "pao.h"
void ptr_Init_0401() {
pin_init();
pwm_PTZ_hz();
PTZ_pwm_init();
AppExit_pin_pwm=4;//车的异常停止值
}
\ No newline at end of file
#ifndef __PAO_H__
#define __PAO_H__
void ptr_Init_0401();
#endif
\ No newline at end of file
#include "common.h"
#include "thread_main.h"
#include "gpio_pwm_carship.h"
#include "gpio_pwm_ptz.h"
#include "mqtt.h"
#include "ship.h"
void ship_Init_0301() {
pwm_speed();
pin_ship_init(); //pwm初始化,车为停止
AppExit_pin_pwm=3;//车的异常停止值
}
\ No newline at end of file
#ifndef __SHIP_H__
#define __SHIP_H__
void ship_Init_0301();
#endif
\ No newline at end of file
#include "common.h"
#include "thread_main.h"
#include "gpio_pwm_carship.h"
#include "gpio_pwm_ptz.h"
#include "mqtt.h"
#include "tank.h"
void tank_Init_0201() {
pin_init();
pinMode(2, OUTPUT);//pwm引脚改为普通引脚
pinMode(21, OUTPUT);//pwm引脚改为普通引脚
digitalWrite(2, LOW);
digitalWrite(21, LOW);
AppExit_pin_pwm=2;//坦克的异常停止值
}
\ No newline at end of file
#ifndef __TANK_H__
#define __TANK_H__
void tank_Init_0201();
#endif
\ No newline at end of file
#include "common.h"
#include "thread_main.h"
#include "gpio_pwm_carship.h"
#include "gpio_pwm_ptz.h"
#include "mqtt.h"
#include "judg.h"
#include "delay.h"
#include "car.h"
#include "tank.h"
#include "ship.h"
#include "pao.h"
int Device_File_Init() {
const char *readbuf=device_inspect();
//将第3个和第4个字符提取出来
char *sub_str=malloc(5);
sub_str[0]=readbuf[2];
sub_str[1]=readbuf[3];
sub_str[2]=readbuf[4];
sub_str[3]=readbuf[5];
sub_str[4]='\0';
printf("开始初始化了");
if(strcmp(sub_str,"0101")==0){//车的编码
car_Init_0101() ;
printf("使用型号%s\n",sub_str);
free(sub_str) ;
}else if(strcmp(sub_str,"0201")==0){//坦克的编码
tank_Init_0201();
printf("使用型号%s\n",sub_str);
free(sub_str);
}else if(strcmp(sub_str,"0301")==0){//船的编码
ship_Init_0301();
printf("使用型号%s\n",sub_str);
free(sub_str);
}else if(strcmp(sub_str,"0401")==0){//炮的编码
ptr_Init_0401();
printf("使用型号%s\n",sub_str);
free(sub_str);
}
Delay_Ms(20,0);
TOPIC=malloc(24);
TOPIC2=malloc(24);
TOPIC3= malloc(16);
sprintf(TOPIC2,"dev2app/%s",readbuf);
sprintf(TOPIC,"app2dev/%s",readbuf);
sprintf(TOPIC3,"%s",readbuf);
printf("1:%s,2:%s,3:%s\n",TOPIC,TOPIC2,TOPIC3);
return 0;
}
#ifndef __JUDG_H__
#define __JUDG_H__
int Device_File_Init();
#endif
\ No newline at end of file
#ifndef COMMON_H
#define COMMON_H
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <time.h>
#include <pthread.h>
#include <errno.h>
#include <wiringPi.h>
#include <stdbool.h>
// 其他公共声明
#endif
#ifndef __DELAY_H__ #ifndef __DELAY_H__
#define __DELAY_H__ #define __DELAY_H__
#include <stdio.h>
#include <time.h>
void Delay_Ms(int sec,int msec); void Delay_Ms(int sec,int msec);
#endif #endif
\ No newline at end of file
#ifndef __IP_H__ #ifndef __IP_H__
#define __IP_H__ #define __IP_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <ifaddrs.h>
extern char ip_address[INET_ADDRSTRLEN];//读到的ip地址 extern char ip_address[INET_ADDRSTRLEN];//读到的ip地址
int ipaddr();//ip地址函数 int ipaddr();//ip地址函数
#endif #endif
#ifndef __MQTT_H__ #ifndef __MQTT_H__
#define __MQTT_H__ #define __MQTT_H__
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <mosquitto.h> #include <mosquitto.h>
#include <cjson/cJSON.h> #include <cjson/cJSON.h>
#include<time.h>
#include "ip.h"
#include "opensh.h"
#include "gpio_pwm.h"
#include "INA226.h"
#include "pthrpoll.h"
#include "gpio_pwm_2.h"
#include "heat.h"
extern char* TOPIC ;//="app2dev/controlcar0004"
extern char* TOPIC2 ;//="dev2app/controlcar0004"
extern char* TOPIC3;//= "controlcar0004"
extern ThreadPool *pool;
extern char* TOPIC ;
extern char* TOPIC2 ;
extern char* TOPIC3 ;
extern uint8_t AppExit_pin_pwm;
//extern ThreadPool *pool;
#define BROKER_ADDRESS "119.45.167.177" #define BROKER_ADDRESS "119.45.167.177"
#define BROKER_PORT 1883 #define BROKER_PORT 1883
extern uint8_t AppExit_pin_pwm;
#define USERNAME "admin" // 替换为你的用户名 #define USERNAME "admin" // 替换为你的用户名
#define PASSWORD "admin" // 替换为你的密码 #define PASSWORD "admin" // 替换为你的密码
extern struct mosquitto *mosq;//创建客服端 extern struct mosquitto *mosq;//创建客服端
extern time_t gStart;//开始时间戳 extern time_t gStart;//开始时间戳
extern int gPwmCount; // 计数 extern int gPwmCount; // 计数
extern int gmessage_type;//message消息值 extern int gmessage_type;//message消息值
int mqtt_init();//mqtt初始化 int mqtt_init();//mqtt初始化
......
#ifndef __opensh_H__ #ifndef __opensh_H__
#define __opensh_H__ #define __opensh_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mqtt.h"
int opencamsh();//打开摄像头函数 int opencamsh();//打开摄像头函数
......
#ifndef THREAD_POOL_H #ifndef THREAD_POOL_H
#define THREAD_POOL_H #define THREAD_POOL_H
#include <pthread.h> #include "common.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
// 任务结构体 // 任务结构体
typedef struct Task { typedef struct Task {
void (*function)(void *); void (*function)(void *);
......
#ifndef __THREAD_MAIN_H__
#define __THREAD_MAIN_H__
#define filename "/home/orangepi/car/master/Deviceld.txt"
int thread_start(void *AppExit(void *arg),void *Mqttbeat(void *arg),void *opensh(void *arg),void *Mqtt_onnect(void *arg)) ;
void *AppExit(void *arg) ;
void *Mqttbeat(void *arg) ;
void *opensh(void *arg) ;
void *Mqtt_onnect(void *arg) ;
char *device_inspect() ;
void thread_end() ;
#endif
\ No newline at end of file
No preview for this file type
# 编译器 # 编译器
CC = gcc CC = gcc
# 编译选项 # 编译选项-Ilib
CFLAGS = -Wall -g -Ilib CFLAGS = -Wall -g -Iinclude -Isystem/gpio -Isystem/sensor -Iusr -Idevice_judg/judg \
-Idevice_judg/device
# 链接库 # 链接库
LDFLAGS = -lwiringPi -lmosquitto -lcjson -lm LDFLAGS = -lwiringPi -lmosquitto -lcjson -lm
# 目标可执行文件 # 目标可执行文件
TARGET = main TARGET = main
# 源文件目录 # 源文件目录
SRC_DIR = usr src SRC_DIR = usr src system/gpio system/sensor device_judg/judg device_judg/device \
# 头文件目录 # 头文件目录
INC_DIR = lib INC_DIR = usr include system/gpio system/sensor device_judg/judg device_judg/device \
# 查找所有 .c 文件 # 查找所有 .c 文件
SRCS = $(foreach dir,$(SRC_DIR),$(wildcard $(dir)/*.c)) SRCS = $(foreach dir,$(SRC_DIR),$(wildcard $(dir)/*.c))
# 目标文件 # 目标文件
......
#include "common.h"
#include "delay.h" #include "delay.h"
void Delay_Ms(int sec,int msec) void Delay_Ms(int sec,int msec) {
{
struct timespec ts; struct timespec ts;
ts.tv_sec = sec; // 秒 ts.tv_sec = sec; // 秒
ts.tv_nsec = msec*1000000; // 1毫秒 = 1000000纳秒 ts.tv_nsec = msec*1000000; // 1毫秒 = 1000000纳秒
......
#include <sys/socket.h>
#include <netinet/in.h>
#include <ifaddrs.h>
#include "common.h"
#include "ip.h" #include "ip.h"
struct ifaddrs *ifap, *ifa; struct ifaddrs *ifap, *ifa;
struct sockaddr_in *sa; struct sockaddr_in *sa;
char ip_address[INET_ADDRSTRLEN]; char ip_address[INET_ADDRSTRLEN];
int ipaddr() {//获取ip int ipaddr() {//获取ip
......
#include "common.h"
#include "gpio_pwm_carship.h"
#include "gpio_pwm_ptz.h"
#include "ip.h"
#include "opensh.h"
#include "INA226.h"
#include "heat.h"
#include "mqtt.h" #include "mqtt.h"
struct mosquitto *mosq;
char* TOPIC; char* TOPIC;
char* TOPIC2; char* TOPIC2;
char* TOPIC3; char* TOPIC3;
...@@ -12,59 +21,63 @@ int message;//存放gmessage_type的值发送心跳,2,3消息为1; ...@@ -12,59 +21,63 @@ int message;//存放gmessage_type的值发送心跳,2,3消息为1;
uint8_t AppExit_pin_pwm=0;//判断坦克或者车的退出 uint8_t AppExit_pin_pwm=0;//判断坦克或者车的退出
int rc=0;//判断mqtt是否成功创建 int rc=0;//判断mqtt是否成功创建
//ThreadPool *pool;//线程池线程函数
ThreadPool *pool;//线程池线程函数
unsigned char modeTemp=0; unsigned char modeTemp=0;
unsigned char typeTemp=0; unsigned char typeTemp=0;
unsigned char pinTemp=0; unsigned char pinTemp=0;
unsigned char valTemp=0; unsigned char valTemp=0;
unsigned char gvalt[4];//存放mqtt接收的tpye,mode等 unsigned char gvalt[4];//存放mqtt接收的tpye,mode等
struct mosquitto *mosq;
time_t gStart;
char *glat=NULL;//加入gps后删除,心跳预留,不更改 char *glat=NULL;//加入gps后删除,心跳预留,不更改
char *glon=NULL;//加入gps后删除,心跳预留,不更改 char *glon=NULL;//加入gps后删除,心跳预留,不更改
time_t gStart;
int mqtt_init() { int mqtt_init() {
// 初始化 mosquitto 库 // 初始化 mosquitto 库
mosquitto_lib_init(); mosquitto_lib_init();
mosq = mosquitto_new(NULL, true, NULL); mosq = mosquitto_new(NULL, true, NULL);
if (!mosq) { if (!mosq) {
fprintf(stderr, "Failed to create Mosquitto client\n"); fprintf(stderr, "Failed to create Mosquitto client\n");
return -1; return -1;
} }
mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5); mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
return 0; return 0;
} }
void on_connect(struct mosquitto *mosq, void *obj, int rc) {//回调函数 void on_connect(struct mosquitto *mosq, void *obj, int rc) {//回调函数
if (rc == 0) if (rc == 0) {
{
printf("Connected to broker\n"); printf("Connected to broker\n");
mosquitto_subscribe(mosq, NULL, TOPIC, 0); mosquitto_subscribe(mosq, NULL, TOPIC, 0);
}
else } else {
{
fprintf(stderr, "Connection failed with code %d\n", rc); fprintf(stderr, "Connection failed with code %d\n", rc);
} }
} }
void mqtt_wirte(){//心跳格式 void mqtt_wirte(){//心跳格式
float voltage = INA226_readBusVoltage(sda_pin, scl_pin); float voltage = INA226_readBusVoltage(sda_pin, scl_pin);
float current = INA226_readCurrent(sda_pin, scl_pin); float current = INA226_readCurrent(sda_pin, scl_pin);
char voltage_str[20]; // 足够存储转换后的字符串的缓冲区 char voltage_str[20]; // 足够存储转换后的字符串的缓冲区
sprintf(voltage_str, "%.2f", voltage); sprintf(voltage_str, "%.2f", voltage);
char current_str[20]; // 足够存储转换后的字符串的缓冲区 char current_str[20]; // 足够存储转换后的字符串的缓冲区
sprintf(current_str, "%.2f", current); sprintf(current_str, "%.2f", current);
heat_tem();//获取CPU温度 heat_tem();//获取CPU温度
message=1; message=1;
cJSON *root = cJSON_CreateObject(); cJSON *root = cJSON_CreateObject();
cJSON *body = cJSON_CreateObject(); cJSON *body = cJSON_CreateObject();
cJSON *head = cJSON_CreateObject(); cJSON *head = cJSON_CreateObject();
cJSON_AddStringToObject(body, "ip", ip_address);//发送设备id cJSON_AddStringToObject(body, "ip", ip_address);//发送设备id
cJSON_AddStringToObject(body, "ID", TOPIC);//发送设备号 cJSON_AddStringToObject(body, "ID", TOPIC);//发送设备号
cJSON_AddStringToObject(body, "V:", voltage_str);//心跳发送电压 cJSON_AddStringToObject(body, "V:", voltage_str);//心跳发送电压
...@@ -72,9 +85,12 @@ void mqtt_wirte(){//心跳格式 ...@@ -72,9 +85,12 @@ void mqtt_wirte(){//心跳格式
cJSON_AddStringToObject(body,"Tem:",temperature);//发送温度 cJSON_AddStringToObject(body,"Tem:",temperature);//发送温度
cJSON_AddStringToObject(body, "N", glat);//gps cJSON_AddStringToObject(body, "N", glat);//gps
cJSON_AddStringToObject(body, "E", glon);//gps cJSON_AddStringToObject(body, "E", glon);//gps
cJSON_AddNumberToObject(head, "message_type",message); cJSON_AddNumberToObject(head, "message_type",message);
cJSON_AddItemToObject(root, "body", body); cJSON_AddItemToObject(root, "body", body);
cJSON_AddItemToObject(root, "head",head); cJSON_AddItemToObject(root, "head",head);
char *payload = cJSON_PrintUnformatted(root); char *payload = cJSON_PrintUnformatted(root);
printf("%s\n",payload); printf("%s\n",payload);
mosquitto_publish(mosq, NULL, TOPIC2, strlen(payload), payload, 0, false); mosquitto_publish(mosq, NULL, TOPIC2, strlen(payload), payload, 0, false);
...@@ -85,69 +101,77 @@ void message_3(cJSON *body,cJSON *pwm_ctrl){//message_type为3,控制pwm ...@@ -85,69 +101,77 @@ void message_3(cJSON *body,cJSON *pwm_ctrl){//message_type为3,控制pwm
cJSON *mode = cJSON_GetObjectItem(pwm_ctrl, "mode"); //mode=1 速度,mode=2 转向(unsigned char) cJSON *mode = cJSON_GetObjectItem(pwm_ctrl, "mode"); //mode=1 速度,mode=2 转向(unsigned char)
cJSON *type = cJSON_GetObjectItem(pwm_ctrl, "type"); cJSON *type = cJSON_GetObjectItem(pwm_ctrl, "type");
cJSON *val = cJSON_GetObjectItem(pwm_ctrl, "val"); //val为pwm的值 0~100(unsigned char)(unsigned char) cJSON *val = cJSON_GetObjectItem(pwm_ctrl, "val"); //val为pwm的值 0~100(unsigned char)(unsigned char)
modeTemp= mode->valueint; modeTemp= mode->valueint;
typeTemp=type->valueint; typeTemp=type->valueint;
valTemp= val->valueint; valTemp= val->valueint;
if(typeTemp==3){ if(typeTemp==3){
valTemp= val->valueint; valTemp= val->valueint;
} }
gvalt[0]=typeTemp; gvalt[0]=typeTemp;
gvalt[1]=modeTemp; gvalt[1]=modeTemp;
gvalt[2]=valTemp; gvalt[2]=valTemp;
gPwmCount = 0; gPwmCount = 0;
printf("typeTemp:%d\n",gvalt[0]); printf("typeTemp:%d\n",gvalt[0]);
printf("modeTemp:%d\n",gvalt[1]); printf("modeTemp:%d\n",gvalt[1]);
printf("valTemp:%d\n",gvalt[2]); printf("valTemp:%d\n",gvalt[2]);
if(AppExit_pin_pwm==1)speed_change(gvalt);
if(AppExit_pin_pwm==3) { if(AppExit_pin_pwm==1) speed_change(gvalt) ;
if (thread_pool_add_task(pool, ship_speed_change, gvalt) != 0) { if(AppExit_pin_pwm==3) ship_speed_change(gvalt) ;
printf("Failed to add task\n"); if(AppExit_pin_pwm == 4) PTZ_pwm_change(gvalt);
}
}
if(AppExit_pin_pwm == 4) {
PTZ_pwm_change(gvalt);
}
} }
void message_4(cJSON *body, cJSON *pin_setctrl){//message 为4时候 void message_4(cJSON *body, cJSON *pin_setctrl){//message 为4时候
cJSON *pin = cJSON_GetObjectItem(pin_setctrl, "pin"); cJSON *pin = cJSON_GetObjectItem(pin_setctrl, "pin");
cJSON *val = cJSON_GetObjectItem(pin_setctrl, "val"); //val为pwm的值 0~100(unsigned char)(unsigned char) cJSON *val = cJSON_GetObjectItem(pin_setctrl, "val"); //val为pwm的值 0~100(unsigned char)(unsigned char)
pinTemp=pin->valueint; pinTemp=pin->valueint;
valTemp= val->valueint; valTemp= val->valueint;
if(valTemp>=1){ if(valTemp>=1){
valTemp=1; valTemp=1;
} }
gPwmCount=0; gPwmCount=0;
gvalt[0]=0; gvalt[0]=0;
gvalt[1]=pinTemp; gvalt[1]=pinTemp;
gvalt[2]=valTemp; gvalt[2]=valTemp;
printf("pinTemp:%d\n",gvalt[1]); printf("pinTemp:%d\n",gvalt[1]);
printf("valTemp:%d\n",gvalt[2]); printf("valTemp:%d\n",gvalt[2]);
pin_value(gvalt[1],gvalt[2]); pin_value(gvalt[1],gvalt[2]);
} }
void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) {//消息回环函数 void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) {//消息回环函数
time_t end = time(NULL); time_t end = time(NULL);
if(gStart+5>end){ if(gStart+5>end){
printf("正在处理过期消息\n"); printf("正在处理过期消息\n");
return; return;
} }
// 确保消息有效 // 确保消息有效
if (message->payload && message->payloadlen) { // 将消息内容转换为字符串 if (message->payload && message->payloadlen) { // 将消息内容转换为字符串
char *payload_str = (char *)malloc(message->payloadlen + 1); char *payload_str = (char *)malloc(message->payloadlen + 1);
if (!payload_str) { if (!payload_str) {
return; return;
} }
memcpy(payload_str, message->payload, message->payloadlen); memcpy(payload_str, message->payload, message->payloadlen);
payload_str[message->payloadlen] = '\0'; payload_str[message->payloadlen] = '\0';
// 解析 JSON // 解析 JSON
cJSON *json = cJSON_Parse(payload_str); cJSON *json = cJSON_Parse(payload_str);
if (json == NULL) { if (json == NULL) {
fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr()); fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr());
free(payload_str); free(payload_str);
} } else {// 提取 head 对象
else {// 提取 head 对象
cJSON *head = cJSON_GetObjectItem(json, "head"); cJSON *head = cJSON_GetObjectItem(json, "head");
if (!cJSON_IsObject(head)) { if (!cJSON_IsObject(head)) {
free(payload_str); free(payload_str);
...@@ -159,21 +183,24 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag ...@@ -159,21 +183,24 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
free(payload_str); free(payload_str);
return; return;
} }
//提取 body数据段 //提取 body数据段
cJSON *body = cJSON_GetObjectItem(json, "body"); cJSON *body = cJSON_GetObjectItem(json, "body");
if (!cJSON_IsObject(body)) { if (!cJSON_IsObject(body)) {
free(payload_str); free(payload_str);
return; return;
} }
cJSON *pwm_ctrl = cJSON_GetObjectItem(body, "pwm_ctrl"); cJSON *pwm_ctrl = cJSON_GetObjectItem(body, "pwm_ctrl");
cJSON *pin_setctrl = cJSON_GetObjectItem(body, "pin_setctrl"); cJSON *pin_setctrl = cJSON_GetObjectItem(body, "pin_setctrl");
if(pwm_ctrl==NULL||pin_setctrl==NULL){ if(pwm_ctrl==NULL||pin_setctrl==NULL){
free(payload_str); free(payload_str);
return; return;
} }
gmessage_type=message_type->valueint; gmessage_type=message_type->valueint;
printf("message_type: %d\n",message_type->valueint); printf("message_type: %d\n",message_type->valueint);
switch(gmessage_type){ switch(gmessage_type){
case 1: case 1:
system("sudo reboot");//重启香橙派 system("sudo reboot");//重启香橙派
......
#include "common.h"
#include "mqtt.h"
#include "opensh.h" #include "opensh.h"
char gwebcam[254];//存放启动火狐网站命令 char gwebcam[254];//存放启动火狐网站命令
int opencamsh(){ int opencamsh(){
const char* url = "https://jywy.yd-ss.com?dev="; const char* url = "https://jywy.yd-ss.com?dev=";
char urls[50]; char urls[50];
...@@ -24,18 +24,27 @@ int opencamsh(){ ...@@ -24,18 +24,27 @@ int opencamsh(){
void refresh_cam() {//刷新页面 void refresh_cam() {//刷新页面
setenv("XAUTHORITY", "/home/orangepi/.Xauthority", 1);//加入授权 setenv("XAUTHORITY", "/home/orangepi/.Xauthority", 1);//加入授权
const char *search_command = "xdotool search --class \"chromium-browser\"";//获取窗口id const char *search_command = "xdotool search --class \"chromium-browser\"";//获取窗口id
FILE *fp = popen(search_command, "r"); FILE *fp = popen(search_command, "r");
if (!fp) { if (!fp) {
perror("Failed to execute search command"); perror("Failed to execute search command");
return; return;
} }
char window_id[32]; char window_id[32];
while (fgets(window_id, sizeof(window_id), fp) != NULL) { while (fgets(window_id, sizeof(window_id), fp) != NULL) {
window_id[strcspn(window_id, "\n")] = '\0'; window_id[strcspn(window_id, "\n")] = '\0';
char activate_command[128]; char activate_command[128];
snprintf(activate_command, sizeof(activate_command), "xdotool windowactivate --sync %s key Ctrl+r", window_id); snprintf(activate_command, sizeof(activate_command), "xdotool windowactivate --sync %s key Ctrl+r", window_id);
pid_t pid = fork(); pid_t pid = fork();
if (pid == 0) { if (pid == 0) {
// 子进程执行命令 // 子进程执行命令
execl("/bin/sh", "sh", "-c", activate_command, (char *)NULL); execl("/bin/sh", "sh", "-c", activate_command, (char *)NULL);
...@@ -48,7 +57,9 @@ void refresh_cam() {//刷新页面 ...@@ -48,7 +57,9 @@ void refresh_cam() {//刷新页面
perror("Failed to fork"); perror("Failed to fork");
} }
} }
pclose(fp); pclose(fp);
printf("recam\n"); printf("recam\n");
} }
#include "pthrpoll.h" #include "pthrpoll.h"
// 初始化任务队列 // 初始化任务队列
static int task_queue_init(TaskQueue *queue) { static int task_queue_init(TaskQueue *queue) {
queue->head = NULL; queue->head = NULL;
......
#ifndef __MAIN_H__ #include "gpio_pwm_carship.h"
#define __MAIN_H__ #include "gpio_pwm_ptz.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
#include <termios.h>
#include <wiringPi.h>
#include "gpio_pwm.h"
#include "gpio_pwm_2.h"
#include "ip.h" #include "ip.h"
#include "mqtt.h" #include "mqtt.h"
#include "delay.h" #include "delay.h"
...@@ -19,13 +7,11 @@ ...@@ -19,13 +7,11 @@
#include "INA226.h" #include "INA226.h"
#include "pthrpoll.h" #include "pthrpoll.h"
#include "heat.h" #include "heat.h"
#include "common.h"
#include "thread_main.h"
#define filename "/home/orangepi/car/master/Deviceld.txt"
pthread_t thread[4];
char buffer[30]; // 用于存储文件内容 char buffer[30]; // 用于存储文件内容
pthread_t thread[4];
//开启线程 //开启线程
int thread_start(void *AppExit(void *arg),void *Mqttbeat(void *arg),void *opensh(void *arg),void *Mqtt_onnect(void *arg)){ int thread_start(void *AppExit(void *arg),void *Mqttbeat(void *arg),void *opensh(void *arg),void *Mqtt_onnect(void *arg)){
if(pthread_create(&thread[0],NULL,AppExit,NULL)!=0) { if(pthread_create(&thread[0],NULL,AppExit,NULL)!=0) {
...@@ -44,11 +30,11 @@ int thread_start(void *AppExit(void *arg),void *Mqttbeat(void *arg),void *opensh ...@@ -44,11 +30,11 @@ int thread_start(void *AppExit(void *arg),void *Mqttbeat(void *arg),void *opensh
perror("Failed to create thread 3"); perror("Failed to create thread 3");
return 3; return 3;
} }
return 10086; return -1;
} }
void *AppExit(void *arg){ //出现意外自动停止 void *AppExit(void *arg) { //出现意外自动停止
while(1){ while(1){
Delay_Ms(0,100); Delay_Ms(0,100);
gPwmCount++; gPwmCount++;
...@@ -136,4 +122,3 @@ void thread_end() { ...@@ -136,4 +122,3 @@ void thread_end() {
pthread_join(thread[3], NULL); pthread_join(thread[3], NULL);
} }
#endif
\ No newline at end of file
#include "gpio_pwm.h" #include "gpio_pwm_carship.h"
#include <stdbool.h> #include "common.h"
// 定义 PWM 引脚的 WiringPi 编号 // 定义 PWM 引脚的 WiringPi 编号
#define PWM_PIN_SPEED 21 #define PWM_PIN_SPEED 21
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
int count_mode = 0; int count_mode = 0;
const int gpioWPi[] = {5, 6, 7, 10, 16, 20, 22, 23, 24, 25, 26, 27};//车能使用高低引脚 const int gpioWPi[] = {5, 6, 7, 10, 16, 20, 22, 23, 24, 25, 26, 27};//车能使用高低引脚
const int gpioWPi_ship[] = {5, 6, 7,10, 16, 20, 22, 23, 24,25,26, 27};//船能使用高低引脚 const int gpioWPi_ship[] = {5, 6, 7,10, 16, 20, 22, 23, 24,25,26, 27};//船能使用高低引脚
void pin_init() {//初始化引脚,车和坦克共用 void pin_init() {//初始化引脚,车和坦克共用
......
#ifndef __GPIO_PWM_H__ #ifndef __GPIO_PWM_CARSHIP_H__
#define __GPIO_PWM_H__ #define __GPIO_PWM_CARSHIP_H__
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
// 定义 PWM 引脚的 WiringPi 编号 // 定义 PWM 引脚的 WiringPi 编号
void pwm_speed();//pwm控制函数 void pwm_speed();//pwm控制函数
void midde_pwm();//车pwm停止函数 void midde_pwm();//车pwm停止函数
void speed_change(unsigned char *buf);//速度和转向值处理mqtt函数 void speed_change(unsigned char *buf);//速度和转向值处理mqtt函数
......
#include "gpio_pwm_2.h" #include "common.h"
#include "gpio_pwm_ptz.h"
#define PWM_PIN_up 21 #define PWM_PIN_up 21
#define PWM_PIN_down 2 #define PWM_PIN_down 2
// 舵机最小脉冲宽度(ms) // 舵机最小脉冲宽度(ms)
#define MIN_PULSE_WIDTH 0.5 #define MIN_PULSE_WIDTH 0.5
// 舵机最大脉冲宽度(ms) // 舵机最大脉冲宽度(ms)
#define MAX_PULSE_WIDTH 2.5 #define MAX_PULSE_WIDTH 2.5
......
#ifndef __GPIO_PWM_2_H__ #ifndef __GPIO_PWM_PTZ_H__
#define __GPIO_PWM_2_H__ #define __GPIO_PWM_PTZ_H__
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
void pwm_PTZ_hz(); void pwm_PTZ_hz();
void PTZ_pwm_init(); void PTZ_pwm_init();
void PTZ_pwm_change(unsigned char *buf) ; void PTZ_pwm_change(unsigned char *buf) ;
#endif #endif
\ No newline at end of file
#include <math.h>
#include "common.h"
#include "INA226.h" #include "INA226.h"
float currentLSB ; float currentLSB ;
......
#ifndef INA226_H #ifndef INA226_H
#define INA226_H #define INA226_H
#include <stdint.h>
#include <stdbool.h>
#include <wiringPi.h>
#include <stdio.h>
#include <math.h>
extern int sda_pin; // GPIO0 extern int sda_pin; // GPIO0
extern int scl_pin; // GPIO1 extern int scl_pin; // GPIO1
......
#include "common.h"
#include "heat.h" #include "heat.h"
#define MAX_LINE_LENGTH 20 #define MAX_LINE_LENGTH 20
......
#ifndef __HEAT_H__ #ifndef __HEAT_H__
#define __HEAT_H__ #define __HEAT_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int heat_tem();//获取CPU温度 int heat_tem();//获取CPU温度
extern char temperature[];//温度存储 extern char temperature[];//温度存储
......
#include "main.h" #include "main.h"
int Device_File_Init() {
const char *readbuf=device_inspect();
//将第3个和第4个字符提取出来
char *sub_str=malloc(3);
sub_str[0]=readbuf[2];
sub_str[1]=readbuf[3];
sub_str[2]='\0';
printf("开始初始化了");
if(strcmp(sub_str,"01")==0){//车的编码
pwm_speed(); //pwm初始化,车为停止
pin_init();
AppExit_pin_pwm=1;//车的异常停止值
free(sub_str);
}else if(strcmp(sub_str,"02")==0){//坦克的编码
pin_init();
pinMode(2, OUTPUT);//pwm引脚改为普通引脚
pinMode(21, OUTPUT);//pwm引脚改为普通引脚
digitalWrite(2, LOW);
digitalWrite(21, LOW);
AppExit_pin_pwm=2;//坦克的异常停止值
free(sub_str);
}else if(strcmp(sub_str,"03")==0){//船的编码
pwm_speed();
pin_ship_init(); //pwm初始化,车为停止
pool = thread_pool_init(1, 1);
AppExit_pin_pwm=3;//车的异常停止值
free(sub_str);
}else if(strcmp(sub_str,"04")==0){//船的编码
pin_init();
pwm_PTZ_hz();
PTZ_pwm_init();
AppExit_pin_pwm=4;//车的异常停止值
free(sub_str);
}
Delay_Ms(20,0);
TOPIC=malloc(24);
TOPIC2=malloc(24);
TOPIC3= malloc(16);
sprintf(TOPIC2,"dev2app/%s",readbuf);
sprintf(TOPIC,"app2dev/%s",readbuf);
sprintf(TOPIC3,"%s",readbuf);
printf("1:%s,2:%s,3:%s\n",TOPIC,TOPIC2,TOPIC3);
return 0;
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (wiringPiSetup() == -1) { if (wiringPiSetup() == -1) {
printf("WiringPi setup failed!\n"); printf("WiringPi setup failed!\n");
return 1; return 1;
} }
if (!INA226_begin(sda_pin, scl_pin)) { if (!INA226_begin(sda_pin, scl_pin)) {
printf("INA226 初始化失败!\n"); printf("INA226 初始化失败!\n");
return 2; return 2;
} }
if(Device_File_Init()!=0) { if(Device_File_Init()!=0) {
printf("设备文件读取初始化失败!\n"); printf("设备文件读取初始化失败!\n");
return 3; return 3;
} }
printf("识别初始化成功\n");
system("pkill chromium"); system("pkill chromium");
// 配置 INA226 // 配置 INA226
INA226_configure(sda_pin, scl_pin); INA226_configure(sda_pin, scl_pin);
// 校准 INA226 // 校准 INA226
float rShuntValue = 0.1; // 分流电阻值(单位:欧姆) float rShuntValue = 0.1; // 分流电阻值(单位:欧姆)
float iMaxExpected = 5; // 最大预期电流(单位:安培) float iMaxExpected = 5; // 最大预期电流(单位:安培)
INA226_calibrate(sda_pin, scl_pin, rShuntValue, iMaxExpected); INA226_calibrate(sda_pin, scl_pin, rShuntValue, iMaxExpected);
gStart=time(NULL);//开始时间戳 gStart=time(NULL);//开始时间戳
ipaddr();//获取ip ipaddr();//获取ip
thread_start(AppExit,Mqttbeat,opensh,Mqtt_onnect);//开启线程 thread_start(AppExit,Mqttbeat,opensh,Mqtt_onnect);//开启线程
thread_end(); thread_end();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
\ No newline at end of file
#ifndef __HAIN_H__
#define __HAIN_H__
#include "common.h"
#include "gpio_pwm_carship.h"
#include "gpio_pwm_ptz.h"
#include "ip.h"
#include "mqtt.h"
#include "delay.h"
#include "opensh.h"
#include "INA226.h"
#include "pthrpoll.h"
#include "heat.h"
#include "thread_main.h"
#include "judg.h"
#endif
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment