for_vlad
for_vlad
h>
#include <stdbool.h>
#include "sleep.h"
#include "config.h"
#include "timer_f.h"
#include "platform.h"
#include "SH1106_Screen.h"
#include "xgpio.h"
#include "xadcps.h"
#include "xiicps.h"
#include "xtime_l.h"
#include "xtmrctr.h"
#include "xscugic.h"
#include "xscutimer.h"
#include "xil_printf.h"
#include "xparameters.h"
#include "xil_exception.h"
// Polling
#define Time_Divisor 4 // will be used to divide
the number of counts per second
float time_record;
// ADC
static XAdcPs XAdcInst; // XADC driver instance
#define XADC_DEVICE_ID XPAR_XADCPS_0_DEVICE_ID // XADC definitions
const float CHANNEL[4] = {1, 9, 6, 15}; // XADC channels
float averagedIntensity[4]; // (top, bottom, left, right)
// PWM Configuration
XTmrCtr TimerCounterInst0; // Xtimer for X motor
XTmrCtr TimerCounterInst1; // Xtimer for Y motor
#define DEADZONE 500 // To prevent motor from constantly moving
#define PWM_PERIOD 5000000 // 5 ms period
#define MIN_IDEAL_EFFICIENCY 97.0f // Threshold efficiency to prevent constant
movement
u32 HIGHTIME0 = 0; // Duty cycle for X-axis motor
u32 HIGHTIME1 = 0; // Duty cycle for Y-axis motor
//LED
XGpio leds; // LED instance
#define LEDSDEVICE_ID XPAR_USER_LEDS_GPIO_DEVICE_ID //user controlled LEDs
#define LED_CHANNEL 1 // GPIO channel for LED
// Buttons
XGpio buttons;
#define BUTTON_DEVIE_ID XPAR_USER_BTNS_GPIO_DEVICE_ID
#define BUTTON_CHANNEL 1
float intensityData[4]; // Array for XADC values (A0, A1, A2, A3)
char buffer1[20], buffer2[20], buffer3[20], buffer4[20];
// Function Prototypes
int configXadc(u16 xadc_id);
float XAdcPs_RawToVoltage_own(u16 adcData);
float calculateEfficiency(float val1, float val2);
void printGPIOoutputs();
void axisAverage(float intensity[]);
void recalibrateComp(int number_of_channels);
void SetPinOutputs(int pin0, int pin1, int pin2, int pin3);
void controlMovement(float intensityData[], char *buffer3, char *buffer4);
int main() {
init_platform();
initDisplay();
// Configure XADC
if (configXadc(XADC_DEVICE_ID) != XST_SUCCESS) {
xil_printf("XADC Configuration Failed.\n\r");
return XST_FAILURE;
}else{xil_printf("XADC Initialised Successfully.\n\r");}
// Initialising polling
XTime current_time;
XTime last_time = 0;
char PrevButtonState;
int count = 0;
while (1) {
XTime_GetTime(¤t_time);
// 325,000,000 / Time_Divisor
if ( (current_time - last_time) > (COUNTS_PER_SECOND / Time_Divisor) ){
//Polling reset
last_time = current_time;
PrevButtonState = ButtonState;
// 0---T[0]---0
// | |
// L[2] R[3]
// | |
// 0---B[1]---0
// Update display
sprintf(buffer1, " X-Axis = %.1f%% ", efficiency_x);
sprintf(buffer2, " Y-Axis = %.1f%% ", efficiency_y);
printCentreX(1, " Efficiency: ");
printCentreX(17, buffer1);
printCentreX(29, buffer2);
printDisplay(2, 46, buffer3);
printDisplay(98, 46, buffer4);
}
}
cleanup_platform();
return 0;
}
// Configure XADC
int configXadc(u16 xadc_id) {
XAdcPs_Config *ConfigPtr;
ConfigPtr = XAdcPs_LookupConfig(xadc_id);
if (!ConfigPtr) return XST_FAILURE;
// Calculate efficiency
float calculateEfficiency(float val1, float val2) {
//always get a percentage < 100
if (val1 > val2){return ((val2 * 100) / val1);}
else {return ((val1 * 100) / val2);}
}
// X-axis
if ((intensityData[1] - DEADZONE) > intensityData[0] && efficiency_x <
MIN_IDEAL_EFFICIENCY) {
sprintf(buffer3, "Rotating: LEFT ");
HIGHTIME0 = (u32)((PWM_PERIOD * PERCENT) / (efficiency_x));
} else {
sprintf(buffer3, "Rotating: _____");
HIGHTIME0 = 0; // 0% duty cycle
// Y-axis
if ((intensityData[1] - DEADZONE) > intensityData[2] && efficiency_y <
MIN_IDEAL_EFFICIENCY) {
sprintf(buffer4, "UP ");
HIGHTIME1 = (u32)((PWM_PERIOD * PERCENT) / (efficiency_y * 2)); // Update
PWM for Y motor
} else {
sprintf(buffer4, "____");
HIGHTIME1 = 0; // 0% duty cycle
printGPIOoutputs();
}
void printGPIOoutputs(){
// Print the pin states in the same format as the PWM output
xil_printf(" Pin4 (LEFT) (IN1) = %d | Pin6 (RIGHT) (IN2) = %d | Pin8 (UP)
(IN3) = %d | Pin10 (DOWN) (IN4) = %d \n",
pin0_state, pin1_state, pin2_state, pin3_state);
}
//compensator re-calibration
void recalibrateComp(int number_of_channels){
float highestReading = 0.0f;
float currentReading;
float dataArray[4];
}
}
//for 4 LDRs
void axisAverage(float intensity[]){
// [0]--------[1]
// | |
// [2]--------[3]
// y axis values
averagedIntensity[2] = (intensity[0] + intensity[2])/2; //left
averagedIntensity[3] = (intensity[1] + intensity[3])/2; //right