summaryrefslogtreecommitdiffhomepage
path: root/analog/motor-power-avr/src/mp_pwm_LR_.c
blob: d1aec12ddb9e83524557fe8260ad772b9bff8a91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "common.h"
#include "mp_pwm_LR_.h"
#include "mp_pwm_L_.h"
#include "mp_pwm_R_.h"

// Variables for current limitation
static uint8_t curLim_temp;
static uint8_t curLim_bat;
static uint8_t curLim_soft;

// This file contains general pwm routines for mp (motor-power) board
//
// state_L_ : x - Inhib - changeDir - Dir - x - x - x - x
// Timer_L_ : timer dedicated to Left side

/** Initialize timers for left and right side PWM generation */
void init_timer_LR_(void) {
    init_pwm_L_();
    init_pwm_R_();

    TCNT_L_ = 0x00;
    TCNT_R_ = 0x80;	// 180° phase shifted to TCNT_L

    OCR_L_ = PWM_MIN_LR_;
    OCR_R_ = PWM_MIN_LR_;

    // set interrupts
    TIMSK |= TIMSK_LR_CFG;

    // launch timers
    TCCR_L_ = TCCR_LR_CFG;
    TCCR_R_ = TCCR_LR_CFG;
}

/** Initialize the timer for left and right current limitation */
void init_curLim (void) {
    curLim_soft = CURLIM_MAX;
    curLim_bat  = CURLIM_MAX;
    curLim_temp = CURLIM_MAX;

    // Enable outputs
    DDRD |= 0x30;

    // Configure and run current limit PWM
    TCCR1A = TCCRA_LR_CFG;
    TCCR1B = TCCRB_LR_CFG;

    // Configure and enable INT0 and INT1
    MCUCR |= MCUCR_LR_CFG;
    GICR  |= GICR_LR_CFG;

    // Apply the current limitation
    update_curLim();
}

uint8_t get_curLim_temp (uint8_t temperature) {
    return (temperature - 40) >> 2;	// TODO : ajuster la fonction de transfert
}

uint8_t get_curLim_bat (uint8_t battery) {
    return (battery - 40) >> 2;		// TODO : ajuster la fonction de transfert
}

/** Update the current limitation PWM
 * this function shall be called after each adjustment of any current limit */
inline void update_curLim(void) {
    uint8_t curLim_tmp;

    // search for MIN(curLim_soft, curLim_temp, curLim_bat)
    curLim_tmp = curLim_soft;

    /* TODO: implement curLim_temp and curLim_bat
    if (curLim_tmp > curLim_temp)
      {
        curLim_tmp = curLim_temp;
      }

    if (curLim_tmp > curLim_bat) 
      {
        curLim_tmp = curLim_bat;
      }
      */

    if (curLim_tmp > CURLIM_MAX) 
      {
        curLim_tmp = CURLIM_MAX;
      }

    // set curlim for _L_ and _R_ channel
    OCR_CurLim_L_ = curLim_tmp;
    OCR_CurLim_R_ = curLim_tmp;
}

// this function shall be called every 2ms or more often
void launch_envTest(void) {
    // TODO : acquerir les donnees de batterie
    // TODO : acquerir les donnees de temperature
    curLim_temp = 50;
    curLim_bat = 60;

    update_curLim();
}

/* Set the software-programmed current limitation */
void setCurLim_soft(uint8_t curLim) {
    curLim_soft = curLim;
    update_curLim();
}