Created
May 25, 2010 03:30
-
-
Save D1plo1d/412718 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int N_OF_COILS = 3; | |
struct BrushlessDCMotor | |
{ | |
int hal_pins[N_OF_COILS]; | |
//positive and negative coil pins | |
int pos_coil_pins[N_OF_COILS]; | |
int neg_coil_pins[N_OF_COILS]; | |
// The current value of each hal sensor and coil output | |
int hal_values[N_OF_COILS]; | |
int coil_values[N_OF_COILS]; | |
}; | |
/*struct Pid | |
{ | |
int p; | |
int i; | |
int d; | |
}*/ | |
struct BrushlessDCMotor bldc; | |
//struct Pid pid; | |
int speed_pwm_value = 0; | |
int speed_pwm_pin = 9; | |
void setup() { | |
pid.p = 0.1; | |
pid.i = 0; | |
pid.d = 0.1; | |
bldc.hal_pins[0] = 0; | |
bldc.hal_pins[1] = 1; | |
bldc.hal_pins[2] = 2; | |
bldc.pos_coil_pins[0] = 3; | |
bldc.neg_coil_pins[0] = 4; | |
bldc.pos_coil_pins[1] = 5; | |
bldc.neg_coil_pins[1] = 6; | |
bldc.pos_coil_pins[2] = 7; | |
bldc.neg_coil_pins[2] = 8; | |
for (int i = 0; i < N_OF_COILS; i++) | |
{ | |
pinMode(bldc.hal_pins[i], INPUT); | |
pinMode(bldc.pos_coil_pins[i], OUTPUT); | |
pinMode(bldc.neg_coil_pins[i], OUTPUT); | |
bldc.hal_values[i] = 0; | |
bldc.coil_values[i] = 0; | |
} | |
Serial.begin(9600) | |
} | |
void loop() { | |
//Polling the hal | |
for (int i = 0; i < N_OF_COILS; i++) | |
{ | |
bldc.hal_values[i] = digitalRead(bldc.hal_pins[i]); | |
} | |
//Updating the motor control outputs | |
updateCoils(); | |
//Polling for serial speed control | |
if (Serial.available() > 0) | |
{ | |
c = Serial.read(); | |
if (c=='+') speed_pwm_value = (speed_pwm_value+1)%255; | |
if (c=='-' && speed_pwm_value>0) speed_pwm_value -= 1; | |
//Sending the speed pwm | |
analogWrite(speed_pwm_pin, speed_pwm_value); | |
} | |
} | |
//Computes the value a coil should be set to to continue spinning the motor. | |
//positive coil[i] = hal[i] and ( not hal[i+1] ) | |
//negative coil[i] = hal[i+1] and ( not hal[i+2] ); | |
#define coilValue(hal1, hal2) ( hal1 & (1^hal2) ) | |
//Sets the value of a coil | |
#define setPosCoil(bldc, coil_index, value) if (bldc.coil_values[coil_index] != value) digitalWrite(bldc.pos_coil_pins[coil_index], value); bldc.coil_values[coil_index] = value | |
#define setNegCoil(bldc, coil_index, value) if (bldc.coil_values[coil_index] == value) digitalWrite(bldc.neg_coil_pins[coil_index], value); bldc.coil_values[coil_index] = !value | |
//Updates the digital outputs controlling each of the BLDC motor controller half hbridges to reflect updates in the hal values | |
void updateCoils() | |
{ | |
for (int i = 0; i< N_OF_COILS; i++) | |
{ | |
setPosCoil( bldc, i, coilValue( bldc.hal_values[i], bldc.hal_values[(i+1)%N_OF_COILS] ) ); | |
setNegCoil( bldc, i, coilValue( bldc.hal_values[i+1]%N_OF_COILS, bldc.hal_values[(i)] ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Arduino:1.8.1 (Windows 7), Kart:"Arduino/Genuino Uno"
C:\Users\User\Documents\Arduino\sketch_mar21a\sketch_mar21a.ino: In function 'void setup()':
sketch_mar21a:28: error: 'pid' was not declared in this scope
pid.p = (0).1;
^
sketch_mar21a:53: error: expected ';' before '}' token
}
^
C:\Users\User\Documents\Arduino\sketch_mar21a\sketch_mar21a.ino: In function 'void loop()':
sketch_mar21a:66: error: 'c' was not declared in this scope
exit status 1
'pid' was not declared in this scope
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.