How to keep a specific PWM value for some time?

Discussions about quadcopters/multi-rotors
Post Reply
DarkKnight
Member
Posts: 39
Joined: Tue Jun 15, 2021 10:19 pm

How to keep a specific PWM value for some time?

Post by DarkKnight »

Hello,

Here is my question. How should I keep a PWM value for a few seconds? or How should I let motor rotates at a constant PWM value for a few seconds just like what it is shown in Fig.1?

I have tried following two ways:(1)I refer to your code https://github.com/bitcraze/crazyflie-f ... ta.py#L139. I have changed the 'time_step =0.1' to 'time_step=3' (assume I want to keep the specific PWM for 3 seconds), but the result is shown in Fig.2.

Code: Select all

    def _ramp_motors(self):
        thrust_mult = 1
        thrust_step = 500
        #time_step = 0.1
        time_step = 3
        thrust = 0
        pitch = 0
        roll = 0
        yawrate = 0

        # # Unlock startup thrust protection
        # for i in range(0, 100):
        #     self._cf.commander.send_setpoint(0, 0, 0, 0)

        localization = Localization(self._cf)

        self._cf.param.set_value('motor.batCompensation', 0)
        self._cf.param.set_value('motorPowerSet.m1', 0)
        self._cf.param.set_value('motorPowerSet.enable', 2)
        self._cf.param.set_value('system.forceArm', 1)

        while self.is_connected: #thrust >= 0:
            thrust += thrust_step * thrust_mult
            if thrust >= 65536 or thrust < 0:
            # if thrust >= 20000 or thrust < 0:
                thrust_mult *= -1
                thrust += thrust_step * thrust_mult
            print(thrust)
            # self._cf.commander.send_setpoint(roll, pitch, yawrate, thrust)
            localization.send_emergency_stop_watchdog()
            self._cf.param.set_value('motorPowerSet.m1', str(thrust))
            time.sleep(time_step)
(2) For the second way that I have tried, I just update your code like the following. However, the result is shown in Fig.3, which is still not right.

Code: Select all

    def _ramp_motors(self):
        thrust_mult = 1
        thrust_step = 1000
        time_step = 0.1
        thrust = 0
        pitch = 0
        roll = 0
        yawrate = 0

        # # Unlock startup thrust protection
        # for i in range(0, 100):
        #     self._cf.commander.send_setpoint(0, 0, 0, 0)

        localization = Localization(self._cf)

        self._cf.param.set_value('motor.batCompensation', 0)
        self._cf.param.set_value('motorPowerSet.m1', 0)
        self._cf.param.set_value('motorPowerSet.enable', 1)
        self._cf.param.set_value('system.forceArm', 1)

        while self.is_connected: #thrust >= 0:
            thrust += thrust_step * thrust_mult
            # if thrust >= 65536 or thrust < 0:
            if thrust >= 65536*0.7 or thrust < 0:
                thrust_mult *= -1
                thrust += thrust_step * thrust_mult
            print(thrust)
            # self._cf.commander.send_setpoint(roll, pitch, yawrate, thrust)
            localization.send_emergency_stop_watchdog()
            t_start = time.time()
             while(1):
                 self._cf.param.set_value('motorPowerSet.m1', str(thrust))
                 time.sleep(0.1)
                 t_end = time.time()
                 if t_end-t_start > 3:
                     break

Could you help to take a look at it and point which part that I didn't implement it correctly? any comments would be helpful! Thank you
Attachments
1.jpg
2.jpg
2.jpg (33.83 KiB) Viewed 9378 times
3.jpg
3.jpg (33.83 KiB) Viewed 9378 times
tobias
Bitcraze
Posts: 2339
Joined: Mon Jan 28, 2013 7:17 pm
Location: Sweden

Re: How to keep a specific PWM value for some time?

Post by tobias »

Without trying this I think the problem is the send_emergency_stop_watchdog which needs to be sent at least every second. If you try with a time step of 1 does it work then?
DarkKnight
Member
Posts: 39
Joined: Tue Jun 15, 2021 10:19 pm

Re: How to keep a specific PWM value for some time?

Post by DarkKnight »

You are right. once I commit out 'send_emergency_stop_watchdog', then It works as it supposed to be. Thanks!!!!
DarkKnight
Member
Posts: 39
Joined: Tue Jun 15, 2021 10:19 pm

Re: How to keep a specific PWM value for some time?

Post by DarkKnight »

tobias wrote: Tue Jan 11, 2022 9:35 am Without trying this I think the problem is the send_emergency_stop_watchdog which needs to be sent at least every second. If you try with a time step of 1 does it work then?
Hi, I still want to know what is 'localization.send_emergency_stop_watchdog' used for? Even though when I comment it out, right now the I can keep the PWM in a specific value for more than 1 second.

However, here is a new problem: When I use CRTL+ C in the terminal to stop the rotor, the rotor won't stop and will keep rotate. Do you know why this will happen and what should I do to stop the rotor by using code instead turning off the battery.

Thanks
kristoffer
Bitcraze
Posts: 630
Joined: Tue Jun 30, 2015 7:47 am

Re: How to keep a specific PWM value for some time?

Post by kristoffer »

Hi!

The emergency stop watchdog is a functionality that can be used to add an emergency stop to the Crazyflie, for instance in a public display. The idea is to use a separate system with a Crazyradio that sends a emergency watchdog packet (using localization.send_emergency_stop_watchdog()) a couple of times per second. If someone presses an emergency stop button, the system simply stops sending the watchdog packet and all Crazyflies stops the motors when not receiving the packet any more.
As we do not always want this functionality to be active, it is enabled in the Crzyflie when receiving the first watchdog packet, and this is what you see. If you never call localization.send_emergency_stop_watchdog() it is not enabled and the motors are not stopped when you kill your program.
The default timeout in the Crazyflie is one second for the watch dog so if you call localization.send_emergency_stop_watchdog() you need to do it a couple of times every second.
Post Reply