⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 app._c

📁 易于atmage128和ucos的手动机器人控制程序。
💻 _C
📖 第 1 页 / 共 4 页
字号:
/*
*************************************************************************************************************
*                                                uC/OS-II
*                                          The Real-Time Kernel
*
*                                                ATmega128
*
* File : APP.C
* By   : Jean J. Labrosse
*************************************************************************************************************
*/

#include  <includes.h>

/*
**************************************************************************************************************
*                                               CONSTANTS
*
* Note(s) : 1) See OS_CFG.H for the default stack size: 'OS_TASK_STK_SIZE'
**************************************************************************************************************
*/

#define  CPU_CLK_FREQ   8000000L    				   //时钟频率
   


/*
**************************************************************************************************************
*                                               VARIABLES
**************************************************************************************************************
*/

OS_STK  TaskStartStk[OS_TASK_STK_SIZE];				  //起始任务堆zai
OS_STK  Task_time_show_Stk[OS_TASK_STK_SIZE];
OS_STK  motor_move_l_Stk[OS_TASK_STK_SIZE];	 		  //左行进电机堆zai
OS_STK  motor_move_r_Stk[OS_TASK_STK_SIZE];	 		  //左行进电机堆zai
OS_STK  motor_move_m_Stk[OS_TASK_STK_SIZE];	 		  //左行进电机堆zai
OS_STK  motor_move_t_Stk[OS_TASK_STK_SIZE];	 		  //左行进电机堆zai

/*
**************************************************************************************************************
*                                           FUNCTION PROTOTYPES
**************************************************************************************************************
*/
/**********************************************任务**********************************/
     
static void  Task_Start(void *p_arg);			      //初始任务
static void  Task_time_show(void *p_arg);
static void  motor_move_l(void *p_arg);	 			  //左行进电机任务
static void  motor_move_r(void *p_arg);	 			  //右行进电机任务
static void  motor_move_m(void *p_arg);	 			  //中行进电机任务
static void  motor_move_t(void *p_arg);	 			  //中行进电机任务


/**********************************************其他函数**********************************/
static void  Task_action(void);				          //运动任务
static void  reset_action(void);				      //复位任务
static void  Task_cfg(void);				          //机器人配置
static void  IO_Init(void);						      //IO初始化
void  main(void);
void  LED_send_16b(unsigned int LED_message);		  //LED发送命令函数		  
void  intit_LED(void);								  //LED初始化函数
void  LED_show(void);		
void  psin(void);									  //给ps发送讯号
void  psout(int j);									  //接收ps讯号
void  scan(void);									  //手柄扫描	
void  data_save(void);								  //EEprom 存数据
void  data_read(void);								  //EEPROM读数据
void Init_TC(void);

unsigned char EEPROMread(int Address);			 	  //EEPROM读函数
int EEPROMwrite(int Address,unsigned char Data);	  //EEPROM写函数
	
/*********************************************邮箱**************************/

OS_EVENT *time_Box;									  //时间信号 邮箱指针
OS_EVENT *motor_l_Box;								  //左行进电机 驱动邮箱指针
OS_EVENT *motor_r_Box;								  //右行进电机 驱动邮箱指针
OS_EVENT *motor_m_Box;								  //中行进电机 驱动邮箱指针
OS_EVENT *motor_t_Box;								  //中行进电机 驱动邮箱指针

int *time_key_ptr;	  	  			  //动作信号发送消息指针 时间消息指针...
int *time_key,*motor_l_key,*motor_r_key,
*motor_m_key,*motor_t_key;			  	  			  //动作信号接收消息指针 时间消息指针....
int time_key_msg=1,motor_l_msg,motor_r_msg,
motor_m_msg,motor_t_msg;	  						   //动作信号 时间信号 底盘运动邮箱 发送信号
char err;		 		  		                  	  //错误信号存储
			 
/*******************************************全局变量*********************************/
INT8U LED_reg[8][2]={{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}};
unsigned char tx_chr;								  //tx_chr发送的字符
unsigned char rx_chr[3][8][2]; 				   		  //rx_chr接收的字符,3个,每个记录一个bit位, 0为上次键值,1为本次键值
unsigned char rx_chr_b[3][2]; 						  //接受字符,3个,低位先行,每个8bit
unsigned int  robot_model=0;		  				  // robot_model机器人工作模式,0正常模式,1记忆模式,2回放模式,3配置模式,默认值为0,robot_model_tmp模式值临时存储值
unsigned int  data_ptr=0x0001;						  //数据存储地址指针
INT8U robot_model_key=0,robot_model_tmp=0; 		                  //模式切换钥匙
INT16U time_count=0,					  //舵机控制脉冲信号计时
robot_model_dly=0,						  //测试模式下的延时计数
auto_s_2=0,auto_x_2=0,					  //自动提升 计数器
//手抓参数配置--------
hand_cy[4]={0,0,0,0},				  //存储手抓运动角度数值
hand_st_0[4]={4,7,4,7},hand_st_1[4]={6,5,6,4},hand_st_2[4]={5,6,5,6},hand_st_3[4]={4,7,4,7},hand_whe[4]={2,2,2,2}, //hand_st_1 打开电源后手抓的位置  hand_st_2 计时开始后手抓的位置 	 hand_st_3闭合后位置   hand_whe的位置 0 开 1 半开 2 闭和	3 半开	  					  //hand_cy手抓位置信号 对应手抓0-4  控制矩形波高电平时间 取值 2-10 dly_time 慢起时间 秒 1 

  

//提升电机,行程开关相关-------------------
contact_t[4][2]={{0,0},{0,0},{0,0},{0,0}};  // 0 1 2 3 行程开关的值  上次值 0  本次值1
t_model=0;  //



//电机的速度值及转向,分别对应    up3,dawnx,  up x, dawn3,   up , down ,   3  ,  x   ,左平移,右平移  建议设为 03f0以下  03ff可能会异常
INT16U speed[2][2][3][13]={{{{0x0180,0x1180,0x0180,0x1180,0x0100,0x1100,0x0000,0x0000,0x0070,0x1070,0x0000,0x03ff,0x13ff},//正常模式,第一步速度,左电机
                             {0x0180,0x1180,0x1180,0x0180,0x0000,0x0000,0x0100,0x1100,0x1070,0x0070,0x0000,0x0000,0x0000},//正常模式,第一步速度,右电机
						     {0x0000,0x0000,0x0080,0x1080,0x0080,0x1080,0x1080,0x0080,0x0250,0x1250,0x0000,0x0000,0x0000}},//正常模式,第一步速度,中电机
							
							{{0x0180,0x1180,0x0180,0x1180,0x0100,0x1100,0x0000,0x0000,0x0070,0x1070,0x0000,0x03ff,0x13ff},//正常模式,第二步速度,左电机
                             {0x0180,0x1180,0x1180,0x0180,0x0000,0x0000,0x0100,0x1100,0x1070,0x0070,0x0000,0x0000,0x0000},//正常模式,第二步速度,右电机
						     {0x0000,0x0000,0x0080,0x1080,0x0080,0x1080,0x1080,0x0080,0x0250,0x1250,0x0000,0x0000,0x0000}},//正常模式,第二步速度,中电机
							
							},
						   {{{0x0180,0x1180,0x0250,0x1250,0x0250,0x1250,0x0000,0x0000,0x0070,0x1070,0x0000,0x03ff,0x13ff},//提速模式,第一步速度,左电机
                             {0x0180,0x1180,0x1250,0x0250,0x0000,0x0000,0x0250,0x1250,0x1070,0x0070,0x0000,0x0000,0x0000},//提速模式,第一步速度,右电机
						     {0x0000,0x0000,0x0080,0x1080,0x0080,0x1080,0x1080,0x0080,0x0250,0x1250,0x0000,0x0000,0x0000}},//提速模式,第一步速度,中电机
							
							{{0x03f0,0x13f0,0x0250,0x1250,0x0250,0x1250,0x0000,0x0000,0x0080,0x1080,0x0000,0x03ff,0x13ff},//高速模式,第二步速度,左电机
                             {0x03d0,0x13d0,0x1250,0x0250,0x0000,0x0000,0x0250,0x1250,0x1080,0x0080,0x0000,0x0000,0x0000},//高速模式,第二步速度,右电机
						     {0x0000,0x0000,0x0080,0x1080,0x0080,0x1080,0x1080,0x0080,0x02a0,0x12a0,0x0000,0x0000,0x0000}},//高速模式,第二步速度,中电机
							
							}};
//电机启动方式,分别对应  up3,dawnx, up x, dawn3, up , down , 3 ,  x  ,左平移,右平移,停 0方式立即加电,1方式逐步加电
INT8U  heading[2][2][13]={{{0,     0,    0,     0,   0,     0,  0,    0,     0,     0 ,0 ,0,0},    //正常模式,第一步,启动模式
	   					   {0,     0,    0,     0,   0,     0,  0,    0,     0,     0 ,0 ,0,0}},   //正常模式,第二步,启动模式
						  
						  {{2,     2,    0,     0,   0,     0,  0,    0,     0,     0 ,0 ,0,0},	   //高速模式,第一步,启动模式
	   					   {2,     2,    0,     0,   1,     1,  0,    0,     0,     0 ,0 ,0,0}}};   //高速模式,第二步,启动模式
//启动过程时间,分别对应   up3,dawnx, up x, dawn3, up , down , 3 ,  x  ,左平移,右平移,单位是秒,只可取整数,最好不要取0
INT8U dly_time[2][2][13]={{{1,     1,    1,     1,   1,     1,  1,    1,     1,     1  ,1 ,1 ,1},    //正常模式,第一步
	   					   {1,     1,    1,     1,   1,     1,  1,    1,     1,     1  ,1 ,1 ,1}},   //正常模式,第二步
						  
						  {{1,     1,    1,     1,   1,     1,  1,    1,     1,     1  ,1 ,1 ,1},	   //高速模式,第一步,
	   					   {1,     1,    1,     1,   2,    2,  1,    1,     1,     1   ,1 ,1 ,1}}};   //高速模式,第二步,

INT8U speed_model=1;     //默认速度模式
/*
**************************************************************************************************************
*                                                MAIN
*
* Note(s): 1) You SHOULD use OS_TASK_STK_SIZE (see OS_CFG.H) when setting OSTaskStkSize prior to calling 
*             OSInit() because OS_TASK_IDLE_STK_SIZE and OS_TASK_STAT_STK_SIZE are set to this value in
*             OS_CFG.H.
**************************************************************************************************************
*/

void  main (void)
{
  	OSTaskStkSize     = OS_TASK_STK_SIZE;             //Setup the total stack size                   
    OSTaskHardStkSize = OS_TASK_HARD_STK_SIZE;  	  //Setup the hardware stack size              
    OSInit();                                   	  //Initialize "uC/OS-II, The Real-Time Kernel"     
	time_Box		  =OSMboxCreate((void*)0);		  //建立时间消息邮箱
	motor_l_Box		  =OSMboxCreate((void*)0);		  //建立左电机消息邮箱
	motor_r_Box		  =OSMboxCreate((void*)0);		  //建立右电机消息邮箱
	motor_m_Box		  =OSMboxCreate((void*)0);		  //建立中电机消息邮箱
	motor_t_Box		  =OSMboxCreate((void*)0);		  //建立中电机消息邮箱
	
	OSTaskCreate(Task_Start, (void *)0, (OS_STK *)&TaskStartStk[OS_TASK_STK_SIZE - 1], 0);
	OSStart();                                  	  //Start multitasking (i.e. give control to uC/OS-II) 
}

/*
*********************************************************************************************************
*                                          STARTUP TASK
起始任务,主任务
手柄的扫描,模式切换,键值改变判别并给Task_action发送消息
*********************************************************************************************************
*/

static void  Task_Start (void *p_arg)
{
    INT16S cpu_usage_1=0x0000,cpu_usage_10=0x0000;	  //cpu使用百分率 个位和十位
    p_arg = p_arg;                                    //Prevent compiler warnings                         
	Init_TC();									 	  //定时计数器初始化           
    IO_Init();                                 	 	  // Initialize the I/Os                            
    intit_LED();								 	  //数码管初始化
	OSStatInit();									  //任务统计初始化	
	OSTaskCreate(Task_time_show, (void *)0, (OS_STK *)&Task_time_show_Stk[OS_TASK_STK_SIZE - 1], 2); //创建时间显示任务 优先级2
	OSTaskCreate(motor_move_l, (void *)0, (OS_STK *)&motor_move_l_Stk[OS_TASK_STK_SIZE - 1], 3); //左电机行进任务 优先级3
	OSTaskCreate(motor_move_r, (void *)0, (OS_STK *)&motor_move_r_Stk[OS_TASK_STK_SIZE - 1], 4); //左电机行进任务 优先级3
	OSTaskCreate(motor_move_m, (void *)0, (OS_STK *)&motor_move_m_Stk[OS_TASK_STK_SIZE - 1], 5); //左电机行进任务 优先级3
	OSTaskCreate(motor_move_t, (void *)0, (OS_STK *)&motor_move_t_Stk[OS_TASK_STK_SIZE - 1], 6); //左电机行进任务 优先级3
	//开电后手抓全开-----------
	hand_cy[0]=hand_st_1[0];
	hand_cy[1]=hand_st_1[1];
	hand_cy[2]=hand_st_1[2];
	hand_cy[3]=hand_st_1[3];
	while (TRUE) 
	{ 
        scan();	 								 	  //手柄信号扫描

//系统复位---------------------------------------------------------------------------
	if(rx_chr[1][0][1]&((~rx_chr[2][4][0])&rx_chr[2][4][1]))	 	  //复位键按下(防颤)
		{
			robot_model_key=1;	   				  //复位打开
			OSTaskDel(2);							  //删除时间任务
			//LED_send_16b(0x054e);	   			  //显示刷新
		}
	
	if(robot_model_key!=0)reset_action();	      //如果复位模式
	else Task_action();  				 	   	  //否则正常运行


//手柄信号改变检查开始计时------------------------------------------------------------------------

		 if(((rx_chr_b[1][1]&0xd0)!=(rx_chr_b[1][0]&0xd0))||(rx_chr_b[2][1]!=rx_chr_b[2][0]))
		  {
		     *time_key_ptr=time_key_msg;					  
		   	 OSMboxPost(time_Box,time_key_ptr);		  //发送时间函数邮件
		  }
		  
		  	
//cpu百分率显示----------------------------------------------------------------------------------
   /*
        cpu_usage_1   =(OSCPUUsage%10)|0x0800;		  //个位 			
		cpu_usage_10  =(OSCPUUsage/10)|0x0700;		  //十位
		LED_send_16b(cpu_usage_1);
		LED_send_16b(cpu_usage_10);
		OCR1B=0x03ff;
		*/
//延时0.1s--------------------------------------------------------------------------	

     OSTimeDly(OS_TICKS_PER_SEC/10);				  // 0.1s
	 }
	 
    }
/*
*********************************************************************************************************
*                                              action 
										控制机器人对于手柄响应的任务
*********************************************************************************************************
*/

static void  Task_action(void)
{

//时间复位--------------
 		 if((~rx_chr[1][0][0])&rx_chr[1][0][1])
		 {
		     OSTaskDel(2);	
			 OSTaskCreate(Task_time_show, (void *)0, (OS_STK *)&Task_time_show_Stk[OS_TASK_STK_SIZE - 1], 2); //创建时间显示任务 优先级2
		 }


//速度切换部分--------------------------------------------------------

		  if((~rx_chr[1][7][0])&rx_chr[1][7][1])     // 切换到正常速度
		  {
		     if(speed_model==1)  speed_model=0;      //如果是低速档
			 else if(speed_model==0) speed_model=1;	 //如果是高速速档 
		  }
		  
		  if(speed_model==0) 
		  {
		      LED_send_16b(0x0500);	 //速度模式0  显示
		  	  LED_send_16b(0x0600);
			  LED_send_16b(0x0700);
			  LED_send_16b(0x0800);
		 }
		  else if(speed_model==1)
		  {
		      LED_send_16b(0x0501);//速度模式1  显示
			  LED_send_16b(0x0601);
			  LED_send_16b(0x0701);
			  LED_send_16b(0x0801);
		  }
//行走部分(按2个轮子)------------------------------------------------------------------

		  
		  if(rx_chr[1][4][1]&rx_chr[2][4][1])		  // up 3
		  	{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -