CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

receiving data on pic16F from an accelerometer MPU6050
Goto page Previous  1, 2, 3, 4, 5, 6  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Fri Jan 27, 2023 5:57 pm     Reply with quote

temtronic wrote:
The PIC like every other computer with just one ALU, can only do ONE operation at a time.
It cannot do ANYTHING 'inside' of a delay_ms(xxxx). it literally just decrements a counter (or two) wasting time, until the count is zero.
Again, would have to see main() to understand WHY there's an inline delay.


I know what you mean but i would have said, maybe this isn't good for the pic to calculate constantly without a delay between every hard operations (I know that my thoughts look stupid but I'm just looking for an answer ^^) i will show you the code tomorrow i don't have it now.

And more, i had a problem on same code last time, i would have used a negative value in a if condition but the compiler says "condition always TRUE" (or false) even if i add parenthesis around the number like this:
Code:

If (variable <= (-1200))
{

   Do things
}

I know i already have done that in a previous code but i don't remember if there is a special syntax for negative value in if condition.
Ttelmah



Joined: 11 Mar 2010
Posts: 19553

View user's profile Send private message

PostPosted: Sat Jan 28, 2023 2:45 am     Reply with quote

obviously 'variable' must be signed.
temtronic



Joined: 01 Jul 2010
Posts: 9247
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jan 28, 2023 7:33 am     Reply with quote

and...

CCS historically defaults int8 and int16 as unsigned

if memory serves me right...
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Sat Jan 28, 2023 8:04 am     Reply with quote

temtronic wrote:
and...

CCS historically defaults int8 and int16 as unsigned

if memory serves me right...


im not sure, but this morning i tried again i declared the variable as a "int" and it magically worked so i m confused...

but if one day i've got this problem again i will think about your advices thank you.
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Sat Jan 28, 2023 8:18 am     Reply with quote

So, this is my main with the delay that i talked about :

Code:

void main()
{
Setup_mpu6050();   


while(TRUE)
  {
   Get_Accel_Values();
 
   Get_Accel_Angles();

   printf("accel_xangle = %7.0f \n\r", accel_xangle);
   printf("accel_yangle = %7.0f \n\r", accel_yangle);
   printf("\n\r");
   delay_ms(2000);
  }


do you think that the purpose is only to print every 2 seconds on terminal then?
PrinceNai



Joined: 31 Oct 2016
Posts: 480
Location: Montenegro

View user's profile Send private message

PostPosted: Sat Jan 28, 2023 9:42 am     Reply with quote

Yes, the purpose is exactly that. 2 seconds between two readings of the sensor and prints. It would work even without this delay (if the sensor supports consecutive readings at that speed), only the printouts would come out way faster.
temtronic



Joined: 01 Jul 2010
Posts: 9247
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jan 28, 2023 9:47 am     Reply with quote

well the MPU6050 is listed as NOT for 'new' designs....so I couldn't get the datasheet from the MANUFACTURER. However considering the chip is I2C, it must be reasonably 'fast' so the only reason for the 2 second delay in main() IS to slow the 'update to display' for us humans to easily read it.

You can reduce it to 1 second, 1/4 second , even less if you want. How short a delay time period depends on what you do with the data in the PC. If just displaying it,using a terminal program, delay_ms(500) would be fine. If using it for some kind of 'real-time' control, delay_ms(1) might be appropriate.
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Sat Jan 28, 2023 10:08 am     Reply with quote

PrinceNai wrote:
Yes, the purpose is exactly that. 2 seconds between two readings of the sensor and prints. It would work even without this delay (if the sensor supports consecutive readings at that speed), only the printouts would come out way faster.


Thanks for your answer, i think i will try with 500ms then to be sure that there is no problem, it should be enough for my project.
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Sat Jan 28, 2023 10:14 am     Reply with quote

temtronic wrote:
well the MPU6050 is listed as NOT for 'new' designs....so I couldn't get the datasheet from the MANUFACTURER. However considering the chip is I2C, it must be reasonably 'fast' so the only reason for the 2 second delay in main() IS to slow the 'update to display' for us humans to easily read it.

You can reduce it to 1 second, 1/4 second , even less if you want. How short a delay time period depends on what you do with the data in the PC. If just displaying it,using a terminal program, delay_ms(500) would be fine. If using it for some kind of 'real-time' control, delay_ms(1) might be appropriate.


The goal is to drive 4 actuators that support a plank and then make it flat. So i will do some tests with differents delays and see what happen.
But first i will check tonight the code library about the function atan to optimize it.
temtronic



Joined: 01 Jul 2010
Posts: 9247
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jan 28, 2023 10:25 am     Reply with quote

Please remember that the delay_ms(2000) does NOTHING with respect to getting the raw sensor data or any 'mathematics' the PIC is asked to do.

You'll probably see that the CCS coding for ATAN is already optimized, same as the other 'trig' functions. You can compare their code to some Microchip Application Notes' on the subject.

As we've stated though to get faster and more accurate response, use scaled integers.

BTW using 3 actuators instead of 4 for the 'level a plank' is easier.
PrinceNai



Joined: 31 Oct 2016
Posts: 480
Location: Montenegro

View user's profile Send private message

PostPosted: Sat Jan 28, 2023 11:09 am     Reply with quote

Quote:

BTW using 3 actuators instead of 4 for the 'level a plank' is easier.


Well, as they showed me at mathematics class decades ago: 3 points define a plane :-)
PrinceNai



Joined: 31 Oct 2016
Posts: 480
Location: Montenegro

View user's profile Send private message

PostPosted: Sat Jan 28, 2023 11:12 am     Reply with quote

That is why a 3 legged bar table never rocks, while a 4 legged always does :-)
PrinceNai



Joined: 31 Oct 2016
Posts: 480
Location: Montenegro

View user's profile Send private message

PostPosted: Sat Jan 28, 2023 11:37 am     Reply with quote

Let's presume the plank doesn't bend. I'd place two sensors aligned on the plank border along x axis, and one on the opposite side, in a triangle formation, all sensors having x and y axis facing the same directions. Here I noticed you actually need only two actuators, one corner can be fixed. Then lift or lower the actuator on x axis so that both sensors there read 0 degrees. You are done with leveling on that axis. Than you lift or lower the actuator on the opposite side of the plank so that y axis reading from that and any other sensor reads 0 deg. You have a leveled plank.

I'm not sure if I explained it good enough, but that is the way we level cylinders in the casino, only instead of actuators we use a technician.

Can of course be done with only one sensor, it's just my one-track thinking you'd need three :-)
AKA TENDO



Joined: 25 Jan 2023
Posts: 47

View user's profile Send private message

PostPosted: Sat Jan 28, 2023 3:49 pm     Reply with quote

PrinceNai wrote:
Let's presume the plank doesn't bend. I'd place two sensors aligned on the plank border along x axis, and one on the opposite side, in a triangle formation, all sensors having x and y axis facing the same directions. Here I noticed you actually need only two actuators, one corner can be fixed. Then lift or lower the actuator on x axis so that both sensors there read 0 degrees. You are done with leveling on that axis. Than you lift or lower the actuator on the opposite side of the plank so that y axis reading from that and any other sensor reads 0 deg. You have a leveled plank.

I'm not sure if I explained it good enough, but that is the way we level cylinders in the casino, only instead of actuators we use a technician.

Can of course be done with only one sensor, it's just my one-track thinking you'd need three :-)



I understand don't worry, but the thing is that i can't modify the project even though what you said was very interesting. I m forced to use one sensor and 4 actuators ^^.
PrinceNai



Joined: 31 Oct 2016
Posts: 480
Location: Montenegro

View user's profile Send private message

PostPosted: Sat Jan 28, 2023 3:54 pm     Reply with quote

Sorry, I over complicated my previous post. One sensor, two actuators would do. I have a question, since you have to use four actuators: how will you know that the plank sits on all four? With three points there is no other way, but with four...

Last edited by PrinceNai on Sat Jan 28, 2023 3:56 pm; edited 1 time in total
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3, 4, 5, 6  Next
Page 3 of 6

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group