Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
MPPT.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 ENAC, Pascal Brisset, Michel Gorraz
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
29 #include <stdbool.h>
30 #include "modules/energy/MPPT.h"
32 #include "mcu_periph/i2c.h"
33 
34 
35 #define MPPT_SLAVE_ADDR 0x40
36 #define NB_I2C_DATA 8
37 #define MPPT_MODE_ADDR 0xf
38 
40 
56 #include "mcu_periph/uart.h"
57 #include "pprzlink/messages.h"
59 
64 #define MPPT_STATUS_IDLE 0
65 #define MPPT_STATUS_WRITING 1
66 #define MPPT_STATUS_ASKING 2
67 #define MPPT_STATUS_READING 3
68 
69 
70 static uint8_t data_index = 0xff;
72 
73 void MPPT_init(void)
74 {
75  MPPT_mode = 0;
77 }
78 
79 
80 static void MPPT_ask(void)
81 {
82  data_index++;
83  if (data_index >= NB_I2C_DATA) {
84  /* Setting the current value */
85  fbw_current_milliamp = MPPT_data[MPPT_IBAT_INDEX];
86 
88  DOWNLINK_SEND_MPPT(DefaultChannel, DefaultDevice, NB_DATA, MPPT_data);
89  data_index = 0;
90  }
91 
95 }
96 
97 void MPPT_periodic(void)
98 {
99 
101  switch (MPPT_status) {
102  case MPPT_STATUS_IDLE:
103  /* If free, change mode if needed */
104  if (MPPT_mode) {
106  mppt_trans.buf[1] = 0;
107  mppt_trans.buf[2] = MPPT_mode;
109  MPPT_mode = 0;
111  } else {
112  MPPT_ask();
113  }
114  break;
115 
116  case MPPT_STATUS_WRITING:
118  break;
119 
120  case MPPT_STATUS_ASKING:
121  /* The slave should send 2 bytes */
124  break;
125 
126  case MPPT_STATUS_READING:
127  /* We got 2 bytes */
128  if (data_index < NB_I2C_DATA) {
129  MPPT_data[data_index] = (mppt_trans.buf[0] << 8) | mppt_trans.buf[1];
130  }
132  break;
133  }
134  }
135 }
MPPT_mode
uint8_t MPPT_mode
0: VBat (mV) 1: IBat (mA) 2: PBat (mW) 3: VSol (mV) 4: ISol (mA) 5: PSol (mW) 6: IConv (mA) 7: PConv ...
Definition: MPPT.c:60
mppt_trans
struct i2c_transaction mppt_trans
Definition: MPPT.c:39
i2c_transaction::buf
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
MPPT.h
Solar cells MPTT monitoring.
MPPT_status
static uint8_t MPPT_status
A value different from 0 is a request from this mode.
Definition: MPPT.c:63
MPPT_STATUS_READING
#define MPPT_STATUS_READING
Definition: MPPT.c:67
NB_DATA
#define NB_DATA
Definition: generic_com.c:38
MPPT_IBAT_INDEX
#define MPPT_IBAT_INDEX
Definition: MPPT.h:39
MPPT_init
void MPPT_init(void)
Definition: MPPT.c:73
MPPT_STATUS_IDLE
#define MPPT_STATUS_IDLE
Definition: MPPT.c:64
data_index
static uint8_t data_index
Definition: MPPT.c:70
I2CTransSuccess
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition: i2c.h:57
uart.h
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
NB_I2C_DATA
#define NB_I2C_DATA
Definition: MPPT.c:36
i2c_transmit
bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Submit a write only transaction.
Definition: i2c.c:324
MPPT_STATUS_WRITING
#define MPPT_STATUS_WRITING
Definition: MPPT.c:65
int16_t
signed short int16_t
Definition: types.h:17
MPPT_STATUS_ASKING
#define MPPT_STATUS_ASKING
Definition: MPPT.c:66
uint8_t
unsigned char uint8_t
Definition: types.h:14
MPPT_ICONV_INDEX
#define MPPT_ICONV_INDEX
Definition: MPPT.h:40
i2c_transaction::status
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
i2c_transaction
I2C transaction structure.
Definition: i2c.h:93
MPPT_ask
static void MPPT_ask(void)
Definition: MPPT.c:80
MPPT_MODE_ADDR
#define MPPT_MODE_ADDR
Definition: MPPT.c:37
MPPT_data
static int16_t MPPT_data[NB_DATA]
Definition: MPPT.c:71
MPPT_ITOTAL_INDEX
#define MPPT_ITOTAL_INDEX
Definition: MPPT.h:41
MPPT_periodic
void MPPT_periodic(void)
Definition: MPPT.c:97
main_fbw.h
MPPT_SLAVE_ADDR
#define MPPT_SLAVE_ADDR
Definition: MPPT.c:35
i2c.h
i2c_receive
bool i2c_receive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint16_t len)
Submit a read only transaction.
Definition: i2c.c:334