智能车制作

 找回密码
 注册

扫一扫,访问微社区

查看: 4993|回复: 17
打印 上一主题 下一主题

[控制类] 现金

[复制链接]

0

主题

47

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
389
威望
276
贡献
71
兑换币
34
注册时间
2009-2-11
在线时间
21 小时
跳转到指定楼层
1#
发表于 2009-3-31 18:58:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
2贡献
请问谁能给我点单片机(9S12)控制的源程序?很感谢!

0

主题

150

帖子

0

精华

高级会员

Rank: 4

积分
749
QQ
威望
361
贡献
238
兑换币
0
注册时间
2009-3-20
在线时间
75 小时
2#
发表于 2009-3-31 19:11:45 | 只看该作者
你出多少“现金”哇

下载报告去,看看大家报告后面的程序,多看一些,或许能捕捉强队无意间在程序中透露的思想
回复

使用道具 举报

0

主题

47

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
389
威望
276
贡献
71
兑换币
34
注册时间
2009-2-11
在线时间
21 小时
3#
 楼主| 发表于 2009-4-6 16:39:44 | 只看该作者
谢谢!最近正在看,但还有不懂的地方,望高手能及时给予援助!
回复

使用道具 举报

0

主题

242

帖子

0

精华

高级会员

Rank: 4

积分
925
QQ
威望
451
贡献
382
兑换币
0
注册时间
2008-11-28
在线时间
46 小时
4#
发表于 2009-4-7 09:58:00 | 只看该作者
顶烙铁头,哈哈,哪有你这么问的,源程序那可是机密啊,你问点小问题那倒可以,或者程序中的错误,你却直接要程序!!!
回复

使用道具 举报

0

主题

47

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
389
威望
276
贡献
71
兑换币
34
注册时间
2009-2-11
在线时间
21 小时
5#
 楼主| 发表于 2009-4-15 16:57:06 | 只看该作者
绝对的新手!呵呵!所以一切从零数!问的都是些直白的问题!
回复

使用道具 举报

0

主题

242

帖子

0

精华

高级会员

Rank: 4

积分
925
QQ
威望
451
贡献
382
兑换币
0
注册时间
2008-11-28
在线时间
46 小时
6#
发表于 2009-4-16 12:56:17 | 只看该作者
/*******************************************************************
****
** Copyright (c) Smartcar Lab of USTB
**----------------------------------------------------------------------
** Module Name: main.c
** Module Date: 03/08/2008
** Module Auth: shiyaohong
** Description: main of system
**----------------------------------------------------------------------
** Revision History:
** Date: month/day/year
** Notes:
********************************************************************
***/
/*--------------- I N C L U D E S ------------------------------------*/
#include "main.h"
/*--------------- M A C R O S ----------------------------------------*/
/*--------------- D E F I N I T I O N --------------------------------*/
/*--------------- D E C L A R A T I O N ------------------------------*/
/*--------------- F U N C T I O N S ----------------------------------*/
/*******************************************************************
**
Function Name: Main
Description: First Start Function
Inputs: None
Outputs: None
NOTES:
********************************************************************
6
*/
void main(void)
{
SystemInit();
SystemStart();
}
模块化程序列表
Port.h/Port.c //单片机IO 口初始化
PLL.h/PLL.c//锁相环时钟初始化
SCI.h/SCI.c //串口初始化
IRQ.h/IRQ.c//外部中断初始化
PWM.h/PWM.c//PWM 模块初始化
ECT.h/ECT.c//ECT 模块初始化
PID.h/PID.c//PID 函数定义
Board.h //电路板版本号、板级信息定义
Config.h //相关工程参数定义
S12Def.h//常用宏定义
S12Types.h//与平台无关的变量定义
以上为模块话程序代码,并无特别之处,在此略。
Schedule.h
#ifndef SCHEDULE_H
#define SCHEDULE_H
/*--------------- I N C L U D E S ------------------------------------*/
#include "../Heads/Global.h"
//#include "../Drivers/Port.h"
#include "../Drivers/PLL.h"
#include "../Drivers/SCI.h"
#include "../Drivers/ECT.h"
#include "../Drivers/PH7_Inter.h"
#include "../Drivers/PJ7_Inter.h"
#include "../Drivers/IRQ.h"
#include "../Drivers/PWM.h"
#include "../Drivers/Timer.h"
7
//#include "../Drivers/HD7279.h"
/*-------------- - M A C R O S ----------------------------------------*/
#define Present_OddEven_state PORTB_BIT0
#define DIR_S PORTB_BIT1
#define Sending_S PORTA_BIT4
#define DIR_B (PTP = PTP & 0XDF)
#define DIR_F (PTP = PTP | 0X20)
#define Led1_ON (PORTA_BIT0 = 0)
#define Led1_OFF (PORTA_BIT0 = 1)
#define Led2_ON (PORTA_BIT1 = 0)
#define Led2_OFF (PORTA_BIT1 = 1)
#define Led3_ON (PORTA_BIT2 = 0)
#define Led3_OFF (PORTA_BIT2 = 1)
#define Led4_ON (PORTA_BIT3 = 0)
#define Led4_OFF (PORTA_BIT3 = 1)
#define Button_0 ( PORTA_BIT5)
#define Button_1 ( PORTA_BIT6)
#define VIDEO_START_LINE 50
#define MAX_VIDEO_LINE 250
#define MAX_VIDEO_POINT 210
#define MAX_VIDEO_USEDLINE 50
#define MAX_INTER_POINT 5
#define Set_Mid_Point 108
#define Set_Max_Point 212
#define Set_Min_Point 4
#define Odd 1 //奇场
#define Even 0 //偶场
#define LIMIT 80
#define MAX_BLACK_NUM 4
#define MAX_STRATEGY 8
//-----------------------------------------------
extern INT8U Last_OddEven_state = Odd;
extern INT32S Set_Lowest_Speed = 100;
extern INT32S Set_Highest_Speed = 140;
extern INT8U Set_Basic_Speed = 70;
extern INT8U Running_Enable=0;
8
extern INT8U Flag_up=0 ;//
extern INT8U Flag_down=0 ;//
extern INT16U v_line = 0; //
extern INT8U v_WhiteBlock ;
extern INT8S v_LeftWhiteBlockLine ;
extern INT8S v_RightWhiteBlockLine ;
extern INT8U v_Last_LeftWhiteBlockLine ;
extern INT8U v_Last_RightWhiteBlockLine ;
extern INT8U v_ValidStartline;
extern INT8U v_FirstStartline;
extern INT8U v_NotValidStartline;
extern INT32U v_StopPulse;
extern INT8U Flag_Stop;
extern INT8U Flag_Pulse;
extern INT8S Flag_Left_Black;
extern INT8S Flag_Right_Black;
extern INT8U v_pre_speed;
extern INT8U Flag_Startline;
extern INT8U Flag_StartValid;
extern INT8U v_point_up[MAX_VIDEO_LINE][MAX_INTER_POINT] ;//
extern INT8U v_point_down[MAX_VIDEO_LINE][MAX_INTER_POINT] ; //
extern INT8U
v_point_used_up[MAX_VIDEO_USEDLINE][MAX_INTER_POINT] ; //
extern INT8U
v_point_used_down[MAX_VIDEO_USEDLINE][MAX_INTER_POINT] ;//
extern INT16S v_slope_2[50]; //
extern INT8U v_slope;
extern INT8U v_slope_average; //
extern INT8S v_slope_last;//
extern INT8S v_slope_now; //
extern INT32S v_slope_sum; //
extern INT8S v_Max_Slope_line;
extern INT8S v_Min_Slope_line;
extern INT8U v_slope_valid_line ;//
extern INT8S F_LeftStraightLine;
extern INT8S F_RightStraightLine;
extern INT8S F_LeftSlopAverage;
extern INT8S F_RightSlopAverage;
extern INT8U v_ValidLine ;
extern INT8U v_TotleValidLine ;
9
extern INT8S v_Max_Slope_line;
extern INT8S v_SlopeUnvalid_line;
extern INT8S v_Min_Slope_line;
extern INT8U v_slope_valid_line ;
extern INT8U v_straight_valid_line ;
extern INT8U v_straight_valid ;
extern INT8U v_straight_line_lost ;
extern INT8S F_SlopAverageTwo ;
extern INT8S F_SlopAverageOne ;
extern INT8U F_StraightTwo ;
extern INT8U v_LastLineCenter;
extern INT32S v_slopeOneAverage;
extern INT32S v_LastslopeOneAverage;
extern INT32S v_slopeTwoSum;
extern INT8U Flag_Stop_s;
extern INT8U Flag_Stop_Time_s;
extern INT8U Flag_Stop_Time;
extern INT32U v_WholePulse;
extern INT8S F_WholePulse;
extern INT8U F_WholePulse_S;
extern INT8U F_WholePulse_SS;
extern INT8U F_RunTest;
///////////////////
extern INT8S S_flag = 0;
extern INT8S S_left = 0;
extern INT8S S_right = 0;
extern INT8S S_straight = 0;
extern INT8U No_line_flag = 0;
extern INT8U Straight_flag = 0;
//============ ======================================
extern INT8S n;
extern INT8U m;
extern INT8U BackLineValidFlag;
extern INT8U l_BlackStartDot;
extern INT8U k;
extern INT8U k2;
extern INT8U NextFlag;
extern INT16S g_SearchStart;
extern INT16S g_SearchEnd;
10
extern INT16S g_SearchBottomLineEnd;
extern INT8U g_SearchDirection;
extern INT16S g_BottomMiddle;
extern INT8U g_BlackPoint[2][MAX_BLACK_NUM];
extern INT8U cnt;
extern INT8S g_BottomValidLine;
extern INT8S g_width[MAX_VIDEO_USEDLINE];
extern INT16S g_BlackPositionCenter[MAX_VIDEO_USEDLINE];
extern INT16S g_SolutedBlackPositionCenter[MAX_VIDEO_USEDLINE];
extern INT16S g_PointCenter[250];
extern INT16S g_LastPointWidth;
extern INT16S g_LastPointCenter;
extern INT16S g_UNvalidPointCenter ;
extern INT32S g_center;
extern INT8U g_ValidLine;
extern INT8U g_FormerValidLine;
extern INT16U g_BlackLineTotal;
extern INT16S g_error;
extern INT8S ValidLineError;
extern INT8S g_WidthError;
extern INT8S g_MinWidth;
extern INT16S g_MaxWidth;
extern INT32S g_DirectionControl;
extern INT16S g_DirectionControlError;
extern INT16S g_MotorControl;
extern INT8U g_MotorValidLine;
extern INT32S g_MotorControlLine;
extern INT32S g_DirectionControlLine;
extern INT16U g_coeff[MAX_VIDEO_USEDLINE];
extern INT8U g_DirectFlag;
extern INT8U g_CurveFlag;
extern INT8U g_UnValidLine;
extern INT8S g_BottomLatestValidLine;
extern INT8U l_tmp ;
/////////////////////////////////////////////////////////////////////////
/*Parameter Select*/
#define SPEED_KPVALUE 4000
#define SPEED_KIVALUE 1800
#define SPEED_KDVALUE 0
11
#define VV_MAX 700000
#define VV_MIN -(700000)
#define VV_DEADLINE 0x02
#define LOCA_KPVALUE 2000
#define LOCA_KIVALUE 0
#define LOCA_KDVALUE 0
#define LOCA_MAX 290000
#define LOCA_MIN -290000
#define LOCA_DEADLINE 0x01
extern struct PID
{
//servo angle PID parameter
signed long loca_Ref; //位置PID,位移设定值
signed long loca_FeedBack; //位置PID,位移反馈值,当前位置
signed long loca_PreError; // 位置PID , 前一次, 位移误差,ui_Ref -
FeedBack
signed long loca_PreIntegral; // 位置PID , 前一次, 位移积分项,
ui_PreIntegral+ui
signed int loca_Kp; //位置PID,比例系数
signed int loca_Ki; //位置PID,积分系数
signed int loca_Kd; //位置PID,微分系数
signed long loca_PreU; //电机控制输出值
//driver motor PID parameter
signed long vi_Ref;
signed int vi_FeedBack;
signed int vi_PreError; //vi_Ref - vi_FeedBack
signed int vi_PreDerror; //d_error-PreDerror;
signed long v_Kp;
signed int v_Ki;
signed int v_Kd;
signed long vl_PreU;
};
struct PID sPID;
12
/*--------------- D E C L A R A T I O N ------------------------------*/
/*PID fuctions*/
signed long int loca_PIDCalc(struct PID *pp );
signed int V_PIDCalc(struct PID *pp );
void ServoPIDInit(void);
void SpeedPIDInit (void);
void SystemInit(void);
void SystemStart(void);
void Running(void);
void ParameterSelect(void);
void ShortDelay(void);
void LongDelay(void);
#endif /* SCHEDULE_H */
Schedule.c
/*******************************************************************
**
Function Name:SystemInit
Description: Init Sysmtem
Inputs: None
Outputs: None
NOTES:
********************************************************************
*/
//
void SystemInit(void)
{
INT8U i;
INT8U j;
DisableInterrupts; //
for(i = 0; i < MAX_VIDEO_USEDLINE; i++)
{
g_coeff[i] = (MAX_VIDEO_USEDLINE - i) * (MAX_VIDEO_USEDLINE
- i) * (MAX_VIDEO_USEDLINE - i);//
}
for(i = 0;i < MAX_VIDEO_LINE;i ++)
13
{
for(j = 0; j < 5; j++)
{
v_point_up[i][j] = 0; //
v_point_down[i][j] = 0; //
}
}
v_ValidStartline = 0; //
F_RunTest = 1;
v_StopPulse = 0;//
Flag_Stop = 0; //
Flag_Startline = 0;//
g_DirectFlag = 0; //
g_CurveFlag = 0; //
Flag_Stop_s = 2;
F_WholePulse_S = 29;
g_BottomMiddle = Set_Mid_Point; //
g_DirectionControl = Set_Mid_Point; //
g_BottomValidLine = MAX_VIDEO_USEDLINE - 1; //
g_ValidLine = MAX_VIDEO_USEDLINE; //
g_FormerValidLine = MAX_VIDEO_USEDLINE; //
INTCR_IRQEN=0;//外部中断IRQ 禁止
DDRA=0X0F;
PORTA=0XF0;
DDRP=0XFF;
PTP=0XFF;
DIR_F;
DDRB = 0X00;
PORTB = 0XFF;
DDRT_DDRT0 = 0; //码盘计数
PLLInit();
SCI0Init();
ECTInit();
Timer_init();
回复

使用道具 举报

0

主题

242

帖子

0

精华

高级会员

Rank: 4

积分
925
QQ
威望
451
贡献
382
兑换币
0
注册时间
2008-11-28
在线时间
46 小时
7#
发表于 2009-4-16 12:56:39 | 只看该作者
PH7_init();
IRQInit();
PWM67Init();
PWM01Init();
ServoPIDInit();
SpeedPIDInit();
14
}
/*******************************************************************
**
Function Name:SystemStart
Description: Start Sysmtem
Inputs: None
Outputs: None
NOTES:
********************************************************************
*/
void SystemStart(void)
{
ParameterSelect();
EnableInterrupts; //开中断
Led1_ON;
Led3_ON;
for(;;)
{
if(Running_Enable)
{
Running();
}
else
{
;
}
//Led2_OFF;
}
}
/*******************************************************************
**
Function Name:IRQ_ISR
Description: IRQ interrupt service function,read CMOS camera
Inputs: None
Outputs: None
NOTES:
********************************************************************
**/
15
#pragma CODE_SEG __NEAR_SEG NON_BANKED /* Interrupt section for this
module. Placement will be in NON_BANKED area. */
__interrupt void IRQ_ISR(void) //IRQ 脚外部中断行同步中断输入
{
if( Present_OddEven_state == Odd)
{ //Led3_ON;
MCCNT = 0x00FF;
Flag_down = 0;
Flag_up = 0;
}
else
{
Running_Enable = 1;
}
if(Last_OddEven_state != Present_OddEven_state)
{
v_line = 0; //行位置清零
v_pre_speed = PACN10;
PACN10 = 0;
v_WholePulse += v_pre_speed;
if(v_WholePulse > 2187)
{
F_WholePulse ++;
v_WholePulse = 0;
}
if((F_WholePulse > F_WholePulse_S) && (F_RunTest == 2))
{
Flag_Stop = 1;
}
if((F_RunTest == 1) && (F_WholePulse > 20)) //测试赛道长度
{
switch((F_WholePulse-20) >> 1)
{
case 0ed1_OFF;Led2_OFF;Led3_OFF;Led4_OFF; break; //
111
case 1:Led1_ON;Led2_OFF;Led3_OFF;Led4_OFF; break; //
001
case 2:Led1_OFF;Led2_ON;Led3_OFF;Led4_OFF; break; //
001
16
case 3:Led1_ON;Led2_ON;Led3_OFF;Led4_OFF; break; //
010
case 4:Led1_OFF;Led2_OFF;Led3_ON; Led4_OFF; break; // 011
case 5:Led1_ON;Led2_OFF;Led3_ON;Led4_OFF; break; // 100
case 6:Led1_OFF;Led2_ON;Led3_ON;Led4_OFF; break; //
101
case 7:Led1_ON;Led2_ON;Led3_ON;Led4_OFF; break; //
110
case 8:Led1_OFF;Led2_OFF;Led3_OFF;Led4_ON;break; //
111
case 9:Led1_ON;Led2_OFF;Led3_OFF;Led4_ON;break; // 001
case 10:Led1_OFF;Led2_ON;Led3_OFF;Led4_ON; break; // 001
case 11:Led1_ON;Led2_ON;Led3_OFF;Led4_ON; break; // 010
case 12:Led1_OFF;Led2_OFF;Led3_ON; Led4_ON; break; // 011
case 13:Led1_ON;Led2_OFF;Led3_ON;Led4_ON; break; // 100
case 14:Led1_OFF;Led2_ON;Led3_ON;Led4_ON; break; //
101
case 15:Led1_ON;Led2_ON;Led3_ON;Led4_ON; break; //
110
default:break;
}
}
if(Flag_Pulse == 1)
{
v_StopPulse += v_pre_speed;
if(v_StopPulse > 3000)
{
Flag_Pulse = 0;
v_StopPulse = 0;
if((Flag_Startline >= Flag_Stop_s) && (F_RunTest == 3))
{
if(F_WholePulse > F_WholePulse_S)
{
Flag_Stop = 1;
}
else
{
Flag_Startline --;
}
17
}
}
}
if(DIR_S == 0)
{
sPID.vi_FeedBack = v_pre_speed ;
}
else
{
sPID.vi_FeedBack = -v_pre_speed ;
}
if(V_PIDCalc(&sPID) > 0)
{
DIR_F;
PWMDTY01 = V_PIDCalc(&sPID);
//PWMDTY01 = 400;
}
else
{
DIR_B;
PWMDTY01 = -V_PIDCalc(&sPID);
}
Last_OddEven_state = Present_OddEven_state;
}
v_line ++;
}
/*******************************************************************
**
Function Name:Running
Description:
Inputs: None
Outputs: None
NOTES:
********************************************************************
**/
18
void Running(void)
{
INT16S i,j;
DisableInterrupts;
//初始化数组
for(i = 0;i < MAX_VIDEO_LINE;i = i + 5)//从250 行中取50 行
{
for(j = 0;j < MAX_INTER_POINT;j ++)
{
v_point_used_down[i / 5][j] = v_point_down[i][j];
v_point_used_up[i / 5][j] = v_point_up[i][j];
}
}
for(n = 0; n < MAX_VIDEO_USEDLINE; n ++)//对中心点赋初值
{
g_BlackPositionCenter[n] = 250;
g_width[n] = 0;
}
g_SearchStart = g_BottomMiddle - LIMIT;
g_SearchEnd = g_BottomMiddle + LIMIT;
v_LastLineCenter = g_BottomMiddle;
if(g_SearchStart < Set_Min_Point + 2)
{
g_SearchStart = Set_Min_Point + 2;
}
else if(g_SearchStart > Set_Mid_Point - 20)
{
g_SearchStart = Set_Mid_Point - 20;
}
if(g_SearchEnd > Set_Max_Point - 2)
{
g_SearchEnd = Set_Max_Point - 2;
}
else if(g_SearchEnd < Set_Mid_Point + 20)
{
g_SearchEnd = Set_Mid_Point + 20;
}
19
//
/*
if((g_BottomMiddle > 148) || (g_BottomMiddle < 68))
{
g_SearchBottomLineEnd = g_BottomValidLine - 15;
}
else
{
g_SearchBottomLineEnd = g_BottomValidLine - 10;
}
*/
//g_SearchBottomLineEnd = g_BottomValidLine - 15;
if((g_ValidLine == 0) || (g_BottomValidLine < 58) || (g_BottomValidLine >
158) )
{
g_SearchBottomLineEnd = g_BottomValidLine - 15;
}
else if((g_BottomValidLine < 78) || (g_BottomValidLine > 128))
{
g_SearchBottomLineEnd = g_BottomValidLine - 20;
}
else
{
g_SearchBottomLineEnd = g_BottomValidLine - 25;
}
if(g_SearchBottomLineEnd < 20)
{
g_SearchBottomLineEnd = 20;
}
for(n = MAX_VIDEO_USEDLINE - 1; n >= g_SearchBottomLineEnd; n --)
//`5```````````````````````````14
{
for(k = 0;k < MAX_BLACK_NUM;k ++)
{
g_BlackPoint[0][k] = 250;
g_BlackPoint[1][k] = 0;
}
cnt = 0;
for(i = 0;i < MAX_INTER_POINT;i ++)
20
{
if((v_point_used_down[n][i + 1] >= g_SearchStart) &&
(v_point_used_down[n][i + 1] <= g_SearchEnd) && (v_point_used_up[n][i] >=
g_SearchStart) && (v_point_used_up[n][i] <= g_SearchEnd) &&
(v_point_used_up[n][i] - v_point_used_down[n][i + 1] <= 40) &&
(v_point_used_up[n][i] - v_point_used_down[n][i + 1] >= 7))
{
g_BlackPoint[0][cnt] = (v_point_used_up[n][i] +
回复

使用道具 举报

0

主题

242

帖子

0

精华

高级会员

Rank: 4

积分
925
QQ
威望
451
贡献
382
兑换币
0
注册时间
2008-11-28
在线时间
46 小时
8#
发表于 2009-4-16 12:57:00 | 只看该作者
v_point_used_down[n][i + 1]) / 2;//记录中心点
g_BlackPoint[1][cnt] = v_point_used_up[n][i] -
v_point_used_down[n][i + 1]; //
if(cnt == 0) //
{
//g_BottomValidLine = n;
g_BlackPositionCenter[n] = g_BlackPoint[0][cnt]; //
g_width[n] = g_BlackPoint[1][cnt];
}
else // (1<n<=4)
{
if(g_BlackPoint[0][cnt] >= v_LastLineCenter) //
{
g_BlackPositionCenter[n] = g_BlackPoint[0][cnt]; //该行
黑线中心点重新赋值
g_width[n] = g_BlackPoint[1][cnt];
}
else if(g_BlackPositionCenter[n] - v_LastLineCenter >
v_LastLineCenter - g_BlackPoint[0][cnt])
{ //
g_BlackPositionCenter[n] = g_BlackPoint[0][cnt];//该行
黑线中心点重新赋值
g_width[n] = g_BlackPoint[1][cnt];
}
}
cnt ++;
// i = 4;//
}
}
21
if(cnt > 0)
{
g_BottomValidLine = n;
//g_BottomLatestValidLine = n;
v_LastLineCenter = g_BlackPositionCenter[n];
n = 0;
}
}
NextFlag = 0;
if(g_BlackPositionCenter[g_BottomValidLine] != 250)
{ NextFlag ++;
// v_LastLineCenter = g_BlackPositionCenter[g_BottomValidLine];
//
for(n = g_BottomValidLine - 1; n >= g_BottomValidLine - 5; n --)
//`5```````````````````````````14
{
g_SearchStart = v_LastLineCenter - 30;
g_SearchEnd = v_LastLineCenter + 30;
if(g_SearchStart < 5)
{
g_SearchStart = 5;
}
if(g_SearchEnd > 201)
{
g_SearchEnd = 201;
}
for(k = 0;k < MAX_BLACK_NUM;k ++)
{
g_BlackPoint[0][k] = 250; //黑块中心
g_BlackPoint[1][k] = 0; //对应的宽度
}
cnt = 0;
for(i = 0;i < MAX_INTER_POINT;i ++)
{
if((v_point_used_down[n][i + 1] >= g_SearchStart) &&
(v_point_used_down[n][i + 1] <= g_SearchEnd) && (v_point_used_up[n][i] >=
g_SearchStart) && (v_point_used_up[n][i] <= g_SearchEnd) &&
(v_point_used_up[n][i] - v_point_used_down[n][i + 1] <= 40) &&
(v_point_used_up[n][i] - v_point_used_down[n][i + 1] >= 7))
22
{
g_BlackPoint[0][cnt] = (v_point_used_up[n][i] +
v_point_used_down[n][i + 1]) / 2;//记录中心点
g_BlackPoint[1][cnt] = v_point_used_up[n][i] -
v_point_used_down[n][i + 1]; //记录黑线宽度
if(cnt == 0) //
{
g_BlackPositionCenter[n] = g_BlackPoint[0][cnt]; //该行
黑线中心点重新赋值
g_width[n] = g_BlackPoint[1][cnt];
}
else // (1<n<=4)
{
if(g_BlackPoint[0][cnt] >= v_LastLineCenter) //
{
g_BlackPositionCenter[n] = g_BlackPoint[0][cnt]; //
该行黑线中心点重新赋值
g_width[n] = g_BlackPoint[1][cnt];
}
else if(g_BlackPositionCenter[n] - v_LastLineCenter >
v_LastLineCenter - g_BlackPoint[0][cnt])
{ //
g_BlackPositionCenter[n] = g_BlackPoint[0][cnt];//该
行黑线中心点重新赋值
g_width[n] = g_BlackPoint[1][cnt];
}
}
cnt ++;
// i = 4;//跳出循环,只要最左端的
}
}
if(cnt > 0)
{
g_BottomLatestValidLine = n;
v_LastLineCenter = g_BlackPositionCenter[n];
NextFlag ++;
n = 0;
23
}
}
}
if(NextFlag == 2)
{
for(n = g_BottomLatestValidLine; n >= 0; n--) //(n 必须为有符号的)
{
//黑块中心赋初值
for(k = 0;k < MAX_BLACK_NUM;k ++)
{
g_BlackPoint[0][k] = 250;
g_BlackPoint[1][k] = 0;
}
//求前一有效行
k = n + 1;
/*
if((n < 40) && (g_BottomValidLine > 45))
{
k = n + 2;
} */
while(g_BlackPositionCenter[k] == 250)
{
k ++ ;
}
//求前第二个有效行
k2 = k + 1;
/*
if((n < 40) && (k < 44))
{
k2 = k + 2;
} */
while(g_BlackPositionCenter[k2] == 250)
{
k2 ++;
}
//
if(g_width[k2] >= g_width[k])
{
24
g_WidthError = g_width[k2] - g_width[k] + 7;
g_MinWidth = g_width[k] - g_WidthError; //(有可能为负)
if(g_MinWidth < 7)
{
g_MinWidth = 7;
}
g_MaxWidth = g_width[k] + g_WidthError;
if(g_MaxWidth > 40)
{
g_MaxWidth = 40;
}
}
else
{
g_WidthError = g_width[k] - g_width[k2] + 7;
g_MinWidth = g_width[k] - g_WidthError;
if(g_MinWidth < 7)
{
g_MinWidth = 7;
}
g_MaxWidth = g_width[k] + g_WidthError + 2;
if(g_MaxWidth > 40)
{
g_MaxWidth = 40;
}
}
//
ValidLineError = 6 * (k - n);
if(ValidLineError > 24)
{
ValidLineError = 24;
}
if(g_BlackPositionCenter[k2] > g_BlackPositionCenter[k]) //向右拐
{
g_SearchDirection = 1; //right
g_error = ((g_BlackPositionCenter[k2] - g_BlackPositionCenter[k])
+ ValidLineError + 10);
g_SearchStart = g_BlackPositionCenter[k] - g_error - g_MaxWidth;
if((g_BlackPositionCenter[k2] - g_BlackPositionCenter[k]) > 5)
25
{
g_SearchEnd = g_BlackPositionCenter[k] + g_MaxWidth / 2;
////有干扰
}
else
{
g_SearchEnd = g_BlackPositionCenter[k] + 10 + g_MaxWidth;
}
if(g_SearchStart < Set_Min_Point + 2)
{
g_SearchStart = Set_Min_Point + 2;
}
if(g_SearchEnd > Set_Max_Point - 2)
{
g_SearchEnd = Set_Max_Point - 2;
}
}
else if(g_BlackPositionCenter[k] >= g_BlackPositionCenter[k2])
{
g_SearchDirection = 0; //left
g_error = ((g_BlackPositionCenter[k] - g_BlackPositionCenter[k2])
+ ValidLineError + 10);
g_SearchEnd = g_BlackPositionCenter[k] + g_error + g_MaxWidth;
if((g_BlackPositionCenter[k] - g_BlackPositionCenter[k2]) > 5)
{
g_SearchStart = g_BlackPositionCenter[k] - g_MaxWidth / 2;
}
else
{
// g_SearchStart = g_BlackPositionCenter[k] - g_error -
g_MaxWidth;
g_SearchStart = g_BlackPositionCenter[k] - 10 - g_MaxWidth;
}
if(g_SearchStart < Set_Min_Point + 2)
{
g_SearchStart = Set_Min_Point + 2;
}
26
if(g_SearchEnd > Set_Max_Point - 2)
{
g_SearchEnd = Set_Max_Point - 2;
}
}
////////////////////////////////////
cnt = 0;
//v_LastLineCenter = v_LastLineCenter + g_BlackPositionCenter[k] -
g_BlackPositionCenter[k2];
//SCI0SendByte(0xaa) ;
// SCI0SendByte(0xaa) ;
//SCI0SendByte(v_LastLineCenter) ;
//SCI0SendByte(g_BlackPositionCenter[k]) ;
//SCI0SendByte(g_BlackPositionCenter[k2]) ;
// SCI0SendByte(v_slopeOneAverage + 128) ;
for(i = 0;i < MAX_INTER_POINT;i ++)
{
if((v_point_used_down[n][i + 1] >= g_SearchStart) &&
(v_point_used_down[n][i + 1] <= g_SearchEnd) && (v_point_used_up[n][i] >=
g_SearchStart) && (v_point_used_up[n][i] <= g_SearchEnd) &&
(v_point_used_up[n][i] - v_point_used_down[n][i + 1] <= g_MaxWidth) &&
(v_point_used_up[n][i] - v_point_used_down[n][i + 1] >= g_MinWidth))
{
g_BlackPoint[0][cnt] = (v_point_used_up[n][i] +
v_point_used_down[n][i + 1]) / 2;//记录中心点
g_BlackPoint[1][cnt] = v_point_used_up[n][i] -
回复

使用道具 举报

0

主题

242

帖子

0

精华

高级会员

Rank: 4

积分
925
QQ
威望
451
贡献
382
兑换币
0
注册时间
2008-11-28
在线时间
46 小时
9#
发表于 2009-4-16 12:57:21 | 只看该作者
v_point_used_down[n][i + 1]; //记录黑线宽度
if(cnt == 0) //
{
g_BlackPositionCenter[n] = g_BlackPoint[0][cnt]; //该行
黑线中心点重新赋值
g_width[n] = g_BlackPoint[1][cnt];
}
else // (1<n<=4)
{
if(g_BlackPoint[0][cnt] >= v_LastLineCenter) //
{
g_BlackPositionCenter[n] = g_BlackPoint[0][cnt]; //
该行黑线中心点重新赋值
27
g_width[n] = g_BlackPoint[1][cnt];
}
else if(g_BlackPositionCenter[n] - v_LastLineCenter >
v_LastLineCenter - g_BlackPoint[0][cnt])
{ //
g_BlackPositionCenter[n] = g_BlackPoint[0][cnt];//该
行黑线中心点重新赋值
g_width[n] = g_BlackPoint[1][cnt];
}
}
cnt ++;
// i = 4;//
}
}
if(cnt > 0)
{
v_LastLineCenter = g_BlackPositionCenter[n];
}
if((k - n) > 20)
{
g_BlackPositionCenter[n] = 250;
g_width[n] = 0;
n = -1;
}
}
}
v_slope_valid_line = 0;
v_slope_sum = 0;
v_slope_average = 0;
v_straight_valid_line = 0;
F_SlopAverageOne = 0;
F_SlopAverageTwo = 0;
F_StraightTwo = 0;
F_SlopAverageOne = 0;
v_straight_valid = 0;
v_ValidLine = 0;
28
v_TotleValidLine = 0;
v_straight_line_lost = 0;
v_slopeTwoSum = 0;
v_LastslopeOneAverage = v_slopeOneAverage;
v_slopeOneAverage = 0;
for(i = 49; i >= 0; i --)
{
g_SolutedBlackPositionCenter[i] = g_BlackPositionCenter[i];
}
for(i = 45; i > 3; i --)
{
v_slope_2[i] = 0;
if((g_BlackPositionCenter[i] != 250) && (g_BlackPositionCenter[i - 4] !=
250) && (g_BlackPositionCenter[i + 4] != 250))
{
v_TotleValidLine ++;
v_ValidLine ++;
v_slope_last = g_BlackPositionCenter[i - 4] -
g_BlackPositionCenter[i];//
v_slope_now = g_BlackPositionCenter[i] - g_BlackPositionCenter[i +
4];
v_slope_2[i] = v_slope_now - v_slope_last; //二次斜率
v_slopeOneAverage += v_slope_last;
v_slopeTwoSum += v_slope_2[i];
if(F_StraightTwo == 0)
{
if((v_slope_now - v_slope_last < 4) && (v_slope_now -
v_slope_last > -4))//直道
{
v_straight_valid_line ++;
v_slope_sum += v_slope_last;
}
}
else if(F_StraightTwo == 1)
{
if((v_slope_now - v_slope_last < 2) && (v_slope_now -
v_slope_last > -2))//直道
29
{
v_straight_valid_line ++;
v_slope_sum += v_slope_last;
}
if((v_ValidLine > 5) && (v_straight_valid_line >= 3))
{
// Led3_ON;
F_SlopAverageTwo = v_slope_sum / v_straight_valid_line ;
F_StraightTwo = 2;
i = 0;
}
}
}
else
{
if(F_StraightTwo == 0)
{
//if(((v_straight_valid_line > 2) && ((v_ValidLine >= 4)) &&
(v_TotleValidLine < 12)) ||((v_straight_valid_line > 4) && ((v_ValidLine > 6))) )
if(((v_TotleValidLine < 10)) ||((v_straight_valid_line > 4) &&
(v_ValidLine > 7) && (v_TotleValidLine < 30)) )
{
//Led2_ON;
v_straight_line_lost = i;
v_straight_valid = 1;
F_StraightTwo = 1;
F_SlopAverageOne = v_slope_sum / v_straight_valid_line ;
}
}
// SCI0SendByte(0xBB);
//SCI0SendByte(v_straight_valid_line);
//SCI0SendByte(v_ValidLine);
v_ValidLine = 0;
v_straight_valid_line = 0;
v_slope_sum = 0;
v_ValidLine = 0;
30
}
}
if(v_slopeTwoSum < 0) v_slopeTwoSum = 0 - v_slopeTwoSum;
v_slopeOneAverage = v_slopeOneAverage / v_TotleValidLine;
//==================================================
========================================
//SCI0SendByte(0xaa);
//SCI0SendByte(0xaa);
///SCI0SendByte(v_slope_sum + 128);
//SCI0SendByte(F_SlopAverageTwo + 128);
//SCI0SendByte(v_straight_line_lost);
/*
if(((F_SlopAverageTwo > 4) && (F_SlopAverageOne < -1) &&
(F_SlopAverageTwo - F_SlopAverageOne > 10)) || ((F_SlopAverageTwo < -4)
&&( F_SlopAverageOne > 1)&& (F_SlopAverageTwo - F_SlopAverageOne <
-10)))
{
//Led4_ON;
for(i = 0;i < v_straight_line_lost;i++)
{
g_BlackPositionCenter[i] = 250;
}
}
*/
for(i = 0;i < 46;i++)
{
if(g_SolutedBlackPositionCenter[i] != 250)
{
if(v_slope_2[i] > 10)
{
v_slope_2[i] = 10 ;
}
else if(v_slope_2[i] < -10)
{
v_slope_2[i] = -10 ;
}
g_SolutedBlackPositionCenter[i] -= v_slope_2[i] * 2;
31
}
if(g_SolutedBlackPositionCenter[i] < 10)
{
g_SolutedBlackPositionCenter[i] = 10;
}
else if(g_SolutedBlackPositionCenter[i] > 206)
{
g_SolutedBlackPositionCenter[i] = 206;
}
}
//===========================================================
===================
m = g_FormerValidLine; //用变量m 将上一场的有效行数存起来
g_FormerValidLine = g_ValidLine;
g_ValidLine = 0;
g_MotorValidLine = 0;
k = g_DirectionControl;//用变量k 将上一场的加权中心值存起来
k2 = g_BottomMiddle;//用变量k2 将上一场的后20 行中心值存起来
g_BlackLineTotal = 0;
g_DirectionControlLine = 0;
g_MotorControlLine = 0;
//g_FrontValidLine = 0;
for(n = (MAX_VIDEO_USEDLINE - 1); n >= 0; n --)
{
if(g_BlackPositionCenter[n] != 250)
{
if(0 == g_ValidLine)
{
g_DirectionControl = 0;
}
g_ValidLine ++;
g_BlackLineTotal += g_BlackPositionCenter[n];
//============加权平均===================
g_center = g_SolutedBlackPositionCenter[n];
//g_center = g_BlackPositionCenter[n];
32
g_DirectionControlLine += g_coeff[n];
g_DirectionControl += g_coeff[n] * g_center;
}
//======= ========
if(n == 30)
{
if(g_ValidLine >= 10)
{
g_BottomMiddle = g_BlackLineTotal / g_ValidLine;
}
}
}
////////////////////////////////////////////////////////////////
// Led2_OFF;
//Led3_OFF;
// Led4_OFF;
for(n = 1; n < 50; n ++)
{
if(g_BlackPositionCenter[n] != 250)
{
if(0 == g_MotorValidLine)
{
g_MotorControl = 0;
}
g_MotorValidLine ++;
if(g_MotorValidLine < 20)
{
g_MotorControlLine += g_coeff[n];
g_MotorControl += g_coeff[n] * g_BlackPositionCenter[n] ;
}
}
}
if(g_ValidLine == 0)
{
if(k < Set_Mid_Point - 40) //判断上一有效场黑线中心靠左还是靠右
{
33
g_DirectionControl = Set_Min_Point ;
g_BottomMiddle = Set_Min_Point + 30;
g_MotorControl = g_DirectionControl;
}
else if(k >= Set_Mid_Point + 40)
{
g_DirectionControl = Set_Max_Point;
g_BottomMiddle = Set_Max_Point - 30;
g_MotorControl = g_DirectionControl;
}
else
{
g_DirectionControl = k;
g_BottomMiddle = k2;
g_MotorControl = g_DirectionControl;
}
}
else if(((m > 40) || (g_FormerValidLine > 40)) && (g_ValidLine < 5))
{
g_DirectionControl = k;
g_BottomMiddle = k2;
g_MotorControl = g_DirectionControl;
}
else
{
g_DirectionControl /= g_DirectionControlLine;
g_MotorControl /= g_MotorControlLine;
}
if(g_DirectionControl < Set_Min_Point + 12)
{
g_DirectionControl = Set_Min_Point + 12;
g_MotorControl = g_DirectionControl;
}
else if(g_DirectionControl > Set_Max_Point - 12)
回复

使用道具 举报

0

主题

242

帖子

0

精华

高级会员

Rank: 4

积分
925
QQ
威望
451
贡献
382
兑换币
0
注册时间
2008-11-28
在线时间
46 小时
10#
发表于 2009-4-16 12:57:27 | 只看该作者
{
g_DirectionControl = Set_Max_Point - 12;
g_MotorControl = g_DirectionControl;
}
34
//===========================================================
===============
if(Sending_S == 0)
{
/////////////////未处理图像/////////
SCI0SendByte( 0xFE ) ; //场数据传输开始标志
for(i = 0; i < 50; i ++)
{
SCI0SendByte(0xFD); //行数据传输开始标志
for(j = 0;j < 5;j ++)
{
SCI0SendByte(v_point_used_down[i][j]);//
//asm("nop");
SCI0SendByte(v_point_used_up[i][j]) ; //
//SCI0SendByte(v_point_down[i][j]);//
asm("nop");
//SCI0SendByte(v_point_up[i][j]) ; //
}
}
SCI0SendByte(0xFC) ; //数据传输结束标志
///////////////处理后图像/////////////////////////////
for(i = 0; i < 50; i ++)
{
SCI0SendByte(g_SolutedBlackPositionCenter[i]); //处理后中心点
//SCI0SendByte(g_BlackPositionCenter[i]); //中心点
}
SCI0SendByte(0xFF) ; //数据传输结束标志
}
for(i = 0; i < 250; i = i + 1)
{
for(j = 0;j < 5;j ++)
{
v_point_up[i][j] = 0 ; //
v_point_down[i][j] = 0 ;//
}
35
}
//if(v_ValidStartline < 2)
if(Present_OddEven_state == Even) //
{
while (Present_OddEven_state == Even);
EnableInterrupts;
}
else //
{
while (Present_OddEven_state == Odd);
asm("nop"); asm("nop"); asm("nop"); asm("nop");
while (Present_OddEven_state == Even);
EnableInterrupts;
}
Running_Enable = 0;
}
/*******************************************************************
**
Function Name:ServoPIDInit
Description: initialize servo angle PID variable
Inputs: None
Outputs: None
NOTES:
********************************************************************
*/
void ServoPIDInit(void)
{
sPID.loca_Ref = Set_Mid_Point;
sPID.loca_FeedBack = 0;
sPID.loca_PreError = 0;
//sPID.loca_PreIntegral = 0;
sPID.loca_Kp = LOCA_KPVALUE;
sPID.loca_Ki = LOCA_KIVALUE;
sPID.loca_Kd = LOCA_KDVALUE;
sPID.loca_PreU = 0;
}
/*******************************************************************
**
Function Name:SpeedPIDInit
Description: initialize speed PID variable
36
Inputs: None
Outputs: None
NOTES:
********************************************************************
*/
/*******************************************************************
**
Function Name:V_PIDCalc
Description: speed PID calculation function
Inputs: None
Outputs: None
NOTES:
********************************************************************
*/
/*******************************************************************
**
Function NamearameterSelect
Description:
Inputs: None
Outputs: None
NOTES:
********************************************************************
/
void ParameterSelect(void)
{
INT8U l_tmp = 0;
INT16U i = 0;
Led1_ON;
Led2_ON;
Led3_ON;
Led4_ON;
F_WholePulse_SS = 30;
F_WholePulse_S = F_WholePulse_SS;
while(Sending_S == 0)
{
// Led3_OFF;
Led4_OFF;
37
if(Button_1 == 0)
{ //Led3_ON;
ShortDelay();//Delay short time
if(Button_1 == 0)
{
ShortDelay();//Delay short time
if(Button_1 == 0)
{
F_WholePulse_S ++;
F_WholePulse_S ++;
Led1_OFF;
Led2_OFF;
Led3_OFF;
Led4_OFF;
//Clear Display
//LED_PORT &= ~(l_tmp << 2);//Display Number
switch((F_WholePulse_S - F_WholePulse_SS) >> 1)
{
case 0ed1_OFF;Led2_OFF;Led3_OFF;Led4_OFF;
break; // 111
case 1:Led1_ON;Led2_OFF;Led3_OFF;Led4_OFF;
break; // 001
case 2:Led1_OFF;Led2_ON;Led3_OFF;Led4_OFF;
break; // 001
case 3:Led1_ON;Led2_ON;Led3_OFF;Led4_OFF;
break; // 010
case 4:Led1_OFF;Led2_OFF;Led3_ON; Led4_OFF;
break; // 011
case 5:Led1_ON;Led2_OFF;Led3_ON;Led4_OFF;
break; // 100
case 6:Led1_OFF;Led2_ON;Led3_ON;Led4_OFF;
break; // 101
case 7:Led1_ON;Led2_ON;Led3_ON;Led4_OFF;
break; // 110
case
8:Led1_OFF;Led2_OFF;Led3_OFF;Led4_ON;break; // 111
case 9:Led1_ON;Led2_OFF;Led3_OFF;Led4_ON;break;
// 001
case 10:Led1_OFF;Led2_ON;Led3_OFF;Led4_ON;
break; // 001
38
case 11:Led1_ON;Led2_ON;Led3_OFF;Led4_ON;
break; // 010
case 12:Led1_OFF;Led2_OFF;Led3_ON; Led4_ON;
break; // 011
case 13:Led1_ON;Led2_OFF;Led3_ON;Led4_ON;
break; // 100
case 14:Led1_OFF;Led2_ON;Led3_ON;Led4_ON;
break; // 101
case 15:Led1_ON;Led2_ON;Led3_ON;Led4_ON;
break; // 110
default:break;
}
LongDelay();
}
}
}
}
/*Parameters Input*/
while(Button_0 == 1)
{ Led4_ON;
if(Button_1 == 0)
{ //Led3_ON;
ShortDelay();//Delay short time
if(Button_1 == 0)
{
ShortDelay();//Delay short time
if(Button_1 == 0)
{
l_tmp ++;
if(l_tmp > MAX_STRATEGY + 1 )
{
l_tmp = 1;
}
Led1_OFF;
Led2_OFF;
Led3_OFF;
39
Led4_OFF;
//Clear Display
//LED_PORT &= ~(l_tmp << 2);//Display Number
switch(l_tmp)
{
case 1:Led1_OFF;Led2_OFF;Led3_OFF;break; // 001
case 2:Led1_ON;Led2_OFF;Led3_OFF; break; // 001
case 3:Led1_OFF;Led2_ON;Led3_OFF; break; // 010
case 4:Led1_ON;Led2_ON;Led3_OFF; break; // 011
case 5:Led1_OFF;Led2_OFF;Led3_ON; break; // 100
case 6:Led1_ON;Led2_OFF;Led3_ON; break; //
101
case 7:Led1_OFF;Led2_ON;Led3_ON; break; //
110
case 8:Led1_ON;Led2_ON;Led3_ON;break; // 111
default:break;
}
SCI0SendByte(l_tmp);
LongDelay();
}
}
}
}
Led1_OFF;
Led2_OFF;
Led3_OFF;
Led4_OFF;
/*Parameters Select*/
/*******************************************************************
***
Function Name:ShortDelay
Description:
Inputs: None
Outputs: None
NOTES:
********************************************************************
*/
void LongDelay(void)
40
{
INT8U j = 0;
INT16U i = 0;
for(j = 0;j < 0x20;j++)
{
for(i = 0;i <0xffff;i++);//Delay long time
}
}
void ShortDelay(void)
{
INT16U i = 0;
for(i = 0;i <50000;i++);//Delay short time
}
/*SCHEDULE_C END*/
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关于我们|联系我们|小黑屋|智能车制作 ( 黑ICP备2022002344号

GMT+8, 2024-9-20 06:14 , Processed in 0.062814 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表