Commit 54c45d9e authored by 957dd's avatar 957dd

Merge branch 'feature/fort-car' into 'master'

Feature/fort car See merge request !4
parents 707083c7 75d9ae3e
#ifndef __GPIO_PWM_2_H__
#define __GPIO_PWM_2_H__
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
void pwm_PTZ_hz();
void PTZ_pwm_init();
void PTZ_pwm_change(unsigned char *buf) ;
#endif
\ No newline at end of file
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <termios.h> #include <termios.h>
#include <wiringPi.h> #include <wiringPi.h>
#include "gpio_pwm.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"
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "gpio_pwm.h" #include "gpio_pwm.h"
#include "INA226.h" #include "INA226.h"
#include "pthrpoll.h" #include "pthrpoll.h"
#include "gpio_pwm_2.h"
extern char* TOPIC ;//="app2dev/controlcar0004" extern char* TOPIC ;//="app2dev/controlcar0004"
extern char* TOPIC2 ;//="dev2app/controlcar0004" extern char* TOPIC2 ;//="dev2app/controlcar0004"
......
File mode changed from 100755 to 100644
No preview for this file type
#include "gpio_pwm_2.h"
#define PWM_PIN_up 21
#define PWM_PIN_down 2
// 舵机最小脉冲宽度(ms)
#define MIN_PULSE_WIDTH 0.5
// 舵机最大脉冲宽度(ms)
#define MAX_PULSE_WIDTH 2.5
float Initial_value=90;
static int val_change = 90;
static int val_up_down = 90;
static unsigned char change_index=0;
static unsigned char up_down_index=0;
void pwm_PTZ_hz() {
int pwm_clock = 24000000 / (50 * 1000);// 定义 PWM 频率为 50Hz
pinMode(PWM_PIN_up,PWM_OUTPUT);
pinMode(PWM_PIN_down,PWM_OUTPUT);
pwmSetClock(PWM_PIN_up,pwm_clock);//=19200*1000/(hz*2000)
pwmSetRange(PWM_PIN_up,1024);//占空比范围
pwmSetClock(PWM_PIN_down,pwm_clock);//=19200*1000/(hz*2000)
pwmSetRange(PWM_PIN_down,1024);//占空比范围
}
float calculate_pulse_width(int angle) {
if (angle < 0) {
angle = 0;
} else if (angle > 270) {
angle = 270;
}
return ((MAX_PULSE_WIDTH - MIN_PULSE_WIDTH) / 270.0) * angle + MIN_PULSE_WIDTH;
}
int calculate_duty_cycle(float pulse_width) {
// 周期(ms)
float period = 1000.0 / 50;
return (int)((pulse_width / period) * 1024);
}
void PTZ_pwm_init() {
float pulse_width = calculate_pulse_width(Initial_value);
int pulse=calculate_duty_cycle(pulse_width);
pwmWrite(PWM_PIN_up,pulse);
pwmWrite(PWM_PIN_down,pulse);
}
void PTZ_pwm_left() {
float pulse_width;
int pulse;
change_index++;
if(change_index>=1) {
change_index=0;
val_change -= 3;
if(val_change <= 0)val_change=0;
pulse_width = calculate_pulse_width(val_change);
pulse=calculate_duty_cycle(pulse_width);
}
printf("%d\n",pulse);
pwmWrite(PWM_PIN_down,pulse);
}
void PTZ_pwm_right() {
float pulse_width;
int pulse;
change_index++;
if(change_index>=1) {
change_index=0;
val_change += 3;
if(val_change >= 270)val_change=270;
pulse_width = calculate_pulse_width(val_change);
pulse=calculate_duty_cycle(pulse_width);
}
printf("%d\n",pulse);
pwmWrite(PWM_PIN_down,pulse);
}
void PTZ_pwm_up() {
float pulse_width;
int pulse;
up_down_index++;
if(up_down_index>=1) {
up_down_index = 0;
val_up_down += 3;
if(val_up_down >= 110) val_up_down=110;
pulse_width = calculate_pulse_width(val_up_down);
pulse=calculate_duty_cycle(pulse_width);
}
printf("%d\n",pulse);
pwmWrite(PWM_PIN_up,pulse);
}
void PTZ_pwm_down() {
float pulse_width;
int pulse;
up_down_index++;
if(up_down_index>=1) {
up_down_index = 0;
val_up_down -= 3;
if(val_up_down <= 70) val_up_down=70;
pulse_width = calculate_pulse_width(val_up_down);
pulse=calculate_duty_cycle(pulse_width);
}
printf("%d\n",pulse);
pwmWrite(PWM_PIN_up,pulse);
}
void PTZ_pwm_change(unsigned char *buf) {
unsigned char mode=buf[1];
unsigned char val=buf[2];
if(val != 0) {
switch(mode) {
case 1:
PTZ_pwm_up();
break;
case 2:
PTZ_pwm_down();
break;
case 3:
PTZ_pwm_right();
break;
case 4:
PTZ_pwm_left();
break;
default:
break;
}
}
}
\ No newline at end of file
...@@ -100,9 +100,10 @@ void message_3(cJSON *body,cJSON *pwm_ctrl){//message_type为3,控制pwm ...@@ -100,9 +100,10 @@ void message_3(cJSON *body,cJSON *pwm_ctrl){//message_type为3,控制pwm
if(AppExit_pin_pwm==3) { if(AppExit_pin_pwm==3) {
if (thread_pool_add_task(pool, ship_speed_change, gvalt) != 0) { if (thread_pool_add_task(pool, ship_speed_change, gvalt) != 0) {
printf("Failed to add task\n"); printf("Failed to add task\n");
}
} }
if(AppExit_pin_pwm == 4) {
PTZ_pwm_change(gvalt);
} }
} }
......
...@@ -10,10 +10,10 @@ int Device_File_Init() { ...@@ -10,10 +10,10 @@ int Device_File_Init() {
printf("开始初始化了"); printf("开始初始化了");
if(strcmp(sub_str,"01")==0){//车的编码 if(strcmp(sub_str,"01")==0){//车的编码
pwm_speed(); //pwm初始化,车为停止 pwm_speed(); //pwm初始化,车为停止
pin_init(); pin_init();
AppExit_pin_pwm=1;//车的异常停止值 AppExit_pin_pwm=1;//车的异常停止值
free(sub_str); free(sub_str);
}else if(strcmp(sub_str,"02")==0){//坦克的编码 }else if(strcmp(sub_str,"02")==0){//坦克的编码
pin_init(); pin_init();
pinMode(2, OUTPUT);//pwm引脚改为普通引脚 pinMode(2, OUTPUT);//pwm引脚改为普通引脚
...@@ -28,6 +28,12 @@ int Device_File_Init() { ...@@ -28,6 +28,12 @@ int Device_File_Init() {
pool = thread_pool_init(1, 1); pool = thread_pool_init(1, 1);
AppExit_pin_pwm=3;//车的异常停止值 AppExit_pin_pwm=3;//车的异常停止值
free(sub_str); 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); Delay_Ms(20,0);
TOPIC=malloc(24); TOPIC=malloc(24);
......
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