Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The purpose of this code is to initially synchronize two motors with the push of

ID: 3828326 • Letter: T

Question

The purpose of this code is to initially synchronize two motors with the push of button 1 and with the push of button 2 start driving straight for 5 seconds and then drive in revers for 5 seconds. The motors are run using the PWM method. Synchronization will be accomplished by changing the duty cycle of the individual motors.

This code must be written in C.

(Hint: Use the HB5 Library)

Components Used:

2 H-Bridge modules (Pmod HB5: H-bridge Driver with Feedback Inputs)

2 DC Motors (DC Motor/Gearbox (1:19 Gear Ratio): Custom 12V Motor Designed for Digilent Robot Kits)

Micro Controller (chipKIT Pro MX4 : Embedded Systems Trainer Board)

Problem: I can’t synchronize my motors because I am not able to add the HB5 library and have no idea how to use it. There is a function in the library that will read the rpm of the motor, which I think will be helpful. Please give me code that will synchronize my motors. We need to re-synchronize every time we start the program since the wheels will be driving on different surfaces. The objective is to make the motors synch, no matter what surface we are running on, then drive straight and in then in reverse.

Please post the C code with comments and how you got the motors to synch.

Here is what I have in my code so far. It doesn’t work

#include <xc.h>

#include <sys/attribs.h>

//configure clock

#pragma config FNOSC=PRIPLL

#pragma config POSCMOD=EC

#pragma config FPLLIDIV=DIV_2

#pragma config FPLLMUL=MUL_20

#pragma config FPLLODIV=DIV_1

#pragma config FPBDIV=DIV_8

int R;

void main(){

    InitializeIO();

    InitializeTIMERS();

   

    while(1)

    {

        calibrate();

        WaitOnBtn1();

        Forward(4.0,R);

        Stop(10.0);

//        Backward(3.0,R);

//        Stop(10.0);

//        Right(3.0,.2);

//        Left(3.0,.2);

//        Stop();

        R=0;

    }

   ;

}

void calibrate(){

    Forward(2.0,.7);

   

     int Scount;

    int S2count;

    int Sa1=PORTGbits.RG14;

    int Sa2=PORTFbits.RF0;

         while(TMR1<400){

        if(Sa1=1){

            Scount+1;

        }

        else{

        }

    }

    while(TMR1<400){

        if(Sa2=1){

            S2count+1;

        }

  

        else{

        }

    }

    Stop(2);

    R=(Scount/S2count);

    return R;

    }

void InitializeIO(){

    TRISAbits.TRISA6=1;

    TRISAbits.TRISA7=1;

    TRISGbits.TRISG12=0;

    TRISGbits.TRISG13=0;

    LATGbits.LATG12=0;

    LATGbits.LATG13=0;

    TRISGbits.TRISG0=0;

    TRISGbits.TRISG1=0;

   

    TRISBbits.TRISB10=0;

    TRISBbits.TRISB11=0;

    TRISBbits.TRISB12=0;

    TRISBbits.TRISB13=0;

   

    return;

}

void InitializeTIMERS(){

    T1CON=0x0000;

    T1CONbits.TCKPS=3;

    T1CONbits.ON=1;

    PR1=0xFFFF;

    TMR1=0;

   

    T2CON=0;

    T2CONbits.TCKPS=7;

    T2CONbits.T32=1;

    T2CONbits.ON=1;

    PR2=0xFFFFFFFF;

    TMR2=0;

   

    return;

   

}

void WaitOnBtn1(){

    while(PORTAbits.RA6==0);

    LATBbits.LATB10=1;

    return;

}

void Forward(float Sec, int D,int x){

    int RunTime=(int)(Sec*39000);

    TMR2=0;

  

    LATGbits.LATG12=1;

    LATGbits.LATG0=0;

    LATBbits.LATB12=0;

    LATBbits.LATB13=0;

    LATBbits.LATB11=1;

      while (TMR2< RunTime){

          PWM(D);

      

    }

    return;

}

void Backward(float Sec, int D){

    int RunTime=(int)(Sec*39000);

    TMR2=0;

    LATGbits.LATG13=0;

     LATGbits.LATG0=1;

    LATBbits.LATB12=1;

    LATBbits.LATB13=0;

    LATBbits.LATB11=0;

    while (TMR2< RunTime){

         PWM(D);

      

    }

    return;

}

void Right(float Sec, int D,int x){

    int RunTime=(int)(Sec*39000);

    TMR2=0;

    LATGbits.LATG12=1;

     LATGbits.LATG0=1;

    LATBbits.LATB12=1;

    LATBbits.LATB13=0;

    LATBbits.LATB11=0;

    while (TMR2< RunTime){

          PWM(D);

      ;

    }

    return;

}

void Left(float Sec, int D){

    int RunTime=(int)(Sec*39000);

    TMR2=0;

    LATGbits.LATG12=0;

     LATGbits.LATG0=0;

    LATBbits.LATB12=1;

    LATBbits.LATB13=0;

    LATBbits.LATB11=0;

    while (TMR2< RunTime){

        PWM(D);

       

    }

    return;

}

void Stop(float Sec){

    int RunTime=(int)(Sec*39000);

    TMR2=0;

    LATGbits.LATG13=0;

     LATGbits.LATG1=0;

    LATBbits.LATB12=0;

    LATBbits.LATB13=0;

    LATBbits.LATB11=0;

    while(TMR2<RunTime);

    return;

}

void PWM(int D){

    TMR1=0;

    int Period=400;

    while(TMR1<Period){

    if(TMR1<Period*D/100){

        LATGbits.LATG13=1;

         LATGbits.LATG1=1;

    }

    else{

        LATGbits.LATG13=0;

         LATGbits.LATG1=0;

    }

    }

    return;

}

//

//void PWM2(int x){

//    TMR1=0;

//    int Period=400;

//    while(TMR1<Period){

//    if(TMR1<Period*x/100){

//        LATGbits.LATG13=1;

//         LATGbits.LATG1=1;

//    }

//    else{

//        LATGbits.LATG13=0;

//         LATGbits.LATG1=0;

//    }

//    }

//    return;

//}

Explanation / Answer

task main() //Assume encoderA is the 'master' motor, and you want to synch the other motors to it.

{

   int speedB;

   speedB=motor[motorA]=50;

   while(true)      //Creates an infinite loop, since "true" always evaluates to true

   {

      if(SensorValue[encoderB] == SensorValue[encoderA])//If the encoder values are equal

      {

         motor[motorB] = speedB; //Maintain speed     

      }

      else if(SensorValue[encoderB] > SensorValue[encoderA])//If encoderB is greater than encoderA (moving faster)

      {

         speedB = speedB - 1; //slow it down

         motor[motorB]=speedB;

      }

      else if(SensorValue[encoderB] < SensorValue[encoderA])//If encoderB is less than encoderA (moving slower)

      {

         speedB = speedB + 1; //speed it up

         motor[motorB] = speedB;

      }

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote