Jallson Suryo
Published © GPL3+

Smart Bike Suspension

An automatic suspension adjustment on a bicycle that able to understand the character of the terrain and the activities of the rider.

AdvancedFull instructions provided20 hours6,292
Smart Bike Suspension

Things used in this project

Hardware components

Arduino Nano 33 BLE Sense
Arduino Nano 33 BLE Sense
×1
SparkFun LiPo Charger/Booster - 5V/1A
SparkFun LiPo Charger/Booster - 5V/1A
×1
SparkFun Lithium ion battery - 1A, 3.7V
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Smartphone (Android or iPhone)
×1

Software apps and online services

Edge Impulse Studio
Edge Impulse Studio
Arduino Science Journal app
iOS or Android app
Arduino IDE
Arduino IDE

Hand tools and fabrication machines

bolt, nuts, rubber foam, cable ties, o ring rubber, silicon glue

Story

Read more

Custom parts and enclosures

servo mount bracket

micro servo mount bracket to bike suspension

Arduino, battery & charger/boost case

Arduino Nano 33 BLE Sense, Lithium battery, Sparkfun charger/boost case

add-on servo bracket

Big gear for suspension cap

Gear cap

Small gear attached to servo

Code

Suspension_moving_servo

Arduino
Arduino code to upload to Nano 33 BLE Sense
/* Edge Impulse Arduino examples
 * Copyright (c) 2021 EdgeImpulse Inc.
 * 
 * MODIFIED by Jallson Suryo for specific Smart Bike Suspension Project
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/* Includes ---------------------------------------------------------------- */
#include <Smart_Bike_Suspension_inferencing.h>
#include <Arduino_LSM9DS1.h> //Click here to get the library: http://librarymanager/All#Arduino_LSM9DS1
#include <Servo.h>

Servo myservo;

/* Constant defines -------------------------------------------------------- */
#define CONVERT_G_TO_MS2    9.80665f
#define MAX_ACCEPTED_RANGE  2.0f        // starting 03/2022, models are generated setting range to +-2, but this example use Arudino library which set range to +-4g. If you are using an older model, ignore this value and use 4.0f instead

const int UNLOCK = 0;
const int LOCK = 180;
const int MIDDLE = 90;

int selected;
int pos = 0;

/*
 ** NOTE: If you run into TFLite arena allocation issue.
 **
 ** This may be due to may dynamic memory fragmentation.
 ** Try defining "-DEI_CLASSIFIER_ALLOCATION_STATIC" in boards.local.txt (create
 ** if it doesn't exist) and copy this file to
 ** `<ARDUINO_CORE_INSTALL_PATH>/arduino/hardware/<mbed_core>/<core_version>/`.
 **
 ** See
 ** (https://support.arduino.cc/hc/en-us/articles/360012076960-Where-are-the-installed-cores-located-)
 ** to find where Arduino installs cores on your machine.
 **
 ** If the problem persists then there's not enough memory for this model and application.
 */

/* Private variables ------------------------------------------------------- */
static bool debug_nn = false; // Set this to true to see e.g. features generated from the raw signal

/**
* @brief      Arduino setup function
*/
void setup()
{
    myservo.attach(9);
    // put your setup code here, to run once:
    Serial.begin(115200);
    // comment out the below line to cancel the wait for USB connection (needed for native USB)
    //while (!Serial);
    Serial.println("Edge Impulse Inferencing Demo");

    if (!IMU.begin()) {
        ei_printf("Failed to initialize IMU!\r\n");
    }
    else {
        ei_printf("IMU initialized\r\n");
    }

    if (EI_CLASSIFIER_RAW_SAMPLES_PER_FRAME != 3) {
        ei_printf("ERR: EI_CLASSIFIER_RAW_SAMPLES_PER_FRAME should be equal to 3 (the 3 sensor axes)\n");
        return;
    }
}

/**
 * @brief Return the sign of the number
 * 
 * @param number 
 * @return int 1 if positive (or 0) -1 if negative
 */
float ei_get_sign(float number) {
    return (number >= 0.0) ? 1.0 : -1.0;
}

/**
* @brief      Get data and run inferencing
*
* @param[in]  debug  Get debug info if true
*/
void loop()
{
    //ei_printf("\nStarting inferencing in 0.5 seconds...\n");

    //delay(100);

    ei_printf("Sampling...\n");

    // Allocate a buffer here for the values we'll read from the IMU
    float buffer[EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE] = { 0 };

    for (size_t ix = 0; ix < EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE; ix += 3) {
        // Determine the next tick (and then sleep later)
        uint64_t next_tick = micros() + (EI_CLASSIFIER_INTERVAL_MS * 1000);

        IMU.readAcceleration(buffer[ix], buffer[ix + 1], buffer[ix + 2]);

        for (int i = 0; i < 3; i++) {
            if (fabs(buffer[ix + i]) > MAX_ACCEPTED_RANGE) {
                buffer[ix + i] = ei_get_sign(buffer[ix + i]) * MAX_ACCEPTED_RANGE;
            }
        }

        buffer[ix + 0] *= CONVERT_G_TO_MS2;
        buffer[ix + 1] *= CONVERT_G_TO_MS2;
        buffer[ix + 2] *= CONVERT_G_TO_MS2;

        delayMicroseconds(next_tick - micros());
    }

    // Turn the raw buffer in a signal which we can the classify
    signal_t signal;
    int err = numpy::signal_from_buffer(buffer, EI_CLASSIFIER_DSP_INPUT_FRAME_SIZE, &signal);
    if (err != 0) {
        ei_printf("Failed to create signal from buffer (%d)\n", err);
        return;
    }

    // Run the classifier
    ei_impulse_result_t result = { 0 };

    err = run_classifier(&signal, &result, debug_nn);
    if (err != EI_IMPULSE_OK) {
        ei_printf("ERR: Failed to run classifier (%d)\n", err);
        return;
    }

    // print the predictions
    ei_printf("Predictions ");
    ei_printf("(DSP: %d ms., Classification: %d ms., Anomaly: %d ms.)",
        result.timing.dsp, result.timing.classification, result.timing.anomaly);
    ei_printf(": \n");
    for (size_t ix = 0; ix < EI_CLASSIFIER_LABEL_COUNT; ix++) {
        ei_printf("    %s: %.5f\n", result.classification[ix].label, result.classification[ix].value);
        if (result.classification[ix].value > 0.5){
          ei_printf("Selected: %s\n", result.classification[ix].label);
          selected = ix;
        }
    }
    if (selected == 0 || selected == 3){ //GO TO LOCK
      if (pos <= UNLOCK){
        for (pos = UNLOCK; pos <= LOCK; pos += 1) { // goes from UNLOCK0 to LOCK180
          myservo.write(pos);
          delay(5);
        }
      }
      else if (pos == MIDDLE || pos == MIDDLE+1 || pos == MIDDLE-1){
        for (pos = MIDDLE; pos <= LOCK; pos += 1) { // goes from MIDDLE90 to LOCK180
          myservo.write(pos);
          delay(5);
        }
      }
      else{
        pos = LOCK;
      }
    }
    
    else if (selected == 1 || selected == 4){  //GO TO MIDDLE
      if (pos >= LOCK){
        for (pos = LOCK; pos >= MIDDLE; pos -= 1) { //goes from LOCK180 to MIDDLE90
          myservo.write(pos);
          delay(5);
        }
      } 
      else if (pos <= UNLOCK){
        for (pos = UNLOCK; pos <= MIDDLE; pos += 1) { // goes from UNLOCK0 to MIDDLE90
          myservo.write(pos);
          delay(5);
        }
      }
      else{
        pos = MIDDLE;
      }
    }
    
    else{ //GO TO UNLOCK
      if (pos >= LOCK){
        for (pos = LOCK; pos >= UNLOCK; pos -= 1) { //goes from LOCK180 to UNLOCK0
          myservo.write(pos);
          delay(5);
        }
      }
      else if (pos == MIDDLE || pos == MIDDLE+1 || pos == MIDDLE-1) {
        for (pos = MIDDLE; pos <= UNLOCK; pos -= 1) { // goes from MIDDLE90 to UNLOCK0
          myservo.write(pos);
          delay(5);
        }
      }
      else{
        pos = UNLOCK;
      }
    }

    
#if EI_CLASSIFIER_HAS_ANOMALY == 1
    ei_printf("    anomaly score: %.3f\n", result.anomaly);
#endif


}

#if !defined(EI_CLASSIFIER_SENSOR) || EI_CLASSIFIER_SENSOR != EI_CLASSIFIER_SENSOR_ACCELEROMETER
#error "Invalid model for current sensor"
#endif

Credits

Jallson Suryo

Jallson Suryo

5 projects • 22 followers
Tech integrator for schools. Also works as a maker and his activities include disassembling, fixing, and making things.

Comments