Commit 97dd55e3 authored by tsharith's avatar tsharith
Browse files

Merge branch 'MainLoop' into 'master'

Main loop

See merge request tsharith/373-scaffoldingbot!1
parents 76f11a50 4670f3b7
#include "locomotion.h"
#include "printing.h"
#include "drivers/mss_ace/mss_ace.h"
#include "drivers/mss_pdma/mss_pdma.h"
#include "drivers/mss_timer/mss_timer.h"
const int timerPeriod=1000000*1000/2;
uint tempC=0;
void Timer1_IRQHandler( void ){
move_forward(100);
*((uint32_t*) 0x40050014) = 0xffff9a65;
/*
if(states_remaining!=0){
MSS_TIM1_init(MSS_TIMER_ONE_SHOT_MODE);
MSS_TIM1_load_immediate(timerPeriod);
MSS_TIM1_start();
MSS_TIM1_enable_irq();
}
if(states_remaining == 0) {
MSS_TIM1_stop();
MSS_GPIO_set_outputs(0);
}
*/
MSS_TIM1_clear_irq();
}
void Timer2_IRQHandler( void ){
double percent = (double)Steps_taken/Required_steps;
percent *= 100;
......@@ -34,141 +15,156 @@ void Timer2_IRQHandler( void ){
MSS_TIM2_clear_irq();
}
void reportTemp() {
void tempUARTtxTimerInit()
{
MSS_TIM2_init(MSS_TIMER_PERIODIC_MODE);
MSS_TIM2_load_immediate(100000000);
MSS_TIM2_start();
MSS_TIM2_enable_irq();
}
void move() {
MSS_TIM1_init(MSS_TIMER_PERIODIC_MODE);
MSS_TIM1_load_immediate(timerPeriod);
MSS_TIM1_start();
MSS_TIM1_enable_irq();
}
int get_Temp(double voltage){
if(voltage>2.109) return -84.532*voltage+327.26;
else {
return 289.87*exp(-0.353*voltage);
typedef enum PrintOpts
{
CUBE=0, TOWER, WALLS
};
typedef enum SBstate
{
INIT = 0, IDLE,PRINT_INIT,PRINT, ABORT
};
typedef struct ScaffoldingBot
{
SBstate status = IDLE;
Printing * p = NULL;
PrintOpts print_sel = 255;
}SB;
int init(ScaffoldingBot *sb)
{
printf("+++ Initializing Scaffolding Bot +++\n");
printf("--- Init Printing ---\n");
if(!sb)
{
printf("ScaffoldingBot Struct Improperly Allocated\n");
return 0;
}
}
sb->p = malloc(sizeof(p));
if(!sb->p)
{
printf("Printing Struct Improperly Allocated\n");
return 0;
}
sb->p->extruder_period = 10000000/32;
sb->p->extruder_excite = 0x936c;
sb->heating_setpoint = 185;
sb->heat_ctrl_sensitivity = 10;
int getVoltage(double temp){
if(temp > 148.982) return (temp - 327.26)/(-84.532);
else {
return log(temp/(289.87))/(-0.353);
init_print();
if(readingToTemp(sb-p->E_shutoff) > 250)
{
printf("Emergency Shutoff Setting too high, unsafe for operation\n");
return 0;
}
}
int voltageToReading(double voltage){
return voltage / 2.56 * 4096;
}
if(ACE_get_ppe_sample(sb->p->adc_handler) != get_raw_thermistor)
{
printf("Misalignment between peripheral CoreABC and ACE thermistor readings\n");
return 0;
}
int main()
{
waitT();
MSS_UART_init(&g_mss_uart1, MSS_UART_115200_BAUD,
MSS_UART_DATA_8_BITS | MSS_UART_NO_PARITY | MSS_UART_ONE_STOP_BIT);
reportTemp();
waitT();
printf("--- Init Locomotion ---\n");
init_loco();
printf("--- Init UART ---\n");
MSS_UART_init(
&g_mss_uart1,
MSS_UART_115200_BAUD,
MSS_UART_DATA_8_BITS | MSS_UART_NO_PARITY | MSS_UART_ONE_STOP_BIT
);
printf("--- Init Interrupts ---\n");
NVIC_EnableIRQ(GPIO0_IRQn);
MSS_GPIO_config( MSS_GPIO_0, MSS_GPIO_INPUT_MODE | MSS_GPIO_IRQ_EDGE_POSITIVE );
MSS_GPIO_enable_irq( MSS_GPIO_0 );
// PDMA_init();
/*PDMA_configure(
PDMA_CHANNEL_0,
PDMA_FROM_ACE,
PDMA_LOW_PRIORITY | PDMA_WORD_TRANSFER | PDMA_INC_DEST_FOUR_BYTES,
PDMA_DEFAULT_WRITE_ADJ
);
uint32_t holder;
PDMA_start(
PDMA_CHANNEL_0,
PDMA_ACE_PPE_DATAOUT,
((uint32_t)HE_Base + SCALED_SP),
sizeof(holder)
);*/
// uint therm=0;
// uint tempC=0;
// uint hotEndStatus=0;
// double voltage=1;
// while(1)
// {
// printf("%d",tempC);
// therm = get_raw_thermistor();
// voltage=((double)therm)/4096.0*2.56;
// tempC=get_Temp(voltage);
// *(uint32_t*)((uint32_t)EX_Base + STEPS) = 50;
// if (tempC>190)
// hotEndStatus=0;
// else if (tempC<180)
// hotEndStatus=1;
// else {
// int randNum=(double)rand()/RAND_MAX*10;
// if(randNum<(190-tempC))
// hotEndStatus=1;
// else
// hotEndStatus=0;
// }
//
//
// set_hotend_switch(hotEndStatus);
//
// }
uint therm = get_raw_thermistor();
init_print();
init_loco();
printf("--- Init Timers ---\n");
tempUARTtxTimerInit();
return 1;
}
// DEBUG BEGIN
uint32_t experiod, scaledsp, pidkp, pidki, pidkd, sens, therm2, pidsp;
char automode, sw, fan;
experiod = get_extruder_period();
automode = get_hotend_auto();
scaledsp = get_hotend_sp();
sw = get_hotend_switch();
fan = get_hotend_fan();
pidkp = get_hotend_pid_k(KP);
pidki = get_hotend_pid_k(KI);
pidkd = get_hotend_pid_k(KD);
sens = get_hotend_pid_sensitivity();
therm = get_raw_thermistor();
pidsp = get_pid_sp();
// DEBUG END
char hotendTog = 1, fanTog = 1;
move();
while( 1 )
void closeSB (ScaffoldingBot * sb)
{
if (!sb) return;
else if (sb->p) free(p);
}
SB * sbot;
int main()
{
sbot = malloc(sizeof(SB));
int exit = 0;
while(!exit)
{
uint8_t buf[] = "20\n\r";
MSS_UART_polled_tx(&g_mss_uart0, buf, sizeof(buf));
extrude(1);
// DEBUG BEGIN
experiod = get_extruder_period();
automode = get_hotend_auto();
scaledsp = get_hotend_sp();
sw = get_hotend_switch();
fan = get_hotend_fan();
pidkp = get_hotend_pid_k(KP);
pidki = get_hotend_pid_k(KI);
pidkd = get_hotend_pid_k(KD);
sens = get_hotend_pid_sensitivity();
therm = get_raw_thermistor();
therm2 = *(uint32_t*)(0x40050220);//get_raw_thermistor();
// *(&ACE->PPE_FLAGS0_IRQ_CLR) = 0xFFFFFFFF;
// set_raw_thermistor(*(&ACE->PPE_FLAGS0));
pidsp = get_pid_sp();
hotendTog = hotendTog ? 0 : 1;
fanTog = fanTog ? 0 : 1;
// DEBUG END
set_hotend_switch(hotendTog);
set_hotend_fan(fanTog);
switch(sbot->status)
{
case INIT:
if(!init(sbot))
sbot->status = ABORT;
else
sbot->status = IDLE;
break;
case IDLE:
break;
case PRINT_INIT:
// assuming print sel has been set in handler
switch(sbot->print_sel)
{
case CUBE:
cube();
sbot->status = PRINT;
break;
case TOWER:
tower();
sbot->status = PRINT;
break;
case: WALLS:
walls();
sbot->status = PRINT;
break;
default:
sbot->state = IDLE;
}
break;
case PRINT:
break;
case ABORT:
set_hotend_switch(sbot->p, 1);
set_hotend_fan(sbot->p,1);
extrude(sbot->p, -5);
while(voltageToTemp(readingToVoltage(get_raw_thermistor(sbot->p))) > 110)
{
}
extrude(sbot->p, -100);
exit = 1;
break;
default:
}
}
return 0;
closeSB(sbot);
free(sbot);
}
......@@ -9,13 +9,50 @@
#define PRINTING_H_
#include "drivers/mss_ace/mss_ace.h"
#define E_STEPS_PER_MM 837
#define E_STEPS_PER_MM 50
// UTILITY
int voltageToTemp(double voltage){
if(voltage>2.109) return -84.532*voltage+327.26;
else {
return 289.87*exp(-0.353*voltage);
}
}
int tempToVoltage(double temp){
if(temp > 148.982) return (temp - 327.26)/(-84.532);
else {
return log(temp/(289.87))/(-0.353);
}
}
int voltageToReading(double voltage){
return voltage / 2.56 * 4096;
}
int readingToVoltage(double reading){
return reading / 4096 * 2.56;
}
int manualHotendCtrl( void ){
uint32_t hotEndStatus;
if (tempC>190)
hotEndStatus=0;
else if (tempC<180)
hotEndStatus=1;
else {
int randNum=(double)rand()/RAND_MAX*10;
if(randNum<(190-tempC))
hotEndStatus=1;
else
hotEndStatus=0;
}
set_hotend_switch(hotEndStatus);
}
// END UTILITY
volatile uint32_t* EX_Base = (uint32_t*) 0x40050100; // EXTRUDER
volatile uint32_t* HE_Base = (uint32_t*) 0x40050200; // HOTEND
volatile uint32_t* test = (uint32_t*) 0x40050208;
ace_channel_handle_t adc_handler;
typedef enum
{
PERIOD = 0x00,
......@@ -25,172 +62,115 @@ typedef enum
typedef enum
{
AUTO = 0x00,
SCALED_SP = 0x04,
SW = 0x08,
FAN = 0x0c,
PID_KP = 0x10,
PID_KI = 0x14,
PID_KD = 0x18,
AUTO = 0x00,
SP = 0x04,
SW = 0x08,
FAN = 0x0c,
AVGINGLIM = 0x18,
SENSITIVITY = 0x1c,
THERMISTOR = 0x20,
PID_SP = 0x24
THERMISTOR = 0x20,
} he;
typedef enum PIDconst
typedef struct Printing
{
KP, KI, KD
} pidc;
volatile uint32_t* EX_Base = (uint32_t*) 0x40050100; // EXTRUDER
volatile uint32_t* HE_Base = (uint32_t*) 0x40050200; // HOTEND
ace_channel_handle_t adc_handler;
uint32_t extruder_period = 10000000/32;
uint32_t extruder_excite = 0x936c;
uint32_t heating_setpoint = 185;
uint32_t heat_ctrl_sensitivity = 10;
const uint32_t E_shutoff = 2134;
};
void init_print()
void init_print(Printing *p)
{
// EXTRUDER
*(uint32_t*)((uint32_t)EX_Base + PERIOD) = 10000000/32;
*(uint32_t*)((uint32_t)EX_Base + STEPS) = 0;
*(uint32_t*)((uint32_t)EX_Base + EXCITE) = 0x936c;
*(uint32_t*)((uint32_t)p->EX_Base + PERIOD) = p->extruder_period;
*(uint32_t*)((uint32_t)p->EEX_Base + STEPS) = 0;
*(uint32_t*)((uint32_t)p->EEX_Base + EXCITE) = p->extruder_excite;
// HOTEND
*(uint32_t*)((uint32_t)HE_Base + AUTO) = 0;
*(uint32_t*)((uint32_t)HE_Base + SCALED_SP) = 0;
*(uint32_t*)((uint32_t)HE_Base + SW) = 0;
*(uint32_t*)((uint32_t)HE_Base + FAN) = 0;
*(uint32_t*)((uint32_t)HE_Base + PID_KP) = 0;
*(uint32_t*)((uint32_t)HE_Base + PID_KI) = 0;
*(uint32_t*)((uint32_t)HE_Base + PID_KD) = 0;
*(uint32_t*)((uint32_t)HE_Base + SENSITIVITY) = 10;
*(&ACE->PPE_FLAGS0_IRQ_EN) = 0xFFFFFFFF; // Thermistor THRESH_FLAGS enable
*(&ACE->FPGA_FLAGS_SEL) = 0xFFFFFFFF;
ACE_init();
adc_handler = ACE_get_channel_handle((const uint8_t *)"ADCDirectInput_5");
adc_handler = ACE_get_channel_handle((const uint8_t *)"ADCDirectInput_5");
*(uint32_t*)((uint32_t)p->HE_Base + AUTO) = 0;
*(uint32_t*)((uint32_t)p->HE_Base + SP) = voltageToReading(tempToVoltage(p->heating_setpoint));
*(uint32_t*)((uint32_t)p->HE_Base + SW) = 0;
*(uint32_t*)((uint32_t)p->HE_Base + FAN) = 0;
*(uint32_t*)((uint32_t)p->HE_Base + SENSITIVITY) = p->heat_ctrl_sensitivity;
ACE_init(); // Thermistor Readings
p->adc_handler = ACE_get_channel_handle((const uint8_t *)"ADCDirectInput_5");
}
void extrude(int mm)
void extrude(Printing *p, int32_t mm)
{
*(uint32_t*)((uint32_t)EX_Base + STEPS) = mm * E_STEPS_PER_MM;
*(uint32_t*)((uint32_t)p->EX_Base + STEPS) = mm * E_STEPS_PER_MM;
}
/*** SETTERS ***/
void set_extruder_period( uint32_t per )
void set_extruder_period(Printing *p, uint32_t per )
{
*(uint32_t*)((uint32_t)EX_Base + PERIOD) = per;
*(uint32_t*)((uint32_t)p->EX_Base + PERIOD) = per;
}
void set_hotend_auto(char on_off)
void set_hotend_auto(Printing *p,char on_off)
{
*(uint32_t*)((uint32_t)HE_Base + AUTO) = on_off;
*(uint32_t*)((uint32_t)p->HE_Base + AUTO) = on_off;
}
void set_hotend_sp(uint32_t scaled_sp)
void set_hotend_sp(Printing *p,uint32_t temp)
{
*(uint32_t*)((uint32_t)HE_Base + SCALED_SP) = scaled_sp;
*(uint32_t*)((uint32_t)p->HE_Base + SP) = voltageToReading(tempToVoltage(temp));
}
void set_hotend_switch(char on_off)
void set_hotend_switch(Printing *p,char on_off)
{
*(uint32_t*)((uint32_t)HE_Base + SW) = on_off;
*(uint32_t*)((uint32_t)p->HE_Base + SW) = on_off;
}
void set_hotend_fan(char on_off)
void set_hotend_fan(Printing *p,char on_off)
{
*(uint32_t*)((uint32_t)HE_Base + FAN) = on_off;
*(uint32_t*)((uint32_t)p->HE_Base + FAN) = on_off;
}
void set_hotend_pid_k(enum PIDconst p, uint32_t val )
void set_hotend_sensitivity(Printing *p, uint32_t val)
{
switch(p)
{
case KP:
*(uint32_t*)((uint32_t)HE_Base + PID_KP) = val;
break;
case KI:
*(uint32_t*)((uint32_t)HE_Base + PID_KI) = val;
break;
case KD:
*(uint32_t*)((uint32_t)HE_Base + PID_KD) = val;
break;
}
}
void set_hotend_pid_sensitivity( uint32_t val)
{
*(uint32_t*)((uint32_t)HE_Base + SENSITIVITY) = val;
*(uint32_t*)((uint32_t)p->HE_Base + SENSITIVITY) = val;
}
/*** GETTERS ***/
uint32_t get_extruder_period( void )
{
return *(uint32_t*)((uint32_t)EX_Base + PERIOD);
}
char get_hotend_auto( void )
uint32_t get_extruder_period( Printing *p)
{
return *(uint32_t*)((uint32_t)HE_Base + AUTO);
return *(uint32_t*)((uint32_t)p->EX_Base + PERIOD);
}
uint32_t get_hotend_sp( void )
char get_hotend_auto(Printing *p)
{
return *(uint32_t*)((uint32_t)HE_Base + SCALED_SP);
return *(uint32_t*)((uint32_t)p->HE_Base + AUTO);
}
char get_hotend_switch( void )
uint32_t get_hotend_sp( Printing *p)
{
return *test;
return voltageToTemp(readingToVoltage(*(uint32_t*)((uint32_t)p->HE_Base + SP)));
}
char get_hotend_fan( void )
char get_hotend_fan( Printing *p)
{
return *(uint32_t*)((uint32_t)HE_Base + FAN);
return *(uint32_t*)((uint32_t)p->HE_Base + FAN);
}
uint32_t get_hotend_pid_k( enum PIDconst p )
uint32_t get_raw_thermistor( Printing *p)
{
uint32_t retval = 404;
switch(p)
{
case KP:
retval = *(uint32_t*)((uint32_t)HE_Base + PID_KP);
break;
case KI:
retval = *(uint32_t*)((uint32_t)HE_Base + PID_KI);
break;
case KD:
retval = *(uint32_t*)((uint32_t)HE_Base + PID_KD);
break;
}
return retval;
return *(uint32_t*)((uint32_t)p->HE_Base + THERMISTOR);
//return ACE_get_ppe_sample(p->adc_handler);
}