7 Replies Latest reply on Mar 10, 2018 12:30 AM by cblinney_2276156

    ISR Problem

    cblinney_2276156

      The initial part of the code is working very well, but after line 139/140 the ISR for sending data across UART/SBUS buggers off.  Up to that point it sends data correctly with a 6ms delay between frames, but after that point goes nuts and keeps sending data constantly without delay and it doesn't seem like anything else in the code is running then.  Its hard to check because if I use the debug and set break points it will get to and run past that point fine, but I can't use it after that point because the flight controller that the UART/SBUS is talking to will have had a fail safe due to too long of a delay between frames.  I can see what is happening though when I hook up my oscilloscope, so I know its sending the 3ms frames with 6ms delay to begin with, and then it goes nuts afterward sending a constant stream of data.  Not really sure what to do at this point.  I have tried everything I can think of as well as testing the Timer component in a separate project to test its functionality, and I can't seem to figure out what is going wrong, any help would be appreciated.  I need to get it running correctly so that I can start test flights and tuning the PID gains.  Attached is the project file.

       

      Thank you

        • 1. Re: ISR Problem
          anks

          You can have look at this project in which Ultrasonic sensor is sending data without using ISR, it may be useful-

          Measuring Distance with US-100 ultrasonic Sensor - Hackster.io

          1 of 1 people found this helpful
          • 2. Re: ISR Problem
            cblinney_2276156

            I mean that sort of helped, I can remove the interrupt from the echo pin but I still have to ping it with the PWM or it doesn't work.  And even doing that it doesn't read it properly and I can't dial the PIDs in well enough.

            • 3. Re: ISR Problem
              jobi

              Timer_Send_WriteCounter API have below side-effect

              (from datasheet)  - This can cause undesired behavior on the terminal count output or period width. This is not an atomic write and the function may be interrupted. The Timer should be disabled before calling this function.

               

              So can you try :

              Timer_Send_Stop(); before the Timer_Send_WriteCounter

               

              Thanks

              Jobin GT

              1 of 1 people found this helpful
              • 4. Re: ISR Problem
                cblinney_2276156

                So both of these comments have helped a bit, its got it so it will run without getting caught up somehow, and it runs without using the PWM component pinging the Trig pin of the HC SR04 sensor.  I also took a bunch of the code out of the interrupt and put it into its own function as I know having a lot of computational code in an interrupt isn't good practice.  Currently it is working better but still seems to be reading the sensor at a slower rate then the ~15Hz it should be.  I have tried adjusting the PIDs and they help, but its still not reacting very quickly like the sensor is only polling around 1Hz (and is audible in motor speed changing).

                 

                As well I don't know why it would be related but it doesn't work as well while running on a 4s (four 3.7v cells in series) then on 3s (three 3.7v cells in series), which doesn't make much sense since its powered off a 5v buck converter.  And I don't just mean that the motors run at different rpms because of it as they should, but it will lock into full throttle even if the sensor reads a value lower then the target height, the throttle never decreases.

                 

                Attached is the newest copy of the project I am working with as described above.

                • 5. Re: ISR Problem
                  jobi

                  Hello,

                  distance_cm() should be called from a Timer ISR to make it period. Not a good idea to put in the main for loop. And the 1 Hz execution is based on all the execution, including the ISRs. You could change the master clock to get more speed.

                  Please verify the distance measurement is working fine in a separate project first.

                   

                  Thanks

                  Jobin GT

                  1 of 1 people found this helpful
                  • 6. Re: ISR Problem
                    cblinney_2276156

                    It should be fine in the main loop shouldn't it?  The Timer ISR still goes off if the system is in the middle of the distance_cm function.  The Timer ISR has to go off every 6ms and send data otherwise the drones flight controller will fail safe and drop it out of the sky, and I can't ping the HC-SR04 sensor that fast, its recommended not to ping the more then every 65ms.  It seems to be running fine as is, maybe you could elaborate more on this problem?

                     

                    I have found another problem as well that has almost got the project working correctly (overshoots still likely due to tuning) but because of the way the derivative and integral variables work in the PID loop, if they aren't constrained to +/- a certain value, they can accumulate and grow rather large if the drone doesn't respond quickly enough, and it takes the system awhile to bring them back to correct itself, but by that time it either landed or took off into the sky.  I noticed the integer was getting as large as 0x0738.  I limited both inte and deri variables to a max of +/-8 and the drone was flying correctly, oscillating between 30cm and 120cm, and I think reducing their extreme values to +/-5 might help correct that, as well as increasing the D gain a bit.  Will report back tomorrow after I charge all the batteries and play with some of the terms.

                     

                    As well I put a bunch of cardboard down, the carpet in this room has been messing with values and taping a sensor at a specific height and testing, I noticed deviations of several cm in either directions out of no where.  Although I tested in another room with different carpet and some with hard flooring and have not noticed it there.

                    • 7. Re: ISR Problem
                      cblinney_2276156

                      Found the biggest problem, PID D term should be subtracted not added.  Made a world of difference.  Thank bot of you though for the other helpful hints, that got it working to the point I could tune the PID.