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

📄 kit05.c

📁 瑞萨智能车大赛源码。High-performance Embedded Workshop
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************/
/* Micom Car Rally Trace Program 2005 Version                               */
/*  2005.04 Renesas Technology Micom Car Rally Executive Committee          */
/****************************************************************************/
/*
 This kit05.c program connects to motor drive board (Vol.3).
 The following contents have been changed with respect to kit04.c.
- The port connected to CPU board has been changed from J2 (Port A) to J3 (Port B).
- Motor mode function has been removed (scrapped) (always brake)
- Motor is able to perform normal rotation, reverse rotation and brake control. (Reverse rotation was not possible previously)
- 2 step process after crossline detection (Pattern 23, 24)
*/

/*======================================*/
/* Include                              */
/*======================================*/
#include    <machine.h>
#include    "h8_3048.h"

/*======================================*/
/* Symbol definition                    */
/*======================================*/

/* Set Constants */
#define         TIMER_CYCLE     3071    /* Timer cycle 1ms              */
                                        /* When it is to be used by f/8 */
                                        /* f / 8 = 325.5[ns]            */
                                        /* Therefore, TIMER_CYCLE       */
                                        /*          = 1[ms] / 325.5[ns] */
                                        /*          = 3072              */
#define         PWM_CYCLE       49151   /* PWM cycle 16ms               */
                                        /* Therefore, PWM_CYCLE         */
                                        /*         = 16[ms] / 325.5[ns] */
                                        /*         = 49152              */
#define         SERVO_CENTER    5000   /* Center value of Servo        */
#define         HANDLE_STEP     26      /* 1 degree part value          */

/* Mask value setting x: With Mask (Invalid) o:Without mask (Valid)     */
#define         MASK2_2         0x66    /* xooxxoox                     */
#define         MASK2_0         0x60    /* xooxxxxx                     */
#define         MASK0_2         0x06    /* xxxxxoox                     */
#define         MASK3_3         0xe7    /* oooxxooo                     */
#define         MASK0_3         0x07    /* xxxxxooo                     */
#define         MASK3_0         0xe0    /* oooxxxxx                     */
#define         MASK4_0         0xf0    /* ooooxxxx                     */
#define         MASK0_4         0x0f    /* xxxxoooo                     */
#define         MASK1_1         0x81    /* oxxxxxxo                     */

/*======================================*/
/* Prototype declaration                */
/*======================================*/
void init(void );
void timer( unsigned long timer_set );
int check_crossline( void );
unsigned char sensor_inp( unsigned char mask );
unsigned char dipsw_get( void );
unsigned char pushsw_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 Declaration          */
/*======================================*/
unsigned long   cnt0;                   /* used in 'timer' function */
unsigned long   cnt1;                   /* used in main             */

/************************************************************************/
/* Main program                                                         */
/************************************************************************/
void main( void )
{
    int     i;
    int     pattern;

    /* Microcomputer function initialization */
    init();                             /* Initialization           */
    set_ccr( 0x00 );                    /* Whole interrupt enable   */

    /* State initialization of micom car */
    handle( 0 );
    speed( 0, 0 );
    pattern = 0;
    cnt1 = 0;

    while( 1 ) {
    switch( pattern ) {

    /*****************************************************************
    About pattern
     0: Switch input wait
     1: Wait for 1 second after the switch is pressed.
    11: Usual trace
    12: Check for the end of long turn to the right
    13: Check for the end of long turn to the left
    21: Processing when first crossline is detected
    22: Skip second crossline
    23: Trace 1 after crossline
    24: Trace 2 after crossline, crank detection
    31: Left crank Clear process  Waits till it stabilizes
    32: Left crank clear process   Check for end of turn
    41: Right crank clear process  Waits till it stabilizes
    42: Right crank clear process  Check for end of turn
    *****************************************************************/

    case 0:
        /* Switch input waiting */
        if( pushsw_get() ) {
            pattern = 1;
            cnt1 = 0;
            break;
        }
        if( cnt1 < 100 ) {              /* LED blinking process */
            led_out( 0x1 );
        } else if( cnt1 < 200 ) {
            led_out( 0x2 );
        } else {
            cnt1 = 0;
        }
        break;

    case 1:
        /* 1 second waiting after the switch is pushed */
        if( cnt1 < 500 ) {
            /* 1.0 seconds starting ago LED1:"OFF" , LED0:"ON" */
            led_out( 0x1 );
        } else if( cnt1 < 1000 ) {
            /* 0.5 seconds starting ago LED1:"ON" , LED0:"OFF" */
            led_out( 0x2 );
        } else {
            /* Start!! */
            led_out( 0x0 );
            pattern = 11;
            cnt1 = 0;
        }
        break;

    case 11:
        /* Usual trace */
        if( check_crossline() ) {       /* Crossline Check */
            pattern = 21;
            break;
        }
        switch( sensor_inp(MASK3_3) ) {
            case 0x00:
                /* Center -> Straight */
                handle( 0 );
                speed( 100 ,100 );
                break;

            case 0x04:
                /* Slightly left inclined -> Slight turn to the right */
                handle( 5 );
                speed( 100 ,100 );
                break;

            case 0x06:
                /* Little left inclined -> Small turn to the right */
                handle( 10 );
                speed( 80 ,69 );
                break;

            case 0x07:
                /* Left inclined from the middle -> Middle turn to the right */
                handle( 15 );
                speed( 50 ,40 );
                break;

            case 0x03:
                /* Large inclined to the left -> Large turn to the right */
                handle( 25 );
                speed( 30 ,21 );
                pattern = 12;
                break;

            case 0x20:
                /* Slightly inclined to the right -> Slight turn to the left */
                handle( -5 );
                speed( 100 ,100 );
                break;

            case 0x60:
                /* A little inclined to the right -> Small turn to the left */
                handle( -10 );
                speed( 69 ,80 );
                break;

            case 0xe0:
                /* Right inclined from the middle veering -> Middle turn to the left */
                handle( -15 );
                speed( 40 ,50 );
                break;

            case 0xc0:
                /* Large inclined to the right -> Large turn to the left */
                handle( -25 );
                speed( 21 ,30 );
                pattern = 13;
                break;

            default:
                break;
        }
        break;

    case 12:
        /* Check of large turning to the right completion */
        if( check_crossline() ) {   /* Crossline check even during turning */
            pattern = 21;
            break;
        }
        if( sensor_inp(MASK3_3) == 0x06 ) {
            pattern = 11;
        }
        break;

    case 13:
        /* Check of large turning completion to the left */
        if( check_crossline() ) {   /* Crossline check even during large turn */
            pattern = 21;
            break;
        }
        if( sensor_inp(MASK3_3) == 0x60 ) {
            pattern = 11;
        }
        break;

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

    case 22:
        /* Second is skipped. */
        if( cnt1 > 100 ) {
            cnt1 = 0;
            pattern = 23;
        }
        break;

    case 23:
        /* 1 of trace after crossline */
        if( cnt1 > 300 ) {
            cnt1 = 0;
            pattern = 24;
            break;
        }
        switch( sensor_inp(MASK3_3) ) {
            case 0x00:
                /* Center -> Straight  */
                handle( 0 );
                speed( 40 ,40 );
                break;
            case 0x04:
            case 0x06:
            case 0x07:
            case 0x03:
                /* Tending to the left -> Turn to the right  */
                handle( 8 );
                speed( 40 ,36 );
                break;
            case 0x20:
            case 0x60:
            case 0xe0:
            case 0xc0:
                /* Tending to the right -> Turn to the left  */
                handle( -8 );
                speed( 36 ,40 );
                break;
            default:
                break;
        }
        break;

    case 24:
        /* 2 of trace after crossline and crank detection */
        switch( sensor_inp(MASK3_3) ) {
            case 0xe0:

⌨️ 快捷键说明

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