Skip to content

Commit

Permalink
setting up code for Seesaw project, communications added
Browse files Browse the repository at this point in the history
  • Loading branch information
jzilly committed Dec 7, 2017
1 parent bfd8058 commit b9871c2
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 53 deletions.
4 changes: 2 additions & 2 deletions Src/freertos.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ void MX_FREERTOS_Init(void) {
// osThreadDef(MMITask, vtMMIStartTask, osPriorityHigh, 0, 128);
// MMITaskHandle = osThreadCreate(osThread(MMITask), NULL);

osThreadDef(DataTransmitTask, vtDataTransmitStartTask, osPriorityHigh, 0, 128);
osThreadDef(DataTransmitTask, vtDataTransmitStartTask, osPriorityNormal, 0, 128);
DataTransmitTaskHandle = osThreadCreate(osThread(DataTransmitTask), NULL);

osThreadDef(GyroTask, vtGyroStartTask, osPriorityNormal, 0, 128);
osThreadDef(GyroTask, vtGyroStartTask, osPriorityHigh, 0, 128);
GyroTaskHandle = osThreadCreate(osThread(GyroTask), NULL);

osThreadDef(ControlTask, vtControlStartTask, osPriorityNormal, 0, 128);
Expand Down
31 changes: 24 additions & 7 deletions userApp/tControl.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "tControlP.h"
#include "tControlPID.h"
#include "tControlLeadLag.h"
#include <math.h>

struct signalPackage seeSig;
char inSignalWrite = 0;
Expand All @@ -23,16 +24,21 @@ void vtControlStartTask(void const * argument) {
xMotorPWMCommand_t xMotorCommandBuffer; /* Buffer contains motor PWM values, to be sent to motor task*/
xIMUDATA_t xIMUDataBuffer; /* Buffer contains IMU read outs, came in from gyro task*/
double dAnglePos, dAngleCorrectionConst = 0.4; /* max variation of motor base speed*/

double dAnglePosPrev = 0.0;
/* local constants ------------------------------------------------- */

// const double dMotorTrustConsta = 9.1493e-6, dMotorTrustConstb = 6.7674e-4,
// dMotorTrustConstc = 5.48456e-4; /* motor constants derived from Mike Hammers paper*/
const uint16_t uiMotorBaseSpeed = 10; /* motor base speed, PWM value [40 ... 100];*/
const uint16_t uiMotorBaseSpeed = 9; /* motor base speed, PWM value [40 ... 100];*/
double tLast = 0.0;

double r = 0.0;
double dN = 0.0; // COUNT FOR REFERENCE SIGNAL
double dPeriod = 600; // Period length constant of reference signal

while (1) {
/* Reading IMU data from the Queue*/
osEvent evt = osMailGet(GyrodataQueueHandle, 100);
osEvent evt = osMailGet(GyrodataQueueHandle, 50);
if (evt.status == osEventMail) {
xIMUDATA_t *IMUData = (xIMUDATA_t *) evt.value.p;
xIMUDataBuffer.AccelX = IMUData->AccelX;
Expand All @@ -45,26 +51,37 @@ void vtControlStartTask(void const * argument) {
}

/* Normalize the measured angle to [-1...1]*/
r = 0 ; //6000*sin(dN/dPeriod);
dN++;
if (dN > 2*3.14*dPeriod) dN = 0; // Enforce bounds on dN

dAnglePos = (double) xIMUDataBuffer.AccelX;

dAnglePosPrev = dAnglePos;


dAnglePos = dAnglePos - r; // Create difference to reference signal

/* calculation of the control input */
double systemTime = (double) xTaskGetTickCount();
double dt = (systemTime - tLast)*0.001;
double dt = (systemTime - tLast); //*0.001;
tLast = systemTime;

double e = dLP(dAnglePos, dt);
double u = dLeadLag(dAnglePos, dt);
// double u = dLeadLag(dAnglePos, dt);
//double u = dP(dAnglePos);
//double u = dPID(dAnglePos,e,dt);
double u = dPID(dAnglePos,e,dt);
//double u = dLeadLag(e, dt);
//double u = dP(e);



/* signal transmission */
inSignalWrite = 1;
seeSig.e = e;
seeSig.y = dAnglePos;
seeSig.u = u;
seeSig.r = r;

inSignalWrite = 0;

/* Control starts here */
Expand Down
3 changes: 2 additions & 1 deletion userApp/tControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ extern osMessageQId GyrodataQueueHandle;

#pragma pack(push, 1) // exact fit - no padding
struct signalPackage{
double e;
double r;
double y;
double u;
};
#pragma pack(pop) //back to whatever the previous packing mode was
Expand Down
8 changes: 4 additions & 4 deletions userApp/tControlPID.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/


double kp = 0.00001;
double Ti = 1000;
double Td = 0.0;
double kp = 0.000005;
double Ti = 2000;
double Td = 0.03;

double e_prev = 0.0;
double ef_prev = 0.0;
Expand All @@ -30,7 +30,7 @@ double dPID(double e, double ef, double dt) {
delta_uD = kp * Td / dt * (ef - 2 * ef_prev + ef_2prev);

// calculate PID output value
delta_u = delta_uP + delta_uI + delta_uD;
delta_u = delta_uP + delta_uD + delta_uI; // + delta_uD;
u = u_prev + delta_u;

// update values
Expand Down
2 changes: 0 additions & 2 deletions userApp/tDataTransmit.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
struct dataPackage {
uint32_t systemTime;
struct signalPackage signals;
double testDouble;
};
#pragma pack(pop) //back to whatever the previous packing mode was

Expand Down Expand Up @@ -49,7 +48,6 @@ void vtDataTransmitStartTask(void const * argument) {
while (inSignalWrite) {
}
dataP.signals = seeSig;
dataP.testDouble = 2.71282;
memcpy(&uaBuffer[3], &dataP, sizeof(dataP));

/* checksum */ //TODO
Expand Down
55 changes: 19 additions & 36 deletions userApp/tGyro.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,17 @@ void vtGyroStartTask(void const * argument)
{
/* read data from IMU */
vReadIMU(&hi2c1, &xIMUReceiveBuffer.AccelX, LSM6DS3_OUTX_H_XL, LSM6DS3_OUTX_L_XL);
vReadIMU(&hi2c1, &xIMUReceiveBuffer.AccelY, LSM6DS3_OUTY_H_XL, LSM6DS3_OUTY_L_XL);
vReadIMU(&hi2c1, &xIMUReceiveBuffer.AccelZ, LSM6DS3_OUTZ_H_XL, LSM6DS3_OUTZ_L_XL);
vReadIMU(&hi2c1, &xIMUReceiveBuffer.GyroX, LSM6DS3_OUTX_H_G, LSM6DS3_OUTX_L_G);
// vReadIMU(&hi2c1, &xIMUReceiveBuffer.AccelY, LSM6DS3_OUTY_H_XL, LSM6DS3_OUTY_L_XL);
// vReadIMU(&hi2c1, &xIMUReceiveBuffer.AccelZ, LSM6DS3_OUTZ_H_XL, LSM6DS3_OUTZ_L_XL);
// vReadIMU(&hi2c1, &xIMUReceiveBuffer.GyroX, LSM6DS3_OUTX_H_G, LSM6DS3_OUTX_L_G);
vReadIMU(&hi2c1, &xIMUReceiveBuffer.GyroY, LSM6DS3_OUTY_H_G, LSM6DS3_OUTY_L_G);
vReadIMU(&hi2c1, &xIMUReceiveBuffer.GyroZ, LSM6DS3_OUTZ_H_G, LSM6DS3_OUTZ_L_G);
// vReadIMU(&hi2c1, &xIMUReceiveBuffer.GyroZ, LSM6DS3_OUTZ_H_G, LSM6DS3_OUTZ_L_G);

/* send data to Queue */
osMailPut(GyrodataQueueHandle, &xIMUReceiveBuffer);

/* send data to the UART */
if(SEND_GYRO_DATA_TO_USART){
uint8_t ucUARTSendBuffer[18];

/* clear send buffer */
for(uint8_t i=0; i<14; i++)
{ucUARTSendBuffer[i]=0; }

/* setup buffer structure */
ucUARTSendBuffer[7] = '\t';
ucUARTSendBuffer[8] = '\t';
ucUARTSendBuffer[17] = '\n';
ucUARTSendBuffer[16] = '\r';

/* load send Buffer with data*/
itoa(xIMUReceiveBuffer.GyroY, ucUARTSendBuffer, 10);
itoa(xIMUReceiveBuffer.AccelZ, &ucUARTSendBuffer[9], 10);

/* send buffer to UART */
HAL_UART_Transmit(& huart2, &ucUARTSendBuffer, sizeof(ucUARTSendBuffer), 100);
}



/* Delay to slow down this task. FixMe: change IMU readout to be controlled by interrupt */
osDelay(1);
osDelay(5);
}
}

Expand All @@ -72,7 +48,7 @@ void vGyroInitLSMD6DS3 (void)

/* Init gyroscope */
/* ODRG = Gyroscope output data rate selection. Default value: ODR_OFF */
/* FSG = Gyroscope full-scale selection. Default value: 245°/s */
/* FSG = Gyroscope full-scale selection. Default value: 245/s */
uiI2C_Data[0] = LSM6DS3_CTRL2_G;
uiI2C_Data[1] = (LSM6D3_ODRG_104HZ | LSM6D3_FSG_245DPS);
HAL_I2C_Master_Transmit(&hi2c1, (uint16_t) (LSM6DS2_ADRESS), (uint8_t *) uiI2C_Data, 0x02, 100);
Expand All @@ -94,12 +70,19 @@ void vGyroInitLSMD6DS3 (void)
*/
void vReadIMU(I2C_HandleTypeDef *pxI2CHandler, int16_t *pxDataValue, uint8_t I2C_HByte, uint8_t I2C_LByte)
{
int8_t pcData;

HAL_I2C_Mem_Read(pxI2CHandler, LSM6DS2_ADRESS, I2C_HByte , 0x01, &pcData, 0x01, 100);
*pxDataValue = (int16_t) pcData<<8;
HAL_I2C_Mem_Read(pxI2CHandler, LSM6DS2_ADRESS, I2C_LByte , 0x01, &pcData, 0x01, 100);
*pxDataValue = *pxDataValue | (int16_t) pcData;
uint8_t isOk = 1;
HAL_StatusTypeDef status;
uint8_t pcData;
int16_t data;

status = HAL_I2C_Mem_Read(pxI2CHandler, LSM6DS2_ADRESS, I2C_HByte , 0x01, &pcData, 0x01, 100);
isOk &= status == HAL_OK;
data = pcData << 8;
status = HAL_I2C_Mem_Read(pxI2CHandler, LSM6DS2_ADRESS, I2C_LByte , 0x01, &pcData, 0x01, 100);
isOk &= status == HAL_OK;
data |= pcData;
if (isOk)
*pxDataValue = data;
}


Expand Down
2 changes: 1 addition & 1 deletion userApp/tLowPass.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "tLowPass.h"

double y0 = 0.0;
double k = 0.01;
double k = 0.4;


double dLP(double e, double h){
Expand Down

0 comments on commit b9871c2

Please sign in to comment.