Skip to content

Instantly share code, notes, and snippets.

@CapnKernel
Created November 25, 2019 00:59
Show Gist options
  • Save CapnKernel/8b7d5812fd15a13c8bcab7b7abff8fb0 to your computer and use it in GitHub Desktop.
Save CapnKernel/8b7d5812fd15a13c8bcab7b7abff8fb0 to your computer and use it in GitHub Desktop.
Changes @beholder77 made for IBT_2
--- motor.ino 2019-11-25 11:57:31.874180846 +1100
+++ motor-ibt2.ino 2019-11-25 11:53:44.063533486 +1100
@@ -102,9 +102,9 @@
gnd gnd
*/
-//#define VNH2SP30 // defined if this board is used
-//#define DISABLE_TEMP_SENSE // if no temp sensors avoid errors
-//#define DISABLE_VOLTAGE_SENSE // if no voltage sense
+#define VNH2SP30 // defined if this board is used
+#define DISABLE_TEMP_SENSE // if no temp sensors avoid errors
+#define DISABLE_VOLTAGE_SENSE // if no voltage sense
//#define DISABLE_RUDDER_SENSE // if no rudder sense
@@ -163,23 +163,34 @@
// and will be noticed by the control program
#define pwm_output_pin 9
+#define pwm_output_pin2 10
#define hbridge_a_bottom_pin 2
#define hbridge_b_bottom_pin 3
#define hbridge_a_top_pin 9
#define hbridge_b_top_pin 10
-#define enable_pin 10 // for vnh2sp30
+#define enable_pin 12 // for vnh2sp30
// for direct mosfet mode, define how to turn on/off mosfets
// do not use digitalWrite!
-#define a_top_on PORTB |= _BV(PB1)
-#define a_top_off PORTB &= ~_BV(PB1)
-#define a_bottom_on PORTD |= _BV(PD2)
-#define a_bottom_off PORTD &= ~_BV(PD2)
-#define b_top_on PORTB |= _BV(PB2)
-#define b_top_off PORTB &= ~_BV(PB2)
-#define b_bottom_on PORTD |= _BV(PD3)
-#define b_bottom_off PORTD &= ~_BV(PD3)
+//#define a_top_on PORTB |= _BV(PB1)
+//#define a_top_off PORTB &= ~_BV(PB1)
+//#define a_bottom_on PORTD |= _BV(PD2)
+//#define a_bottom_off PORTD &= ~_BV(PD2)
+//#define b_top_on PORTB |= _BV(PB2)
+//#define b_top_off PORTB &= ~_BV(PB2)
+//#define b_bottom_on PORTD |= _BV(PD3)
+//#define b_bottom_off PORTD &= ~_BV(PD3)
+
+#define a_top_on PORTB |= _BV(PB1) //9
+#define a_top_off PORTB &= ~_BV(PB1) //9
+#define a_bottom_on PORTD |= _BV(PD2) //2
+#define a_bottom_off PORTD &= ~_BV(PD2) //2
+#define b_top_on PORTB |= _BV(PB2) //10
+#define b_top_off PORTB &= ~_BV(PB2) //10
+#define b_bottom_on PORTD |= _BV(PD2) //3 -2
+#define b_bottom_off PORTD &= ~_BV(PD2) //3 -2
+
#define clutch_pin 11 // use pin 11 to engage clutch
@@ -373,6 +384,8 @@
if(pwm_style) {
digitalWrite(pwm_output_pin, LOW); /* enable internal pullups */
pinMode(pwm_output_pin, OUTPUT);
+ digitalWrite(pwm_output_pin2, LOW); /* enable internal pullups */
+ pinMode(pwm_output_pin2, OUTPUT);
}
// test shunt type, if pin wired to ground, we have 0.01 ohm, otherwise 0.05 ohm
shunt_resistance = digitalRead(shunt_sense_pin);
@@ -417,16 +430,22 @@
OCR1A = 1500/DIV_CLOCK + value * 3 / 2 / DIV_CLOCK;
//OCR1A = 1350/DIV_CLOCK + value * 27 / 20 / DIV_CLOCK;
else if(pwm_style == 2) {
- OCR1A = abs((int)value - 1000) * DIV_CLOCK;
+ //OCR1A = abs((int)value - 1000) * DIV_CLOCK;
if(value > 1040) {
- a_bottom_off;
+ OCR1A = 0;
+ OCR1B = abs((int)value - 1000) * DIV_CLOCK;
+ a_bottom_on;
b_bottom_on;
} else if(value < 960) {
- b_bottom_off;
+ OCR1B = 0;
+ OCR1A = abs((int)value - 1000) * DIV_CLOCK;
+ b_bottom_on;
a_bottom_on;
} else { // low, set pwm for brake
- a_bottom_off;
- b_bottom_off;
+ a_bottom_off;
+ b_bottom_off;
+ OCR1A = abs((int)value - 1000) * DIV_CLOCK;
+ OCR1B = abs((int)value - 1000) * DIV_CLOCK;
}
} else {
uint16_t OCR1A_u = 16000, OCR1B_u = 16000, ICR1_u = 1000;
@@ -559,7 +578,7 @@
if(pwm_style) {
TCCR1A=0;
TCCR1B=0;
- while(digitalRead(pwm_output_pin)); // wait for end of pwm if pulse is high
+ while(digitalRead(pwm_output_pin)||digitalRead(pwm_output_pin2)); // wait for end of pwm if pulse is high
TIMSK1 = 0;
if(pwm_style == 2) {
a_bottom_off;
@@ -595,7 +614,8 @@
TIMSK1 = 0;
} else if(pwm_style == 2) {
TCNT1 = 0;
- TCCR1A=_BV(COM1A1)|_BV(WGM11); //NON Inverted PWM
+ TCCR1A=_BV(COM1A1)|_BV(COM1B1)|_BV(WGM11); //NON Inverted PWM
+ //TCCR1A=_BV(WGM11); //NON Inverted PWM
TCCR1B=_BV(WGM13)|_BV(WGM12)|_BV(CS10); //PRESCALER=0 MODE 14(FAST PWM)
// use 1khz safe at all speeds. 20khz is nice to avoid
@@ -603,6 +623,12 @@
// or it will overheat the part at very low speeds.
ICR1=16000/DIV_CLOCK; //fPWM=1khz
TIMSK1 = 0;
+
+ a_top_off;
+ a_bottom_off;
+ b_top_off;
+ b_bottom_off;
+
digitalWrite(hbridge_a_bottom_pin, LOW);
digitalWrite(hbridge_b_bottom_pin, LOW);
@@ -610,6 +636,8 @@
pinMode(hbridge_a_bottom_pin, OUTPUT);
pinMode(hbridge_b_bottom_pin, OUTPUT);
+ pinMode(hbridge_a_top_pin, OUTPUT);
+ pinMode(hbridge_b_top_pin, OUTPUT);
pinMode(enable_pin, INPUT);
digitalWrite(enable_pin, HIGH);
@@ -767,8 +795,7 @@
uint32_t v = TakeADC(CURRENT, p);
if(pwm_style == 2) // VNH2SP30
- return v * 9 / 34 / 16;
-
+ return v * 9 / 34 / 16;
if(low_current) {
// current units of 10mA
// 275 / 128 = 100.0/1024/.05*1.1 for 0.05 ohm shunt
@@ -1253,4 +1280,4 @@
out_sync_b = 0;
break;
}
-}
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment