MPU6050 Need help for the direction and speed

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

hello, here I attached my project, im using mpu6050 sensor to run 2 motors. I should control the speed and the direction of the motors but Im struggling with writing the loop and getting the values for the gyroscope and the acceleration. I really need helps with that so please check the file that I attached. Thank you.

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

It sounds like a fun project!

Although I don't have access to MPU650

and I don't know what kind of motors you are using,

I tried to write a main.c skeleton.

I used a uint16_t for command, but only the first 4 bit is used for determine what to do,

so remaining 12 bit can be used to specify the speed etc.

Needless to say, there are infinite methods to implement what you are saying,

so this is just a sample idea from me.

========================

#include <project.h>

#include <mpu6050.h>

#include <stdio.h>

/* sample command flag */

/* right motor on  0x0001 */

/* left  motor on  0x0002 */

/* right motor back 0x0004 */

/* left  motor back 0x0008 */

#define RIGHT_FORWARD  0x0001

#define RIGHT_BACKWARD 0x0004

#define LEFT_FORWARD  0x0002

#define LEFT_BACKWARD  0x0008

#define STOP          0x0000

#define FORWARD    (RIGHT_FORWARD  | LEFT_FORWARD)

#define BACKWARD  (RIGHT_BACKWARD | LEFT_BACKWARD)

#define TURN_RIGHT (RIGHT_FORWARD)

#define TURN_LEFT  (LEFT_FORWARD)

#define SPIN_RIGHT (RIGHT_FORWARD  | LEFT_BACKWARD)

#define SPIN_LEFT  (RIGHT_BACKWARD | LEFT_FORWARD)

//    RMotor_WriteCompare(100);

//    LMotor_WriteCompare(100);

    //OFFSET VALUES

    float accelXOFF,accelYOFF,accelZOFF;

    float gyroXOFF,gyroYOFF,gyroZOFF;

    //REAL VALUES

    float  ax, ay, az; //acceleration floats

    float  gx, gy, gz; //gyroscope floats

  

void init_hardware(void)

{

    CyGlobalIntEnable;

  

    I2C_MPU6050_Start();

SERIAL_Start();

    RMotor_Start();

    LMotor_Start();

    ClockRMotor_Start();

    ClockLMotor_Start();

MPU6050_init();

MPU6050_initialize();

}

/**

* get_sensor_data

* acquire data from MPU650

*/

void get_sensor_data(void)

{

    //RAW VALUES

    int16_t accelX, accelY, accelZ; // variables for accelerometer raw data

    int16_t gyroX, gyroY, gyroZ; // variables for gyro raw data

    accelX = MPU6050_getAccelerationX();

    accelY = MPU6050_getAccelerationY();

    accelZ = MPU6050_getAccelerationZ();

  

    gyroX = MPU6050_getRotationX();

    gyroY = MPU6050_getRotationY();

    gyroZ = MPU6050_getRotationZ();

    accelXOFF = accelXOFF + accelX;

    accelYOFF = accelYOFF + accelY;

    accelZOFF = accelZOFF + accelZ;

    gyroXOFF = gyroXOFF + gyroX;

    gyroYOFF = gyroYOFF + gyroY;

    gyroZOFF = gyroZOFF + gyroZ;  

    accelXOFF = accelXOFF/100;

    accelYOFF = accelYOFF/100;

    accelZOFF = accelZOFF/100;

    gyroXOFF = gyroXOFF/100;

    gyroYOFF = gyroYOFF/100;

    gyroZOFF = gyroZOFF/100;

}

/**

* calc_what_do_do

* calculate the direction etc

* and decide what to command to the motors

*/

uint16_t calc_what_to_do(void)

{

    uint16_t what_to_do = STOP ;

  

    /* calc and decide what to let the motors do */

    /* forward, backward, turn etc */

    /* turn can be slow moving turn

    * or spin at current place

    *

    */

  

    return( what_to_do ) ;

}

void do_motors(uint16_t command)

{

    switch(command & 0x000F) { /* currently using only 4 bit */

    case FORWARD:

        /* turn both motors to forward direction */

        break ;

    case BACKWARD:

        /* turn both motors to backword direction */

        break ;

    case TURN_RIGHT:

        /* turn right motor forward direction */

        /* stop left motor or slower forward direction  */

        break ;

    case TURN_LEFT:

        /* stop right motor or slower forward direction */

        /* turn left motor forward direction */

        break ;

    case SPIN_RIGHT:

        /* turn right motor forward */

        /* turn left motor backward */

        break ;

    case SPIN_LEFT:

        /* turn right motor backward */

        /* turn left motor forward */

        break ;

    case STOP:

    default:

        /* stop both motors */

        break ;

    }

}

int main()

{

    uint32_t command ;

  

    init_hardware() ;

  

    for(;;)

    {

        get_sensor_data() ;

        command = calc_what_to_do() ;

        do_motors(command) ;

        CyDelay(15); /* 15ms delay */

    }

}

========================

moto

View solution in original post

0 Likes
4 Replies
Anonymous
Not applicable

bob.marlowe​ I attached the new files

0 Likes

The file i2cfunctions.c and probably .h are missing from the project, so I cannot compile successfully.

You will need to put reads from the MPU and writes to the motor-drivers into the for-loop just before the CyDelay()

Check whether A0 of MPU is tied high, otherwise use I2C-address 0x68h

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

It sounds like a fun project!

Although I don't have access to MPU650

and I don't know what kind of motors you are using,

I tried to write a main.c skeleton.

I used a uint16_t for command, but only the first 4 bit is used for determine what to do,

so remaining 12 bit can be used to specify the speed etc.

Needless to say, there are infinite methods to implement what you are saying,

so this is just a sample idea from me.

========================

#include <project.h>

#include <mpu6050.h>

#include <stdio.h>

/* sample command flag */

/* right motor on  0x0001 */

/* left  motor on  0x0002 */

/* right motor back 0x0004 */

/* left  motor back 0x0008 */

#define RIGHT_FORWARD  0x0001

#define RIGHT_BACKWARD 0x0004

#define LEFT_FORWARD  0x0002

#define LEFT_BACKWARD  0x0008

#define STOP          0x0000

#define FORWARD    (RIGHT_FORWARD  | LEFT_FORWARD)

#define BACKWARD  (RIGHT_BACKWARD | LEFT_BACKWARD)

#define TURN_RIGHT (RIGHT_FORWARD)

#define TURN_LEFT  (LEFT_FORWARD)

#define SPIN_RIGHT (RIGHT_FORWARD  | LEFT_BACKWARD)

#define SPIN_LEFT  (RIGHT_BACKWARD | LEFT_FORWARD)

//    RMotor_WriteCompare(100);

//    LMotor_WriteCompare(100);

    //OFFSET VALUES

    float accelXOFF,accelYOFF,accelZOFF;

    float gyroXOFF,gyroYOFF,gyroZOFF;

    //REAL VALUES

    float  ax, ay, az; //acceleration floats

    float  gx, gy, gz; //gyroscope floats

  

void init_hardware(void)

{

    CyGlobalIntEnable;

  

    I2C_MPU6050_Start();

SERIAL_Start();

    RMotor_Start();

    LMotor_Start();

    ClockRMotor_Start();

    ClockLMotor_Start();

MPU6050_init();

MPU6050_initialize();

}

/**

* get_sensor_data

* acquire data from MPU650

*/

void get_sensor_data(void)

{

    //RAW VALUES

    int16_t accelX, accelY, accelZ; // variables for accelerometer raw data

    int16_t gyroX, gyroY, gyroZ; // variables for gyro raw data

    accelX = MPU6050_getAccelerationX();

    accelY = MPU6050_getAccelerationY();

    accelZ = MPU6050_getAccelerationZ();

  

    gyroX = MPU6050_getRotationX();

    gyroY = MPU6050_getRotationY();

    gyroZ = MPU6050_getRotationZ();

    accelXOFF = accelXOFF + accelX;

    accelYOFF = accelYOFF + accelY;

    accelZOFF = accelZOFF + accelZ;

    gyroXOFF = gyroXOFF + gyroX;

    gyroYOFF = gyroYOFF + gyroY;

    gyroZOFF = gyroZOFF + gyroZ;  

    accelXOFF = accelXOFF/100;

    accelYOFF = accelYOFF/100;

    accelZOFF = accelZOFF/100;

    gyroXOFF = gyroXOFF/100;

    gyroYOFF = gyroYOFF/100;

    gyroZOFF = gyroZOFF/100;

}

/**

* calc_what_do_do

* calculate the direction etc

* and decide what to command to the motors

*/

uint16_t calc_what_to_do(void)

{

    uint16_t what_to_do = STOP ;

  

    /* calc and decide what to let the motors do */

    /* forward, backward, turn etc */

    /* turn can be slow moving turn

    * or spin at current place

    *

    */

  

    return( what_to_do ) ;

}

void do_motors(uint16_t command)

{

    switch(command & 0x000F) { /* currently using only 4 bit */

    case FORWARD:

        /* turn both motors to forward direction */

        break ;

    case BACKWARD:

        /* turn both motors to backword direction */

        break ;

    case TURN_RIGHT:

        /* turn right motor forward direction */

        /* stop left motor or slower forward direction  */

        break ;

    case TURN_LEFT:

        /* stop right motor or slower forward direction */

        /* turn left motor forward direction */

        break ;

    case SPIN_RIGHT:

        /* turn right motor forward */

        /* turn left motor backward */

        break ;

    case SPIN_LEFT:

        /* turn right motor backward */

        /* turn left motor forward */

        break ;

    case STOP:

    default:

        /* stop both motors */

        break ;

    }

}

int main()

{

    uint32_t command ;

  

    init_hardware() ;

  

    for(;;)

    {

        get_sensor_data() ;

        command = calc_what_to_do() ;

        do_motors(command) ;

        CyDelay(15); /* 15ms delay */

    }

}

========================

moto

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA
0 Likes