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

📄 kit06.c

📁 瑞赛模型车比赛参考程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/*======================================*/
/* Include declarations                 */
/*======================================*/
#include    <machine.h>
#include    "h8_3048.h"

/*======================================*/
/* Symbol definitions                   */
/*======================================*/

/* Constants */
#define         TIMER_CYCLE     3071    /* Timer cycle: 1 ms        */
                                        /* When  /8 is used,        */
                                        /*  /8 = 325.5 ns           */
                                        /* 亪TIMER_CYCLE =          */
                                        /*      1[ms] / 325.5[ns]   */
                                        /*               = 3072     */
#define         PWM_CYCLE       49151   /* PWM cycle: 16 ms         */
                                        /* 亪PWM_CYCLE =            */
                                        /*      16[ms] / 325.5[ns]  */
                                        /*               = 49152    */
#define         SERVO_CENTER    4730//4460//4560    /* Servo sensor value       */
#define         HANDLE_STEP     26      /* Value for 1              */

/* Mask value setting:  : Masked (disabled) O: Not masked (enabled) */
#define         MASK2_2         0x66    /*                          */
#define         MASK2_0         0x60    /*                          */
#define         MASK0_2         0x06    /*                          */
#define         MASK3_3         0xe7    /*                          */
#define         MASK0_3         0x07    /*                          */
#define         MASK3_0         0xe0    /*                          */
#define         MASK4_0         0xf0    /*                          */
#define         MASK0_4         0x0f    /*                          */
#define         MASK4_4         0xff    /*                          */

/*======================================*/
/* Prototype declarations               */
/*======================================*/
void init( void );
void timer( unsigned long timer_set );
int check_crossline( void );
int check_rightline( void );
int check_leftline( void );
unsigned char sensor_inp( unsigned char mask );
unsigned char dipsw_get( void );
unsigned char pushsw_get( void );
unsigned char startbar_get( void );
void led_out( unsigned char led );
void speed( int accele_l, int accele_r );
void handle( int angle );
char unsigned bit_change( char unsigned in );

/*======================================*/
/* Global variable declarations         */
/*======================================*/
unsigned long   cnt0;                   /* For the timer functions      */
unsigned long   cnt1;                   /* Used in main()               */
int             pattern;                /* Pattern number               */

/************************************************************************/
/* Main Program                                                         */
/************************************************************************/
void main( void )
{
    int     i;

    /* Microcontroller function initialization */
    init();                             /* Initialization               */
    set_ccr( 0x00 );                    /* Enable all interrupts        */

    /* Micom car state initialization */
    handle( 0 );
    speed( 0, 0 );

    while( 1 ) {
    switch( pattern ) {

    /*****************************************************************
    Notes on patterns
     0: Wait for switch input
     1: Check whether or not the start bar is open
    11: Normal trace
    12: Check for the end of a wide turn to right
    13: Check for the end of a wide turn to left
    21: Processing for when the first crossline is detected
    22: Read and jump over the second
    23: Detect a trace or sharp turn after a crossline
    31: Left sharp turn clear processing: Wait briefly until clear
    32: Left sharp turn clear processing: Check that the end of the turn has been reached
    41: Right sharp turn clear processing: Wait briefly until clear
    42: Right sharp turn clear processing: Check that the end of the turn has been reached
    51: Processing when the first right half-line has been detected
    52: Read and jump over the second
    53: Trace following a right half line
    54: Check that the right lane change has completed
    61: Processing when the first left half-line has been detected
    62: Read and jump over the second
    63: Trace following a left half line
    64: Check that the left lane change has completed
    *****************************************************************/

    case 0:
        /* Wait for switch input */
        if( pushsw_get() ) {
            pattern = 1;
            cnt1 = 0;
            break;
        }
        if( cnt1 < 100 ) {              /* Processing for LED blinking */
            led_out( 0x1 );
        } else if( cnt1 < 200 ) {
            led_out( 0x2 );
        } else {
            cnt1 = 0;
        }
        break;

    case 1:
        /* Check whether or not the start bar is open */
        if( !startbar_get() ) {
            /* Start!! */
            led_out( 0x0 );
            pattern = 11;
            cnt1 = 0;
            break;
        }
        if( cnt1 < 50 ) {              /* Processing for LED blinking */
            led_out( 0x1 );
        } else if( cnt1 < 100 ) {
            led_out( 0x2 );
        } else {
            cnt1 = 0;
        }
        break;

    case 11:
        /* Normal trace */
		if( check_crossline() ) {       /* Also check the crossline during large turns. */
            pattern = 21;
            break;
        }
        if( check_rightline() ) {       /* Right half line check    */
            pattern = 51;
            break;
        }
        if( check_leftline() ) {        /* Left half line check     */
            pattern = 61;
            break;
        }
        switch( sensor_inp(MASK3_3) ) {
            case 0x00:
                /* Center   straight */
                handle( 0 );
                speed(85 ,85 );
                break;

            case 0x04:
                /* Move slightly to the left   turn slightly to the right */
                handle( 8 );
                speed( 90 ,80 );
                break;

            case 0x06:
                /* Move somewhat to the left   turn somewhat to the right */
                handle( 15 );
                speed( 80 ,65 );
                break;

            case 0x07:
                /* Move a medium amount to the left   turn a medium amount to the right */
                handle( 20 );
                speed( 70 ,20 );
                break;

            case 0x03:
                /* Move significantly to the left   turn significantly to the right */
                handle( 28 );
                speed( 65 ,40 );
                pattern = 12;
                break;

            case 0x20:
                /* Move slightly to the right   turn slightly to the left */
                handle( -8 );
                speed( 80,90 );
                break;

            case 0x60:
                /* Move somewhat to the right   turn somewhat to the left */
                handle( -15 );
                speed( 65 ,80 );
                break;

            case 0xe0:
                /* Move a medium amount to the right   turn a medium amount to the left */
                handle( -20 );
                speed( 50 ,70 );
                break;

            case 0xc0:
                /* Move significantly to the right   turn significantly to the left */
                handle( -28 );
                speed( 40 ,65 );
                pattern = 13;
                break;

            default:
                break;
        }
        break;

    case 12:
        /* Check for completion of a large turn to the right. */
        if( check_crossline() ) {       /* Also check the crossline during large turns. */
            pattern = 21;
            break;
        }
        if( check_rightline() ) {       /* Check for right half line.   */
            pattern = 51;
            break;
        }
        if( check_leftline() ) {        /* Check for left half line.    */
            pattern = 61;
            break;
        }
        if( sensor_inp(MASK3_3) == 0x06 ) {
            pattern = 11;
        }
		switch( sensor_inp(MASK4_4) ) {
            case 0x04:
                /* Move slightly to the right   turn slightly to the left */
                handle( 16 );
                speed( 70 ,60 );
                break;

            case 0x07:
                /* Move a medium amount to the right   turn a medium amount to the left */
                handle(24 );
                speed( 65 ,45 );
                break;

            case 0x03:
                /* Move significantly to the right   turn significantly to the left */
                handle( 32 );
                speed( 60 ,40 );
                break;
			
			case 0x83:
			case 0x81:
				handle(36);
				speed(55,30);
				break;

			case 0xc1:
			case 0xc3:
				handle(40);
				speed(55,20);

            default:
                break;
        }

        break;

    case 13:
        /* Check for completion of a large turn to the left. */
        if( check_crossline() ) {       /* Also check the crossline during large turns. */
            pattern = 21;
            break;
        }
        if( check_rightline() ) {       /* Check for right half line.   */
            pattern = 51;
            break;
        }
        if( check_leftline() ) {        /* Check for left half line.    */
            pattern = 61;
            break;
        }
        if( sensor_inp(MASK3_3) == 0x60 ) {
            pattern = 11;
        }
		switch( sensor_inp(MASK4_4) ) {

            case 0x20:
                /* Move slightly to the left   turn slightly to the right */
                handle( -16 );
                speed( 60 ,70 );
                break;

            case 0xe0:
                /* Move a medium amount to the left   turn a medium amount to the right */
                handle( -24 );
                speed( 45 ,65 );
                break;

            case 0xc0:
                /* Move significantly to the left   turn significantly to the right */
                handle( -32 );
                speed( 40 ,60 );
                break;

			case 0xc1:
			case 0x81:
				handle(-36);
				speed(30,55);
				break;

			case 0x83:
			case 0xc3:
				handle(-40);
				speed(25,45);

            default:
                break;
        }
        break;

    case 21:
        /* Processing when the first crossline is detected */
        led_out( 0x3 );
        handle( 0 );
        speed( 0 ,0 );
        pattern = 22;
        cnt1 = 0;
        break;

    case 22:
        /* Read and ignore the second line. */
        if( cnt1 > 110 ) {
            pattern = 23;
            cnt1 = 0;
        }
        break;

    case 23:
        /* Detection of a trace, sharp turn after a crossline */
        if( sensor_inp(MASK4_4)==0xf8 || sensor_inp(MASK4_4)==0xf0 ) {
            /* If a left sharp turn recognized   proceed to left sharp turn clear processing */
            led_out( 0x1 );
            handle( -40 );			//handle( -38 );
            speed( 18,55);
            pattern = 31;
            cnt1 = 0;
            break;
        }
        if( sensor_inp(MASK4_4)==0x1f ||sensor_inp(MASK4_4)==0x0f ) {
            /* If a right sharp turn recognized   proceed to right sharp turn clear processing */
            led_out( 0x2 );
            handle( 40 );			//handle( 38 );
            speed(55 ,35 );
            pattern = 41;
            cnt1 = 0;
            break;
        }
        switch( sensor_inp(MASK3_3) ) {
            case 0x00:
                /* Center   straight */
                handle( 0 );
                speed( 40 ,40 );
                break;
            case 0x04:
            case 0x06:
            case 0x07:
            case 0x03:
                /* Move left   turn right */
                handle( 10 );		//handle( 8 );	
                speed( 40 ,30 );
                break;
            case 0x20:
            case 0x60:
            case 0xe0:
            case 0xc0:
                /* Move right   turn left */
                handle( -10 );		//handle( -8 );
                speed( 30 ,40 );
                break;
        }
        break;

    case 31:
        /* Left sharp turn clear processing - wait briefly until stable */
        if( cnt1 > 150 ) {
            pattern = 32;
            cnt1 = 0;
        }
        break;

    case 32:
        /* Left sharp turn clear processing - check for completion of the turn */
        if( sensor_inp(MASK3_3) == 0x60 || sensor_inp(MASK3_3) == 0x30 ) {
            led_out( 0x0 );
            pattern = 11;
            cnt1 = 0;
        }
        break;

    case 41:
        /* Right sharp turn clear processing - wait briefly until stable */
        if( cnt1 > 150 ) {
            pattern = 42;
            cnt1 = 0;
        }
        break;

    case 42:
        /* Right sharp turn clear processing - check for completion of the turn */
        if( sensor_inp(MASK3_3) == 0x06||sensor_inp(MASK3_3) == 0x0c ) {
            led_out( 0x0 );
            pattern = 11;
            cnt1 = 0;
        }
        break;

    case 51:
        /* Processing when the first right half line is detected */
		
		if( check_crossline() ) {       /* Also check the crossline during large turns. */
            pattern = 21;
            break;
        }
        led_out( 0x2 );
        handle( 0 );
        speed( 0 ,0 );
        pattern = 52;
        cnt1 = 0;
        break;

    case 52:
        /* Read and ignore the second line. */
		if( check_crossline() ) {       /* Also check the crossline during large turns. */
            pattern = 21;
            break;
        }
        if( cnt1 > 80 ) {
            pattern = 53;
            cnt1 = 0;
        }
        break;

    case 53:
        /* Trace, lane change after a right half line */
        if( sensor_inp(MASK4_4) == 0x00 ) {
            handle( 15 );
            speed( 40 ,20 );//speed( 50 ,35 );

⌨️ 快捷键说明

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