Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code must be written in MPlab. Not Arduino. Code must be specific to this chip and board. Code that is posted has comments The objective

Code must be written in MPlab. Not Arduino. Code must be specific to this chip and board. Code that is posted has comments

The objective is to have the Robot SEARCH for a black line. When a black line is detected the robot will then FOLLOW the black line. If the line ends the robot will turn around and go back following the black line again. This will go on forever.

I have the code completed up to the point where I have simple control of both motors and some driving functions.

I NEAD YOU TO COMPLET THE LOGIC PORTION OF THIS ROBOT.

The CODE IS IN C

I am USING THE chipKIT Pro MX4 : Embedded Systems Trainer Board. With The Microchip PIC32MX460F512L.

I am Programing in MPLAB X IDE V3.50

I am using 4 IR Proximity Sensors. When the Line is black, the reading is 0 and when it is white the sensor reads 1.

The motors are controlled using a PWM ISR operation and are controlled using the OC2 and OC3 registers.

Please comment all the code so I can follow along.

Please post the entire Code when finished. The robot must make it all the way around.

Bellow is a picture of the course the robot will be running.

image text in transcribed

My CODE:

/* * Included Libraries */ #include /* contains Vector Name/Number Macros */ #include /* contains __ISR() Macros */ #include

/* * Oscillator Settings * Timer Set up PLL */ #pragma config FNOSC = PRIPLL // Oscillator selection #pragma config POSCMOD = EC // Primary oscillator mode #pragma config FPLLIDIV = DIV_2 // PLL input divider #pragma config FPLLMUL = MUL_20 // PLL multiplier #pragma config FPLLODIV = DIV_1 // PLL output divider #pragma config FPBDIV = DIV_8 // Peripheral bus clock divider

/* * */ void InitializeSystem(); void Initialize_IO(); void Initialize_Timer1(); void Initialize_Timer2(); void Initialize_Timer3(); void Initialize_OC(); void Initialize_IC(); void Interrupts_enable(); void Motor_Forward(); void Stop(); void TurnLeft(); void TurnRight(); void Motor_Backward(); void Check_Min_OC();

/* * Main Driver */ void main(){ InitializeSystem(); Initialize_IO(); Initialize_Timer1(); Initialize_Timer2(); Initialize_Timer3(); Initialize_OC(); Initialize_IC(); Interrupts_enable();

/* * Infinit loop */ while(1){

/* IR sensor registers on board F13 = 1;//LL F4 = 1;//LC F5 = 1;//RC F12 = 1;//RR */ /* Motor Registers on Board D6 = left Motor D7 = Right Motor */ /* Logic section the make robot follow the line */ //Left sensor black, right sensor white, turn left if (PORTFbits.RF13 == 1 && PORTFbits.RF12 == 0) { LATBbits.LATB5 = 1; TurnLeft(); }

//Left sensor white, right sensor black, turn right else if (PORTFbits.RF13 == 0 && PORTFbits.RF12 == 1) { LATBbits.LATB6 = 1; TurnRight(); }

//Both sensors Black, go forward else if (PORTFbits.RF4 == 0 && PORTFbits.RF5 == 0) { LATBbits.LATB5 = 1; LATBbits.LATB6 = 1; Motor_Forward(); }

} }

void InitializeSystem(){ Initialize_IO(); Initialize_Timer1(); Initialize_Timer2(); Initialize_Timer3(); Initialize_OC(); Initialize_IC();

}

void Initialize_IO(){ // Motor Direction bits TRISDbits.TRISD6 =0; LATDbits.LATD6 = 0; TRISDbits.TRISD7 =0; LATDbits.LATD7 = 0; TRISDbits.TRISD1 =0; LATDbits.LATD1 = 0; TRISDbits.TRISD2 =0; LATDbits.LATD2 =0; // truns on IR sensor; TRISFbits.TRISF13 = 1;//LL TRISFbits.TRISF4 = 1;//LC TRISFbits.TRISF5 = 1;//RC TRISFbits.TRISF12 = 1;//RR }

void Initialize_Timer1(){ T1CONbits.TON = 0; T1CONbits.TCKPS = 3; PR1 = 0xFFFF; TMR1 = 0; T1CONbits.ON =1;; } void Initialize_Timer2(){ T2CONbits.TON = 0; T2CONbits.TCKPS = 7; PR2 = 500; TMR2 = 0; T2CONbits.ON =1;; } void Initialize_Timer3(){ T3CONbits.TON = 0; T3CONbits.TCKPS = 7; PR3 = 0xFFFF; TMR3 = 0; T3CONbits.ON =1;; } void Initialize_OC(){ // output compare OC2CONbits.OCM =6; OC2R = 100; OC2RS = 100; OC2CONbits.ON=1; OC3CONbits.OCM =6; OC3R = 100; OC3RS = 100; OC3CONbits.ON = 1; /* Set Interrupt Controller for multi-vector mode */ } void Initialize_IC(){ // Initialize IC2 TRISDbits.TRISD9 = 1; IC2CON= 0x0000; IC2CONbits.SIDL = 1; // halt in CPU Idle Mode IC2CONbits.C32 = 0; // 16bit timer; IC2CONbits.ICTMR = 0; // select Timer 3; IC2CONbits.ICI = 3; // interrupt every 4 events IC2CONbits.ICM = 5; // increment every 16 rising edge // Initialize IC3 TRISDbits.TRISD10 = 1; IC3CON= 0x0000; IC3CONbits.SIDL = 1; // halt in CPU Idle Mode IC3CONbits.C32 = 0; // 16bit timer; IC3CONbits.ICTMR = 0; // select Timer 3; IC3CONbits.ICI = 3; // interrupt every fourth event IC3CONbits.ICM = 5; // increment every 16 rising edge }

void Interrupts_enable(){ // Timer one interrupt is enabled when it enters the Path search mode IPC1bits.T1IP = 7; IFS0bits.T1IF = 0; IEC0bits.T1IE = 0; // Interrupt for IC2 IFS0bits.IC2IF =0; IPC2bits.IC2IP = 7; IEC0bits.IC2IE = 1; // Interrupt for IC3 IFS0bits.IC3IF =0; IPC3bits.IC3IP = 7; IEC0bits.IC3IE = 1; // turn ON IC2 & IC3 IC2CONbits.ON = 1; IC3CONbits.ON= 1; //// Interrupt for Timer1 IFS0bits.T1IF =0; IPC1bits.T1IP = 7; IEC0bits.T1IE = 1; INTCONbits.MVEC = 1; __builtin_enable_interrupts(); } void Motor_Forward(){ LATDbits.LATD6 = 0; LATDbits.LATD7 = 1; } void Stop(){ OC2RS = 0; OC3RS = 0; } void TurnLeft(){ OC2RS = 300; OC3RS = 0; } void TurnRight(){ OC2RS = 0; OC3RS = 300; } void Motor_Backward(){ LATDbits.LATD6 = 1; LATDbits.LATD7 = 0; OC2RS = 500; OC3RS = 500; } void Check_Min_OC(){ if (OC2RS

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions