Code Inve Pend
Code Inve Pend
#include <SPI.h>
/* Serial rates for UART */
#define BAUDRATE 115200
/* SPI commands */
#define AMT22_NOP 0x00
#define AMT22_RESET 0x60
#define AMT22_ZERO 0x70
/* Define special ascii characters */
#define NEWLINE 0x0A
#define TAB 0x09
/* We will use these define macros so we can write code once
compatible with 12 or 14 bit encoders */
#define RES12 12
#define RES14 14
/* SPI pins */
#define ENC_0 2
#define ENC_1 3
#define SPI_MOSI 51
#define SPI_MISO 50
#define SPI_SCLK 52
#define motorPin1 12
#define motorPin2 13
int alpha_raw;
int theta_raw;
float alpha_rad;
float theta_rad;
float theta_prev = 0;
float alpha_prev = 0;
float theta_dot;
float alpha_dot;
int t_old = 0;
int t_new = 0;
int dt = 0;
//Q = diag(10,1,5,1) and R=10
//Working Test 1
//float K_theta = -3.1623;
//float K_alpha = 93.4756;
//float K_theta_dot = -4.1504;
//float K_alpha_dot = 12.3874;
//Q = diag(10,1,5,1) and R=10
//Working Test 2
float K_theta = -3.1623;//-3.1623,-2.5623
float K_alpha = 92.84756;//92.84756
float K_theta_dot = -4.0991504;//-3.8991504
float K_alpha_dot = 12.003874;//12.003874
return data;
}
/*
* This function sets the state of the SPI line. It isn't necessary
but makes the code more readable than having digitalWrite everywhere
* This function takes the pin number of the desired device as an
input
*/
void setCSLine (uint8_t encoder, uint8_t csLine)
{
digitalWrite(encoder, csLine);
}
void setZeroSPI(uint8_t encoder)
{
spiWriteRead(AMT22_NOP, encoder, false);
//this is the time required between bytes as specified in the
datasheet.
//We will implement that time delay here, however the arduino is
not the fastest device so the delay
//is likely inherantly there already
delayMicroseconds(3);