这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 高校专区 » 湖北理工TEA » A5组 索珈顺 魏兴顺 吕敏捷 智能小车

共9条 1/1 1 跳转至

A5组 索珈顺 魏兴顺 吕敏捷 智能小车

菜鸟
2016-04-16 13:53:15     打赏

材料已经买好,打算在循迹避障的基础功能上,加上密码开锁。

方向程序模块已经写好

#include 
#include "header\direction.h"

void forward()		//前进
{
	in_1 = 1;
	in_2 = 0;
	in_3 = 1;
	in_4 = 0;
}

void retreat()		//后退
{
	in_1 = 0;
	in_2 = 1;
	in_3 = 0;
	in_4 = 1;
}

void turn_left()	//左转
{
	in_1 = 0;
	in_2 = 1;
	in_3 = 1;
	in_4 = 0;
}

void turn_right()	//右转
{
	in_1 = 1;
	in_2 = 0;
	in_3 = 0;
	in_4 = 1;
}

void stop()			//停止
{
	in_1 = 0;
	in_2 = 0;
	in_3 = 0;
	in_4 = 0;
}





菜鸟
2016-04-16 18:00:19     打赏
2楼

助工
2016-04-17 10:20:52     打赏
3楼
不错,会使用添加代码,这个功能,虽说代码简答,但是没事,继续努力,尽量把整套系统的方案拿上来,或者有什么问题,多往论坛上发,这个论坛 做各个行业的都有,总会有咱们协会没见过的 很精妙的点子的。

助工
2016-04-17 11:12:49     打赏
4楼
介绍太单调了,把你们组的想法思路尽快加上来

菜鸟
2016-04-17 15:06:58     打赏
5楼
感觉你不能附个高电平就行呀  你要是这去控制速度,转向的速度也是要控制,最后写出来之后,每个电机的转速都有细微的差异,所以要调节占空比,你这样写,后期感觉很难写!

菜鸟
2016-04-18 19:39:32     打赏
6楼

总体方案介绍的太简单  学学置顶的那个帖子    把你的过程和思路都写出来  还有所用器材的材料的图片也要贴出来

这里有很多人看的  让大家看看你的思路 然后给你建议!!!



专家
2016-04-19 10:39:30     打赏
7楼
这就叫程序写好了?

菜鸟
2016-04-20 15:14:34     打赏
8楼
方向+PWM波调速+舵机整个模块基本完成。 别的地方调用只需修改电机pwm值和角度值即可。
#include <reg52.h>
#include "header\direction.h"

unsigned char pwm_left_value = 15;		//左电机PWM值
unsigned char pwm_right_value = 15;		//右电机PWM值
unsigned char angle_value = 12;			//舵机角度值

void forward()		//前进
{
	in_1 = 1;
	in_2 = 0;
	in_3 = 1;
	in_4 = 0;
}

void retreat()		//后退
{
	in_1 = 0;
	in_2 = 1;
	in_3 = 0;
	in_4 = 1;
}

void turn_left()	//左转
{
	in_1 = 0;
	in_2 = 1;
	in_3 = 1;
	in_4 = 0;
}

void turn_right()	//右转
{
	in_1 = 1;
	in_2 = 0;
	in_3 = 0;
	in_4 = 1;
}

void stop()			//停止
{
	in_1 = 0;
	in_2 = 0;
	in_3 = 0;
	in_4 = 0;
}

void t0_init()		//初始化T0定时器,定时0.5ms
{
	EA = 1;
	ET0 = 1;

	TMOD &= 0xf0;
	TMOD |= 0x01;
	TL0 = 0x33;
	TH0 = 0xfe;
	TF0 = 0;
	TR0 = 1;	
}

void t1_init()		//初始化T1定时器,定时0.5ms
{
	EA = 1;
	ET1 = 1;

	TMOD &= 0x0f;
	TMOD |= 0x10;
	TL1 = 0x33;
	TH1 = 0xfe;
	TF1 = 0;
	TR1 = 1;	
}

void in_t0() interrupt 1	//电机调速
{
	static unsigned char count = 0;

	TL0 = 0x33;
	TH0 = 0xfe;
	
	if(count < pwm_left_value)
		pwm_ENA_left = 1;
	else
		pwm_ENA_left = 0;
		
	if(count < pwm_right_value)
		pwm_ENB_right = 1;
	else
		pwm_ENB_right = 0;
		
	count++;
	if(count > 19)		//10ms为一个周期
		count = 0; 	
}

void in_t1() interrupt 3	//舵机角度调整	
{
	static unsigned char count = 0;
   	
	TL1 = 0x33;
	TH1 = 0xfe;

	if(count < angle_value)
		pwm_steering_engine = 1;
	else
		pwm_steering_engine = 0;
		
	count++;
	if(count > 39)		//20ms为一个周期
		count = 0; 
}

下面为头文件
#ifndef __DIRECTION_H__
#define __DIRECTION_H__

sbit in_1=P0^0;
sbit in_2=P0^1;
sbit in_3=P0^2;
sbit in_4=P0^3;
sbit pwm_ENA_left = P0^5;
sbit pwm_ENB_right = P0^6;
sbit pwm_steering_engine = P1^6;	//舵机PWM信号口 

void forward();
void retreat();
void turn_left();
void turn_right();
void stop();
void t0_init();
void t1_init();

#endif

 


 


菜鸟
2016-04-29 22:02:59     打赏
9楼

超声波避障程序已经写好了,测试了下,基本还可以,以下是源代码


#include <reg52.h>
#include "header\direction.h"

unsigned char pwm_left_value = 15;		//左电机PWM值
unsigned char pwm_right_value = 15;		//右电机PWM值
unsigned char angle_value = 12;			//舵机角度值,12为居中
unsigned char count_1 = 0;				//电机pwm周期计数
unsigned char count_2 = 0;				//舵机pwm周期计数

void forward()		//前进
{
	in_1 = 1;
	in_2 = 0;
	in_3 = 1;
	in_4 = 0;
}

void retreat()		//后退
{
	in_1 = 0;
	in_2 = 1;
	in_3 = 0;
	in_4 = 1;
}

void turn_left()	//左转
{
	in_1 = 0;
	in_2 = 1;
	in_3 = 1;
	in_4 = 0;
}

void turn_right()	//右转
{
	in_1 = 1;
	in_2 = 0;
	in_3 = 0;
	in_4 = 1;
}

void stop()			//停止
{
	in_1 = 0;
	in_2 = 0;
	in_3 = 0;
	in_4 = 0;
}

void t0_init()		//初始化T0定时器,定时0.5ms
{
	EA = 1;
	ET0 = 1;

	TMOD &= 0xf0;
	TMOD |= 0x01;
	TL0 = 0xa3;
	TH0 = 0xff;
	TF0 = 0;
	TR0 = 1;	
}

void in_t0() interrupt 1	//电机调速 + 舵机角度调整
{
	TL0 = 0xa3;
	TH0 = 0xff;
	
	if(count_1 < pwm_left_value)
		pwm_ENA_left = 1;
	else
		pwm_ENA_left = 0;
		
	if(count_1 < pwm_right_value)
		pwm_ENB_right = 1;
	else
		pwm_ENB_right = 0;
		
	count_1++;
	count_1 %= 20;		//10ms为一个周期
		
	if(count_2 < angle_value)
		pwm_steering_engine = 1;
	else
		pwm_steering_engine = 0; 
		
	count_2++;
	count_2 %= 40;		//20ms为一个周期	
}

 下面是超声波模块

#include <reg52.h>
#include "header\chaoshengbo.h"
#include "header\direction.h"
#include "header\delay.h"

unsigned char flag_s;	//距离判断
float s;	//距离变量

void delay_us(unsigned char x)
{
	while(x--);
}

void chaoshengbo_init()
{
	csb_trig = 1;	//触发脉冲,大于10us
	delay_us(10);
	csb_trig = 0;
	
	while(!csb_echo);	//等待高电平
	TR1 = 1;			//打开定时器
	while(csb_echo);	//等待低电平
	TR1 = 0;		   	//关闭定时器

	s = (TH1 * 256 + TL1) / 58;		//距离,单位厘米

	if(s >= 25)
		flag_s = 1;
	else
		flag_s = 0;

	TH1 = 0;
	TL1 = 0;
}

void chaoshengbo()
{
	unsigned char left_flag = 1;
	unsigned char right_flag = 1;

	chaoshengbo_init();
	delay(10);
	
	if(flag_s)		//如果大于25cm
		forward();
	
	if(!flag_s)		//如果小于25cm
	{
		stop();		//停车
		
		angle_value = 9;	//转向右边
		count_2 = 0;
		delay(400);		
		chaoshengbo_init();	//检测距离
		delay(20);
		right_flag = flag_s;
		
		angle_value = 12;	//居中
		count_2 = 0;
		delay(400);
		
		angle_value = 16;	//转向左边
		count_2 = 0;
		delay(400);		
		chaoshengbo_init();	//检测距离
		delay(20);
		left_flag = flag_s;
		
		angle_value = 12;	//居中
		count_2 = 0;
		delay(400);
		flag_s = 1;	
	}
	
	if(1 == right_flag && 0 == left_flag)	//右边的距离大于左边
	{
		retreat();			//后退
		delay(400);

		angle_value = 9;	//右转
		delay(400);
		turn_right();
		delay(450);	
	}
	
	if(0 == right_flag && 1 == left_flag)	//左边的距离大于右边
	{
		retreat();			//后退
		delay(400);

		angle_value = 16;	//左转
		delay(400);
		turn_left();
		delay(450);
	}
	
	if(0 == right_flag && 0 == left_flag)	//两边都不能走
	{
		retreat();	//后退
		delay(800);
	}  	
}

void t1_init()		//初始化T1定时器
{
	EA = 1;
	ET1 = 1;

	TMOD &= 0x0f;
	TMOD |= 0x10;
	TL1 = 0;
	TH1 = 0;
	TF1 = 0;
	//TR1 = 1;	
}

void in_t1() interrupt 3
{
	csb_echo = 0;
}

 

下面是主函数

#include <reg52.h>
#include "header\direction.h"
#include "header\chaoshengbo.h"

void main()
{
	t0_init();
	t1_init();
	angle_value = 12;

	while(1)
	{
		chaoshengbo();
	}
}

 


共9条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]