Author Topic: DC motor speed controller with regen braking, using ATTINY85/45.  (Read 30287 times)

0 Members and 1 Guest are viewing this topic.

Offline RefrigeratorTopic starter

  • Super Contributor
  • ***
  • Posts: 1578
  • Country: lt
Re: DC motor speed controller with regen braking, using ATTINY85/45.
« Reply #50 on: November 15, 2021, 10:03:37 pm »
For whatever reason enabling timer0 overflow interrupt in phase correct PWM mode is no bueno.
I've looked through the datasheet multiple times now and can't find any hints as to why.
Starting to get tired of these issues to be honest.

Here's the code i'm testing stuff out with:
Code: [Select]
#include <avr/interrupt.h>


int Sampling_tick, Sampling_period;

ISR(TIM0_TOV0_vect){
//ISR(TIMER0_COMPA_vect){
  OCR0B = 255;
  Sampling_tick ++;
}

void setup() {
//TIMSK|=(1<<TOIE0); //enabling timer0 interrupt

    PORTB = 0b00000011;
    DDRB = 0b00000011;  // set PB0 and PB1 as output.

     OCR0A = 0;
     OCR0B = 10;

 TCCR0B = 0b00000001;  // set clock source for timer

 TCCR0A = 0b10100001; // enable and set waveform gen to slow pwm

// enable global interrupt
//sei();
/* enable timer 0 interrupt on counter overflow */
TIMSK|=(1<<TOIE0); //enabling timer0 interrupt

/* global interrupt enable */
//sei();
SREG |= 1 << 7;

}

void loop() {

//delay(10);
     //OCR0A ++;
     //OCR0B ++;

if (Sampling_tick >= Sampling_period){
      // nothing happens here yet
}
}

//ISR(TIM0_TOV0_vect){
////ISR(TIMER0_COMPA_vect){
//  OCR0B = 255;
//  Sampling_tick ++;
//}

OCR0B = 10; works but no other OCR0B = whatever;  work after timer0 interrupt enable bit is set in TIMSK.  |O |O |O

Ps: i'm using a spare Digispark board to test this code out because it's easier. Also i know interrupts (in general) on timer0 work because i have used them previously but i have no idea why they aren't working now.
« Last Edit: November 15, 2021, 10:12:57 pm by Refrigerator »
I have a blog at http://brimmingideas.blogspot.com/ . Now less empty than ever before !
An expert of making MOSFETs explode.
 

Offline RefrigeratorTopic starter

  • Super Contributor
  • ***
  • Posts: 1578
  • Country: lt
Re: DC motor speed controller with regen braking, using ATTINY85/45.
« Reply #51 on: November 16, 2021, 10:18:41 pm »
Okay i've changed the current control yet again.
Now the ADC reading from the throttle potentiometer is just a target and if current is below the treshold i increment the control value by 1 until it either reaches max current or reaches the throttle ADC value.
If current is above or the control value is above the throttle ADC reading then i decrement the control value by 1 each loop.
I should probably make the decrement value a seperate thing to perhaps change the response of the loop.
I increment the value in each loop cycle so it more or less ramps steadily.
I haven't yet been successful at getting the interrupt to run and i've given up on interrupts for now.
Also i added some code that reads the base current whenever the throttle potentiometer is set in the deadband to compensate for drift in diff amp reference voltage.

Tested the new control algorithm out on the scooter and it's much quieter than previous one.
Weird thing i noticed is that the current limit works but i think it's being affected by the noise somehow?
With the current limit set at 10A when i throttle up with the motor stalled it sits at 3A but if i let the motor run it jumps to 9A~10A.
If i stall the motor it settles down at 3A again.
This is the current coming from the battery.
Measuring the current in the motor loop at stall it's around 10A and drops when i let the motor run.
« Last Edit: November 17, 2021, 08:57:42 am by Refrigerator »
I have a blog at http://brimmingideas.blogspot.com/ . Now less empty than ever before !
An expert of making MOSFETs explode.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf