Arduinoprosjekter

DIY ESC uten hall sensor

https://simple-circuit.com/arduino-sensorless-bldc-motor-controller-esc/ 

  1. esc diy med sensor: https://simple-circuit.com/arduino-cd-rom-brushless-motor-control/
  2. esc diy uten sensor: https://simple-circuit.com/arduino-cd-rom-brushless-motor-control/

 

hall effekt sensors: jord – A – B – C – jord – 5v

// Sensorless brushless DC (BLDC) motor control with Arduino UNO (Arduino DIY ESC).

// This is a free software without any warranty.

#define SPEED_UP          A0
#define SPEED_DOWN        A1
#define PWM_MAX_DUTY      255
#define PWM_MIN_DUTY      50

#define PWM_START_DUTY    100

 

byte bldc_step = 0, motor_speed;

unsigned int i;

void setup() {

  DDRD  |= 0x38;           // Configure pins 3, 4 and 5 as outputs

  PORTD  = 0x00;

  DDRB  |= 0x0E;           // Configure pins 9, 10 and 11 as outputs

  PORTB  = 0x31;

  // Timer1 module setting: set clock source to clkI/O / 1 (no prescaling)

  TCCR1A = 0;

  TCCR1B = 0x01;

  // Timer2 module setting: set clock source to clkI/O / 1 (no prescaling)

  TCCR2A = 0;

  TCCR2B = 0x01;

  // Analog comparator setting

  ACSR   = 0x10;           // Disable and clear (flag bit) analog comparator interrupt

  pinMode(SPEED_UP,   INPUT_PULLUP);

  pinMode(SPEED_DOWN, INPUT_PULLUP);

}

// Analog comparator ISR

ISR (ANALOG_COMP_vect) {

  // BEMF debounce

  for(i = 0; i < 10; i++) {

    if(bldc_step & 1){

      if(!(ACSR & 0x20)) i -= 1;

    }

    else {

      if((ACSR & 0x20))  i -= 1;

    }

  }

  bldc_move();

  bldc_step++;

  bldc_step %= 6;

}

void bldc_move(){        // BLDC motor commutation function

  switch(bldc_step){

    case 0:

      AH_BL();

      BEMF_C_RISING();

      break;

    case 1:

      AH_CL();

      BEMF_B_FALLING();

      break;

    case 2:

      BH_CL();

      BEMF_A_RISING();

      break;

    case 3:

      BH_AL();

      BEMF_C_FALLING();

      break;

    case 4:

      CH_AL();

      BEMF_B_RISING();

      break;

    case 5:

      CH_BL();

      BEMF_A_FALLING();

      break;

  }

}

 

void loop() {

  SET_PWM_DUTY(PWM_START_DUTY);    // Setup starting PWM with duty cycle = PWM_START_DUTY

  i = 5000;

  // Motor start

  while(i > 100) {

    delayMicroseconds(i);

    bldc_move();

    bldc_step++;

    bldc_step %= 6;

    i = i - 20;

  }

  motor_speed = PWM_START_DUTY;

  ACSR |= 0x08;                    // Enable analog comparator interrupt

  while(1) {

    while(!(digitalRead(SPEED_UP)) && motor_speed < PWM_MAX_DUTY){

      motor_speed++;

      SET_PWM_DUTY(motor_speed);

      delay(100);

    }

    while(!(digitalRead(SPEED_DOWN)) && motor_speed > PWM_MIN_DUTY){

      motor_speed--;

      SET_PWM_DUTY(motor_speed);

      delay(100);

    }

  }

}

 

void BEMF_A_RISING(){

  ADCSRB = (0 << ACME);    // Select AIN1 as comparator negative input

  ACSR |= 0x03;            // Set interrupt on rising edge

}

void BEMF_A_FALLING(){

  ADCSRB = (0 << ACME);    // Select AIN1 as comparator negative input

  ACSR &= ~0x01;           // Set interrupt on falling edge

}

void BEMF_B_RISING(){

  ADCSRA = (0 << ADEN);   // Disable the ADC module

  ADCSRB = (1 << ACME);

  ADMUX = 2;              // Select analog channel 2 as comparator negative input

  ACSR |= 0x03;

}

void BEMF_B_FALLING(){

  ADCSRA = (0 << ADEN);   // Disable the ADC module

  ADCSRB = (1 << ACME);

  ADMUX = 2;              // Select analog channel 2 as comparator negative input

  ACSR &= ~0x01;

}

void BEMF_C_RISING(){

  ADCSRA = (0 << ADEN);   // Disable the ADC module

  ADCSRB = (1 << ACME);

  ADMUX = 3;              // Select analog channel 3 as comparator negative input

  ACSR |= 0x03;

}

void BEMF_C_FALLING(){

  ADCSRA = (0 << ADEN);   // Disable the ADC module

  ADCSRB = (1 << ACME);

  ADMUX = 3;              // Select analog channel 3 as comparator negative input

  ACSR &= ~0x01;

}

 

void AH_BL(){

  PORTB  =  0x04;

  PORTD &= ~0x18;

  PORTD |=  0x20;

  TCCR1A =  0;            // Turn pin 11 (OC2A) PWM ON (pin 9 & pin 10 OFF)

  TCCR2A =  0x81;         //

}

void AH_CL(){

  PORTB  =  0x02;

  PORTD &= ~0x18;

  PORTD |=  0x20;

  TCCR1A =  0;            // Turn pin 11 (OC2A) PWM ON (pin 9 & pin 10 OFF)

  TCCR2A =  0x81;         //

}

void BH_CL(){

  PORTB  =  0x02;

  PORTD &= ~0x28;

  PORTD |=  0x10;

  TCCR2A =  0;            // Turn pin 10 (OC1B) PWM ON (pin 9 & pin 11 OFF)

  TCCR1A =  0x21;         //

}

void BH_AL(){

  PORTB  =  0x08;

  PORTD &= ~0x28;

  PORTD |=  0x10;

  TCCR2A =  0;            // Turn pin 10 (OC1B) PWM ON (pin 9 & pin 11 OFF)

  TCCR1A =  0x21;         //

}

void CH_AL(){

  PORTB  =  0x08;

  PORTD &= ~0x30;

  PORTD |=  0x08;

  TCCR2A =  0;            // Turn pin 9 (OC1A) PWM ON (pin 10 & pin 11 OFF)

  TCCR1A =  0x81;         //

}

void CH_BL(){

  PORTB  =  0x04;

  PORTD &= ~0x30;

  PORTD |=  0x08;

  TCCR2A =  0;            // Turn pin 9 (OC1A) PWM ON (pin 10 & pin 11 OFF)

  TCCR1A =  0x81;         //

}

 

void SET_PWM_DUTY(byte duty){

  if(duty < PWM_MIN_DUTY)

    duty  = PWM_MIN_DUTY;

  if(duty > PWM_MAX_DUTY)

    duty  = PWM_MAX_DUTY;

  OCR1A  = duty;                   // Set pin 9  PWM duty cycle

  OCR1B  = duty;                   // Set pin 10 PWM duty cycle

  OCR2A  = duty;                   // Set pin 11 PWM duty cycle

}

http://www.instructables.com/id/Make-Your-Own-ESC/

/* Lokomotivstyring med Bluetooth RC Controller
 *  kontroller funger for fram og tilbake
 *  Slår av/på hovedlys og bremselys
 *  funksjon for fløyte.
 *  
 *  to av utgangene på motorkontrolerne styrer motoren.
 *  De to andre styrer lys foran og bak
 *  
 */


//#include <Tone.h>
//pulsbredde på pinnene  3, 5, 6, 9, 10, and 11.
//#include <Servo.h>
//#include <L293.h>
#define pinForward 8     
#define pinBack 5       
#define pinSpeedForwardBack 9
#define pinFrontLights 6
#define pinBackLights 3
#define pinFrontSteering 10
#define bluetooth Serial1
#define MotorA 10
#define MotorB 11
#define floyte 4
#define lok
#define debug

/*
 * F: fram
 * B: bak
 * R: høyre
 * L: venstre
 * G: venstre fram
 * H: venstre tilbake
 * J: høyre tilbake
 * I: høyre fram
 * W: hovdelys på
 * w: hovdlys av
 * U: baklys(bremselys) på
 * u: baklys (bremselys) av
 * V: Horn på
 * v: horn av
 * X: varsellys på 
 * x: varsellys av
 * 
 */
int fart=100;
int frontlysstyrke=100;
int baklysstyrke=100;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println(__FILE__);
//  Serial1.begin(115200);
//  Serial1.println("AT");
    pinMode(pinFrontLights, OUTPUT);
  pinMode(pinBackLights, OUTPUT);
//  leftRight.attach(pinFrontSteering);
//  bluetooth.begin(115200);  
     pinMode(MotorA, OUTPUT); // MOTOR1
     pinMode(MotorB, OUTPUT);
     digitalWrite(MotorA, LOW); // stop
     digitalWrite(MotorB, LOW); // 
     tone(floyte,400,500);  // fløyte
 
}

void loop() {
  //  Serial1.println("blue..");
  while (Serial.available())
  {
 char c=Serial.read();
 switch (c) {
//  case '1'..'9':setfart(c);break;
  case 'F': forward();break;
  case 'B': back() ;break;
  case 'L': break;//left();break;
  case 'R':  break;//right();break;
  case 'G':  break;//left_forward();break;
  case 'H':  break;//left_back();break;
  case 'J': break;// right_back();break;
  case 'I': break;// right_forward();break;
  case 'W': hovedlys_pa();break;
  case 'w': hovedlys_av();break;
  case 'U': bremselys_pa();break;
  case 'u': bremselys_av();break;
  case 'V': horn_pa();break;
  case 'v': horn_av();break;
  case 'X': break;//  varsellys_pa();break;
  case 'x':  break;// varsellys_av();break;
  default: stopp();break;
  }
  }
  }


  void forward()  {digitalWrite(MotorA,LOW); analogWrite(MotorB, fart);  }
  void back()     {digitalWrite(MotorA,HIGH); analogWrite(MotorB, 255-fart);  }
  void stopp()     {digitalWrite(MotorA,LOW); digitalWrite(MotorB,LOW);  }
  void hovedlys_pa() {analogWrite(pinFrontLights,frontlysstyrke); }
  void hovedlys_av() {analogWrite(pinFrontLights,0)  ;}
  void bremselys_pa() {analogWrite(pinBackLights,baklysstyrke); }
  void bremselys_av() {analogWrite(pinBackLights,0) ; }
  void horn_pa()   {   tone(floyte,400,500); } // fløyte
  void horn_av() {   tone(floyte,400,500); } // fløyte
 // void varsellys_pa() {analogWrite(pinBackLights,baklysstyrke) }
//  void varsellys_av() {analogWrite(pinBackLights,0)  }
  void setfart(char i) 
  {
    
  }