Re: [kivy-users] Abridged summary of kivy-users@googlegroups.com - 2 updates in 1 topic

13 views
Skip to first unread message

Paul Wandendeya

unread,
Jun 22, 2024, 3:16:45 PM (10 days ago) Jun 22
to kivy-...@googlegroups.com
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.core.sensor import Accelerometer

class ProjectileStopApp(App):
    def build(self):
        # Given values
        self.countdown_time = 5  # seconds
        self.impact_threshold = 10  # Acceleration threshold for projectile impact (in m/s^2)
        self.projectile_detected = False

        # Create the app layout
        layout = BoxLayout(orientation='vertical')
        self.label = Label(text=f"Countdown: {self.countdown_time}")
        layout.add_widget(self.label)

        # Initialize the accelerometer sensor
        self.accelerometer = Accelerometer()
        self.accelerometer.bind(acceleration=self.check_projectile)

        # Schedule a function to check for projectile impact
        self.countdown = self.countdown_time
        Clock.schedule_interval(self.update_countdown, 1)  # Check every second

        return layout

    def update_countdown(self, dt):
        # Decrease the countdown time
        self.countdown -= 1

        # Update the label text based on the countdown
        self.label.text = f"Countdown: {self.countdown}"

        # Check if projectile impact is detected
        if self.projectile_detected:
            self.label.text = "Projectile impact detected!"
            Clock.unschedule(self.update_countdown)  # Stop the countdown

    def check_projectile(self, sensor, acceleration):
        # Check if the acceleration exceeds the impact threshold
        if abs(acceleration.x) > self.impact_threshold or \
           abs(acceleration.y) > self.impact_threshold or \
           abs(acceleration.z) > self.impact_threshold:
            self.projectile_detected = True

if __name__ == '__main__':
    ProjectileStopApp().run() can you assist me to correct the code to simulate accelerometer data?

On Sat, Jun 15, 2024, 3:00 AM <kivy-...@googlegroups.com> wrote:
Paul Wandendeya <paulwan...@gmail.com>: Jun 14 03:53PM +0300

The project will make bullets of the enemy fall off far away before humans
such like bullet of Oswald who was involved in the actions of destroying
life of the fourth President of USA John ...more
ELLIOT GARBUS <elli...@cox.net>: Jun 14 05:46PM

There are some elements in the program that do not make sense.
 
# Register the accelerometer sensor
self.accelerometer = Window.request_accelerometer()
 
The Window class does not have a method ...more
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to kivy-users+...@googlegroups.com.

ElliotG

unread,
Jun 22, 2024, 8:32:00 PM (10 days ago) Jun 22
to Kivy users support
"can you assist me to correct the code to simulate accelerometer data?"
Here is an example that simulates an accelerometer.

from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import AliasProperty, NumericProperty
from kivy.event import EventDispatcher

kv = """
BoxLayout:
    orientation: 'vertical'
    Label:
        id: label
        text: f'Countdown: {app.countdown_time}'
"""


class Accelerometer(EventDispatcher):
    x = NumericProperty()
    y = NumericProperty()
    z = NumericProperty()

    def get_acceleration(self):
        return (self.x ** 2 + self.y ** 2 + self.z ** 2) ** 0.5

    acceleration = AliasProperty(get_acceleration, None, bind=['x', 'y', 'z'])

    def simulate(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z


class ProjectileStopApp(App):
    countdown_time = NumericProperty(5)  # seconds

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.impact_threshold = 10  # Acceleration threshold for projectile impact (in m/s^2)
        self.projectile_detected = False
        self.schedule = None  # holds the countdown schedule

        # Initialize the accelerometer sensor
        self.accelerometer = Accelerometer()
        self.accelerometer.bind(acceleration=self.check_projectile)

    def build(self):

        # Schedule a function to check for projectile impact
        self.schedule = Clock.schedule_interval(self.update_countdown, 1)  # Check every second
        return Builder.load_string(kv)


    def update_countdown(self, dt):
        # Decrease the countdown time
        self.countdown_time -= 1

        if self.countdown_time == 3:
            # simulate a change in acceleration
            self.accelerometer.simulate(100, 0, 0)


        # Check if projectile impact is detected
        if self.projectile_detected:
            self.root.ids.label.text = "Projectile impact detected!"
            self.schedule.cancel()  # Stop the countdown

        if self.countdown_time < 0:
            self.root.ids.label.text = "Countdown completed, No impact detected"
            self.schedule.cancel()  # Stop the countdown

    def check_projectile(self, acceleration, value):
        print(f'{acceleration=} {value=}')

        # Check if the acceleration exceeds the impact threshold
        if (abs(acceleration.x) > self.impact_threshold or
                abs(acceleration.y) > self.impact_threshold or
                abs(acceleration.z) > self.impact_threshold):

            self.projectile_detected = True


if __name__ == '__main__':
    ProjectileStopApp().run()
Reply all
Reply to author
Forward
0 new messages