Jump to content
superbrew

MPU6050

Recommended Posts

I have been getting familiar with the Tiva Launchpad and I have started working with the MPU6050. After some help from this forum, I was able to get it up and running and thought I would share some of my results. I have a goal of building a self balancing robot, but I am trying to take tiny steps to get there.

Here is a short video of my progress. I am still a long way off, but I am happy to be progressing. :)

 

 

PS, is there a way to embed youtube videos?

Share this post


Link to post
Share on other sites

Congrats on the progress! You might consider either running your refresh rate a bit higher, or using a better servo. Those smaller servos, in particular the one that was in the video, can be a bit jittery.

 

Do you have any of your code online? I've been working on a Robotics Booster Pack, and it also has a MPU6050. I'm able to pull the raw angles from it, but have yet to get the DMP in the IMU to work. Can't wait until I can start working with Quaternions!

Share this post


Link to post
Share on other sites

Thank you both for the replies.

 

Congrats on the progress! You might consider either running your refresh rate a bit higher, or using a better servo. Those smaller servos, in particular the one that was in the video, can be a bit jittery.

 

Do you have any of your code online? I've been working on a Robotics Booster Pack, and it also has a MPU6050. I'm able to pull the raw angles from it, but have yet to get the DMP in the IMU to work. Can't wait until I can start working with Quaternions!

I am thinking it is the servo, it is just a $3 servo, so I was not expecting much. I raised the refresh rate and increased the resolution of the PWM, but the behavior did not change that much. I suspect a lot of that has to do with my filter for the angle and my PID controller.

 

I do not have any code online. I am a beginner and my code most likely would not help you. I began by modifying the airmouse example, then added in a very simple complimentary filter for the angles. Then I added in the servo code, and a rudimentary PID controller. As I become more familiar with the Timer structure of these uC's I hope to imprive the filter and PID.

Share this post


Link to post
Share on other sites

Wouldn't mind to take a look at that dirty code.

 

Would you mind to share? I was trying to do something similar with mpu-9050 with no luck.

 

Thank you

Share this post


Link to post
Share on other sites

Here is the function I use to collect the data:

uint32_t newTicks;
float
MotionMain(uint32_t ticks)
{
	float gX, gY, gZ, aX, aY, aZ;
	float gyro_angle_x;
	static float oldAngle;
	volatile float dt;
	float alpha = 0.90;

	MPU6050DataRead(&g_sMPU6050Inst, MotionCallback, &g_sMPU6050Inst);

	MotionI2CWait(__FILE__, __LINE__);

	MPU6050DataGyroGetFloat(&g_sMPU6050Inst, &gX, &gY, &gZ);
	MPU6050DataAccelGetFloat(&g_sMPU6050Inst, &aX, &aY, &aZ);

	aX = ((atanf(aY/sqrtf(pow(aX,2)+pow(aZ,2))))*57.296) - g_faoffsetX;

	dt = (float)ticks/1000;

	gyro_angle_x = oldAngle + ((gX-g_fgoffsetX)*dt)*57.296;

	oldAngle = alpha*gyro_angle_x + (1-alpha)*aX;

	return oldAngle;
}

This is only for one axis, in this case X.

Here is the PID routine, I think there is much room for improvement here:

float PID(float setpoint, float input, float Kp, float Ki, float Kd)
{
	static float oldError;
	float error = setpoint - input;
	static float I_err;
	static float D_err;

	if(input > -0.5 && input < 0.5) error = 0;

	I_err += oldError;
	D_err  = error - oldError;

	oldError = error;
	return Kp*error + Ki*I_err + Kd*D_err;
}

And the main loop, this is messy and needs a lot of work:

    ticks = 0;
    oldticks = 0;

    //
    // Drop into the main loop.
    //
    oldticks = g_ui32SysTickCount;
    while(1)
    {
    	angle = MotionMain(ticks);

    	if(UARTPeek('\r') != -1)
    	{
    		UARTgets(command, sizeof(command));
			sscanf(command, "%f,%f,%f", &Kp, &Ki, &Kd);
    	}
		PIDangle = PID(0.0,angle, Kp, Ki, Kd);

    	ui8Adjust = round((-PIDangle));
    	if(firstTime != 0) ROM_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_4, ui8Adjust * ui32Load / 1000);
    	firstTime = 1;
    	SysCtlDelay(SysCtlClockGet()/300);
        ticks = g_ui32SysTickCount - oldticks;
        oldticks = g_ui32SysTickCount;
    }

This code gets 'lost' easily. If I exceed the travel of the servo or move too quickly it can get stuck at one extreme or the other. I think this project is beyond my level for the time being, so I have put it on hold to work my way up.

Share this post


Link to post
Share on other sites

Imo, it's definitely possible. It's a shame that the SensorHub code doesn't use the DMP in the MPU6050.

 

I've been considering porting the FreeIMU or equivalent MPU6050 code to the Tiva/Stellaris Launchpad since I want to make use of the DMP (I want my Quaternions!), but don't have an eta for it (I want to implement an I2C Master ISR first).

Share this post


Link to post
Share on other sites

Imo, it's definitely possible. It's a shame that the SensorHub code doesn't use the DMP in the MPU6050.

 

I've been considering porting the FreeIMU or equivalent MPU6050 code to the Tiva/Stellaris Launchpad since I want to make use of the DMP (I want my Quaternions!), but don't have an eta for it (I want to implement an I2C Master ISR first).

 

I do think the same, such a shame!!

 

If you port the MPU6050 to the Launchpad, please, don't hesitate on sharing it here. I think it could have a lot of applications, and probably, the MPU6050 used with FreeIMU is way more consistent.

 

Would also like to check that I2c Master ISR as well, I am using I2c with a DAC by writting directly the commands from the CPU.

 

Thank you.

Share this post


Link to post
Share on other sites

I am new to Tiva C, i wanna communicate TM4C123GH6PM to MPU 6050. But i have so many unclear points, 

1. Which I2C mode should i use among 4 modes: 

– Master transmit
– Master receive
– Slave transmit
– Slave receive

2. Do we have to configure the MPU 6050 module and what is the order should we do?

Thanks

Share this post


Link to post
Share on other sites

I do think the same, such a shame!!

 

If you port the MPU6050 to the Launchpad, please, don't hesitate on sharing it here. I think it could have a lot of applications, and probably, the MPU6050 used with FreeIMU is way more consistent.

 

Would also like to check that I2c Master ISR as well, I am using I2c with a DAC by writting directly the commands from the CPU.

 

Thank you.

 

@@pak, Unfortunately, I haven't had much luck porting the I2CDev implementation to the Tiva Launchpad.

 

I tried porting it before the competition, but was constantly getting values that didn't make sense (the quaternion was not stable at all. Looked more like gibberish). I gave it another shot last night, but still no go.

 

I've started looking into Invensense's library and am thinking I'll give that a shot instead. While I love the community around I2CDev, I trust the manufacturer to provide much better documentation and resources for the port. We'll see how that goes (might work on it this week).

 

The I2C + ISR implementation was halted due to other things taking priority, and I don't think I'll be resuming work on it for the forseable future. I'll post a link to my work as soon as I've uploaded the code.

 

 

@happysmile09: Your best bet at this moment would be to look at the Energia fork of the Arduino IDE and try using the I2CDev libraries with it. I would also suggest reading up on how I2C and the MPU6050 work.

Share this post


Link to post
Share on other sites

@@pak, Unfortunately, I haven't had much luck porting the I2CDev implementation to the Tiva Launchpad.

 

I tried porting it before the competition, but was constantly getting values that didn't make sense (the quaternion was not stable at all. Looked more like gibberish). I gave it another shot last night, but still no go.

 

I've started looking into Invensense's library and am thinking I'll give that a shot instead. While I love the community around I2CDev, I trust the manufacturer to provide much better documentation and resources for the port. We'll see how that goes (might work on it this week).

 

The I2C + ISR implementation was halted due to other things taking priority, and I don't think I'll be resuming work on it for the forseable future. I'll post a link to my work as soon as I've uploaded the code.

 

 

Thank you MrCruz.

 

I was playing again with MPU9150 without luck, but I already got two GY-521 boards, so I will test your code.

 

Looking forward to hear from you on this topic.

 

Regards

Share this post


Link to post
Share on other sites

Here some quick and dirty I2C/MPU6050 files. I am using bare metal, so there is not a lot of overhead. The interrupt handler and the enable/priority assignment is done by the RTOS I am using, so it's missing here. The only other RTOS feature used is that the ISR wakes up the calling task (wai_flg/set_flg). I could read the data off the MPU6050 with 1kHz sample rate.
 
Perhaps that will got you going quickly faster.
 

- Thomas
 
 
 

Thank you MrCruz.
 
I was playing again with MPU9150 without luck, but I already got two GY-521 boards, so I will test your code.
 
Looking forward to hear from you on this topic.
 
Regards

lm4f120_i2c.c

lm4f120_i2c.h

mpu6050.c

mpu6050.h

Share this post


Link to post
Share on other sites

Here some quick and dirty I2C/MPU6050 files. I am using bare metal, so there is not a lot of overhead. The interrupt handler and the enable/priority assignment is done by the RTOS I am using, so it's missing here. The only other RTOS feature used is that the ISR wakes up the calling task (wai_flg/set_flg). I could read the data off the MPU6050 with 1kHz sample rate.

 

Perhaps that will got you going quickly faster.

 

- Thomas

 Thank you  Thomas.

 

 

 

I am using TIvaware under CCS6 (which is free for Stellaris/Tiva devices).

What IDE did you use?

 

Regards

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...