Go Down

Topic: Brushless Motor and Hall effect sensor (Read 86 times) previous topic - next topic

chrismil

Mar 08, 2019, 07:06 pm Last Edit: Mar 08, 2019, 07:10 pm by chrismil
Hello,

I am working in a project and I have a brushless motor controlled through an ESC and I want to get the speed data while the motor is running. However I did not manage to do that. Below you can see the code for running the motor and the hall effect sensor code
How can I combine these two codes and get the speed readings from the hall effect sensor while the motor is running in the same manner?


Brushless motor code:
#include <Wire.h>
#include <Servo.h>

Servo esc_signal;
int velocity = 8;

void setup() {
  esc_signal.attach(9);
  esc_signal.write(velocity);
  delay(100);
  for (velocity = 8; velocity <= 17; velocity += 1)
  {
    esc_signal.write(velocity);
    delay(1000);
  }
  for (velocity = 17; velocity >= 0; velocity -= 1)
  {
    esc_signal.write(velocity);
    delay(1500);
  }
  esc_signal.write(0);
  delay(1000);
}


Hall effect sensor code:
volatile float rpmcount = 0;
float rpm = 0;
float radial_vel = 0;
float linear_vel = 0;
unsigned long lastmillis = 0;

void setup()
{
 Serial.begin(9600);
 attachInterrupt(0, rpm_bike, FALLING); //interrupt zero (0) is on pin two(2).
}

void loop()
{
 if (millis() - lastmillis == 1000)
 {  /*Uptade every one second, this will be equal to reading frecuency (Hz).*/
 detachInterrupt(0);    //Disable interrupt when calculating.
 rpm = rpmcount * 60;  //Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.
 radial_vel = rpm * M_PI / 30; //convert rpm to radial velocity in rad/s.
 linear_vel = radial_vel * 0.33; //convert radial velocity to linear velocity in m/s.
 Serial.print("RPM = "); //print the word "RPM".
 Serial.print(rpm); // print the rpm value.
 Serial.print("\t\t Linear Speed = "); //print the word "Linear Velocity".
 Serial.print(linear_vel); //print the linear velocity value.
 Serial.println(" m/s"); //print the word "m/s".
 
 rpmcount = 0; // Restart the RPM counter
 lastmillis = millis(); // Update lasmillis
 attachInterrupt(0, rpm_bike, FALLING); //enable interrupt
 }
}

void rpm_bike()
{ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
  rpmcount++;
}

Go Up