Page 1 of 3

Trigonometry Causing PWM to stop

Posted: Sun Aug 21, 2011 4:58 am
by Microman171
Hey,

I'm working on a quad rotor project using the ChibiOS system. You can see the full source here: https://github.com/dancollins/Quad-Roto ... /QuadRotor

The problem is in this piece of code:

Code: Select all

static msg_t ControllerThread(void *arg) { // Controller thread

   (void)arg; // Don't need arguments...

   

   systime_t time = chTimeNow();

   

   int32_t pitch_angle_accum = 0;

   int32_t roll_angle_accum = 0;

   

   while (1) {

      accel_read();

      gyro_read();

      

      //IMUupdate(gyro.x * 0.0012141420883438813, gyro.y * 0.0012141420883438813, gyro.z * 0.0012141420883438813, accel.x, accel.y, accel.z);

      

      float norm_x = 2*(q0*q2 - q1*q3);

      float norm_y = 2*(q0*q1 + q2*q3);

      float norm_z = (1 - 2*(q1*q1 + q2*q2));

      

      /*float elev = acosf(norm_z);

      float azim = atan2f(norm_y, norm_x);



      /*int16_t pitch = elev * sinf(azim) / M_PI * 32767;

      int16_t roll = elev * cosf(azim) / M_PI * 32767;

      int16_t yaw = 0;

      

      /*int16_t pitch_angle_target = 0; // TODO: Fix

      int16_t roll_angle_target = 0; // TODO: Fix



      int16_t pitch_angle_error = pitch_angle_target - pitch;

      int16_t roll_angle_error = roll_angle_target - roll;



      // 25 deg max error.

      pitch_angle_error = saturate(pitch_angle_error, 4551);

      roll_angle_error = saturate(roll_angle_error, 4551);



      pitch_angle_accum += pitch_angle_error;

      roll_angle_accum += roll_angle_error;

      

      // 20 deg s max error.

      pitch_angle_accum = saturate(pitch_angle_accum, 364088);

      roll_angle_accum = saturate(roll_angle_accum, 364088);



      int32_t pitch_rate_target = (pitch_angle_error * 2 * 65536 + pitch_angle_accum * 98) >> 16;

      int32_t roll_rate_target = (roll_angle_error * 2 * 65536 + roll_angle_accum * 98) >> 16;



      int16_t pitch_rate_comp = ((pitch_rate_target - (gyro.x * 4000 / 360)) * 6 * 36) >> 16;

      int16_t roll_rate_comp = ((roll_rate_target - (gyro.y * 4000 / 360)) * 6 * 36) >> 16;



      pitch_rate_comp = saturate(pitch_rate_comp, 250);

      roll_rate_comp = saturate(roll_rate_comp, 250);



      motor_set(0, pitch_rate_comp, roll_rate_comp, 0);*/
      

      time += MS2ST(100);

      chThdSleepUntil(time);

   }

   return 0;

}


How the code is commented above results in successful PWM (the motors are continually refreshed). However, removing the first '/*' (commenting out the elev and azim) results in the PWM's refresh loop no long running. Same goes if the IMUUpdate function is uncommented.

math.h is correctly included, and linked (using the "-lm" flag), and the functions do not generate any warnings in the compiler. However, they seem to prevent the PWM working. The system does not hang, as the blinking LED thread still runs correctly.

Any advice would be much appreciated.


Cheers,

MM

Re: Trigonometry Causing PWM to stop

Posted: Sun Aug 21, 2011 6:58 am
by Giovanni
Hi,

This looks more like a compiler problem than something related to the OS or the PWM driver.

Anyway your code is not very readable, I see some comments open /* but not were comments are closed with a */. Comments should always be closed and cannot be nested, not sure if this is related to your problem. It is possible the code just shows incorrectly in the forum.

Anyway, it is possible that the problem is related to the floating point libraries, the cortex-m3 does not have hardware floating point so make sure you are giving the compiler the proper options to use software floating point.

I suggest to create a function without any OS or PWM code inside but just your FP math and see if it works correctly. Dividing the problem in smaller parts often makes finding the solution easier.

Giovanni

Re: Trigonometry Causing PWM to stop

Posted: Sun Aug 21, 2011 8:26 am
by Microman171
Hey,

I'm pretty lazy with commenting out stuff sometimes. There are no compiler errors with having the comments like that, and it makes it easy to uncomment smaller chunks. Sorry it makes things hard to read.

I've done a little searching on floating point with the arm-none-eabi-gcc compiler, and found I need to add "-mfloat-abi=soft" to the options. I did this, and recompiled the relevant source files. Unfortunatly this did not fix the problem.

Having never needed to work with floating point math before, it's possible I just missed something simple?

As for getting some code to work on the device without the OS; I have no idea how to do this. Firstly, I don't know how the lower level parts of the system work or how to initiate the controller, as they seem more complicated than the 8/16bit microcontrollers I normally use. Next, I'm not sure how I would output the data if it was successful.

By using an LED, I have found that enabling the trig functions / floating point math stops the callback function from ever being called. The blinky thread continues to run fine.


Thanks for helping out so far,

MM

Re: Trigonometry Causing PWM to stop

Posted: Sun Aug 21, 2011 9:03 am
by Giovanni
Are you able to do debugging?

Put a breakpoint into that function and then go step by step, if it is a FP problem then probably an exception will be triggered at some point.

Also try to increase the stack size of that thread, it is possible you are hitting a stack overflow.

About running things without an OS, if you use ChibiOS/RT and do not spawn any thread then you are doing just that, do everything in the main() function and use the OS for I/O only, you can also perform low level I/O if you need.

Anyway I doubt that the OS the is the problem in your case, you need to make sure you are using the correct FP options and libraries for your compiler.

Giovanni

Re: Trigonometry Causing PWM to stop

Posted: Mon Aug 22, 2011 6:48 am
by Microman171
Hey,

No way to debug at the moment. Investing in a JTAG debugger is something I haven't done yet.

Originally, the stack size was 128 bytes, and did cause a stack overflow. It took me a while to think of trying a different stack size, and doing so solved the overflow problem. I tried, after reading your post, to increase it from a working 256 bytes to 1024 bytes. Unfortunately, but not surprisingly, it did not fix the problem.

When I get the chance, I will try some floating point math with just the main thread. If this works, then perhaps I should merge the I2C thread into the main thread.

As far as floating point options in the compiler go, I have no idea what I need to set. If my floating point tests fail, then I will continue to look into it.


Cheers,

MM

Re: Trigonometry Causing PWM to stop

Posted: Mon Aug 22, 2011 7:30 am
by Giovanni
What compiler are you using? CodeSourcery?

Giovanni

Re: Trigonometry Causing PWM to stop

Posted: Mon Aug 22, 2011 8:45 am
by Microman171
I am using this tool chain: https://github.com/jsnyder/arm-eabi-toolchain which does meantion code sourcery in the description.

Re: Trigonometry Causing PWM to stop

Posted: Mon Aug 22, 2011 11:22 am
by mabl
Hello Microman171,

I also use this toolchain, but had some trouble with the built-in functions, in my case i.e. strcmp.

I did some small changes to the makefile which seem to solve my problems:

Code: Select all

diff --git a/Makefile b/Makefile
index 76105b0..edcde95 100644
--- a/Makefile
+++ b/Makefile
@@ -2,18 +2,23 @@ SHELL = /bin/bash
 TARGET=arm-none-eabi
 PREFIX=$(HOME)/arm-cs-tools/
 PROCS=4
-CS_BASE = 2010.09
-CS_REV = 51
+#CS_BASE = 2010.09
+#CS_REV = 51
+CS_BASE = 2011.03
+CS_REV = 42
 GCC_VERSION = 4.5
 MPC_VERSION = 0.8.1
 CS_VERSION = $(CS_BASE)-$(CS_REV)
 LOCAL_BASE = arm-$(CS_VERSION)-arm-none-eabi
 LOCAL_SOURCE = $(LOCAL_BASE).src.tar.bz2
 LOCAL_BIN = $(LOCAL_BASE)-i686-pc-linux-gnu.tar.bz2
-SOURCE_URL = http://www.codesourcery.com/sgpp/lite/arm/portal/package7812/public/arm-none-eabi/$(LOCAL_SOURCE)
+#SOURCE_URL = http://www.codesourcery.com/sgpp/lite/arm/portal/package7812/public/arm-none-eabi/$(LOCAL_SOURCE)
 BIN_URL = http://www.codesourcery.com/sgpp/lite/arm/portal/package7813/public/arm-none-eabi/$(LOCAL_BIN)
 
-SOURCE_MD5_CHCKSUM = 0ab992015a71443efbf3654f33ffc675
+SOURCE_URL = http://www.codesourcery.com/sgpp/lite/arm/portal/package8733/public/arm-none-eabi/$(LOCAL_SOURCE)
+MD5_CHECKSUM = 7c302162ec813d039b8388bd7d2b4176
+
+#SOURCE_MD5_CHCKSUM = 0ab992015a71443efbf3654f33ffc675
 BIN_MD5_CHECKSUM = 273e0c8da3111244957935aa4d6da197
 
 install-cross: cross-binutils cross-gcc cross-g++ cross-newlib cross-gdb
@@ -157,7 +162,7 @@ cross-g++: cross-binutils cross-gcc cross-newlib gcc-$(GCC_VERSION)-$(CS_BASE) m
    $(MAKE) installdirs install-target && \
    $(MAKE) -C gcc install-common install-cpp install- install-driver install-headers
 
-NEWLIB_FLAGS="-ffunction-sections -fdata-sections -DPREFER_SIZE_OVER_SPEED -D__OPTIMIZE_SIZE__ -Os -fomit-frame-pointer -fno-unroll-loops -D__BUFSIZ__=256 -mabi=aapcs"
+NEWLIB_FLAGS="-ffunction-sections -fdata-sections -DPREFER_SIZE_OVER_SPEED -D__OPTIMIZE_SIZE__ -Os -fomit-frame-pointer -fno-unroll-loops -D__BUFSIZ__=256"
 cross-newlib: cross-binutils cross-gcc newlib-$(CS_BASE)
    mkdir -p build/newlib && cd build/newlib && \
    pushd ../../newlib-$(CS_BASE) ; \


I think the most important change is the removal of -mabi=aapcs. It might solve your problems too.

Re: Trigonometry Causing PWM to stop

Posted: Tue Aug 23, 2011 8:30 am
by Microman171
I rebuilt the toolchain with that patch. Everything went fine, but it didn't fix the problem.

When I get the chance to have a good look at this, I will try some floating point stuff in the main thread...


Cheers,

MM

Re: Trigonometry Causing PWM to stop

Posted: Tue Aug 23, 2011 8:56 am
by mabl
Too bad. I also just tested some trigonometry (tan) in the main thread - all worked fine.