Accelerometer as Puncho-o-meter | The “stm32-Discovery Board Sensor”- Part 3

Reading Time: 4 minutes

Hello everyone, In this blog, we are going to provide you the way to use the Accelerometer Sensor of the Discovery Board as a Punch-o-meter. This is going to be the last part of the series in which we will work with Accelerometer as Puncho-o-meter.

In the previous part, we have discussed the Accelerometer Sensor of the Discovery Board and we also got the readings from the F3 Board successfully. If you haven’t read those parts then I suggest you please go through them first before readings this. Here are the two parts of the series PART 1, PART 2. In this blog we will move further to build Accelerometer as Puncho-o-meter

As we are working with the Embedded Systems they make our task fast and reliable. Also, they are much smaller in size compared to traditional computers, which makes them compact and portable, and useful for mass production. Management of Embedded Systems is pretty easy, as elements used in their creation are cheap & long-lasting. Embedded Systems are also cost-effective.

Puncho-o-meter

What is Puncho-o-meter?

Punch-O-Meter

Puncho-o-Meter is a tool that allows you to measure the speed (actually, acceleration) of your punch. The faster the punch – the better score you get!

This meter basically works on the Accelerometer Sensor which gets the readings from the sensor and on the basis of those readings it calculates how hard you punch. It is also played as a game in many gaming centers and malls.


Build a Puncho-o-meter

Today we are going to do the same, we are going to write a program and we are going to measure the acceleration. The difference is we are going to use the Discovery Board instead of your punch. We are going to get the readings from the x-axis of the moving board. Then we are going to calculate the highest value of the readings and will call it as Max Acceleration value. So here our Board will work as a Punch.


stm32f3 Sensor- “Accelerometer as Puncho-o-meter”

2 x 3Axis Accelerometer Sensor -Tracks Position by Motion Arduino TTL  Raspberry | eBay

This f3 board contains three sensors as we have already discussed in our previous blogs and one of them is Accelerometer. Now this sensor which is packed in a package called LSM303DLHC is going to provide us access to this Board’s Sensor and using that we are going to get the readings successfully.

To keep things simple, we’ll measure the acceleration only on the X-axis while the board remains horizontal.


Punch-o-meter

Here’s what the punch-o-meter must do:

  • By default, the app is not “observing” the acceleration of the board.
  • When a significant X acceleration is detected (i.e. the acceleration goes above some threshold), the app should start a new measurement.
  • During that measurement interval, the app should keep track of the maximum acceleration observed.
  • After the measurement interval ends, the app must report the maximum acceleration observed. You can report the value using the iprintln macro.

Let’s build this

At first, we need to set the Threshold value and whenever our readings go above this, it will note it down and compare with the previous, and then at last, it will print the highest value on the itm console.

 const SENSITIVITY: f32 = 12. / (1 << 14) as f32;
    const THRESHOLD: f32 = 0.5;

After this, we will store the lsm package, itm, delay, and monotimer in variables and will get the readings of the Sensor from the package.

   let (mut lsm303dlhc, mut delay, mut itm, mono_timer) = initialization();
    match lsm303dlhc.set_accel_sensitivity(Sensitivity::G12) {
        Ok(senstivity) => senstivity,
        Err(err) => panic!("Can't set accelerometer senstivity {:?}", err),
    }

At last, we are going to provide the loop in which this whole process will work.

The process:

  • Get the readings of the x-axis of the Sensor.
  • Compare that with the Threshold value. If it is above the provided value the next process is started.
  • It will measure the values for few mini seconds and then it will provide the Maximum value as output to itm console.
 // If acceleration goes above a threshold, we start measuring
                if accel_x > THRESHOLD {
                    iprintln!(&mut itm.stim[0], "START!");

                    max_accel = accel_x;
                    instant = Some(mono_timer.now());
                }
            }

// Still measuring
            Some(ref instant) if instant.elapsed() < measurement_time => {
                if accel_x > max_accel {
                    max_g = accel_x;
                }
            }
            _ => {
                // Report max value
                iprintln!(&mut itm.stim[0], "Max acceleration: {}g", max_accel);

This will take place with some delay. At last you will get the Maximum Acceleration value.

The whole code looks like this.

 let measurement_time = mono_timer.frequency().0; // 1 second in ticks
    let mut instant = None;
    let mut max_accel = 0.;
    loop {
        let accel_x = f32::from(lsm303dlhc.accel().unwrap().x).abs() * SENSITIVITY;

        match instant {
            None => {
                // If acceleration goes above a threshold, we start measuring
                if accel_x > THRESHOLD {
                    iprintln!(&mut itm.stim[0], "START!");

                    max_accel = accel_x;
                    instant = Some(mono_timer.now());
                }
            }
            // Still measuring
            Some(ref instant) if instant.elapsed() < measurement_time => {
                if accel_x > max_accel {
                    max_accel = accel_x;
                }
            }
            _ => {
                // Report max value
                iprintln!(&mut itm.stim[0], "Max acceleration: {}g", max_accel);

                // Measurement done
                instant = None;

                // Reset
                max_accel = 0.;
            }
        }

        delay.delay_ms(50_u8);
    }

You can also visit this repository for the same process and you need to uncomment the CODE for punch-o-meter to access the working of this code. This was all from my side hope you get it. I will see you in the next blog, thanks for reading.


If you want to read more content like this?  Subscribe to Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe to Rust Times Newsletter: https://bit.ly/2Vdlld7.

rust-times

Written by 

Nitin is a Software Consultant, with experience of more than 7 months. He works on Rust Programming Language and Embedded Development using Rust. He is also fond of Java Programming & Artificial Intelligence. Apart from that, his hobbies are Watching Netflix, Reading, Singing & Writing.

Leave a Reply