Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Yeferson yate monrroy
Created February 8, 2017

Thermal therapy

This project was based on therapeutic benefit through heat therapy in people suffering from idiopathic scoliosis

42
Thermal therapy

Things used in this project

Story

Read more

Schematics

Protoboard

Code

Code

C/C++
/* Header File Includes */
#include <project.h>

//ALS
/* IIR Filter coefficient */
/* Cut off frequency = fs/(2 * pi * iir_filter_constant).  In this project fs ~= 1 ksps.
This results in a cut-off frequency of 15.91 Hz.  We are using IIR filter as FIR requires 
more order of filter to get the same cut-off frequency*/
#define FILTER_COEFFICIENT_ALS			        (10)

/* Constants for photodiode current calculation */
/* Scale Factor = (VREF / (2048 * 220K)) * 10^9 nA = 2.6633 
   As the TIA produces a negative voltage, the scale factor is made 
   negative */
#define ALS_CURRENT_SCALE_FACTOR_NUMERATOR		(-26633)
#define ALS_CURRENT_SCALE_FACTOR_DENOMINATOR	(10000)

/* Constants for ambient light calculation */
/* Scale Factor = 10000Lx / 3000nA = 3.333 */
#define ALS_LIGHT_SCALE_FACTOR_NUMERATOR		(3333)
#define ALS_LIGHT_SCALE_FACTOR_DENOMINATOR		(1000)

#define PWM_DUTY_SCALE	         (1u)	
#define PWM_DUTY_OFFSET	         (25)
#define ADC_CHANNEL_ALS          (1u)	


/* Structure that holds the ALS current and the ambient light illuminance value  */
/* Use __attribute__((packed)) for GCC and MDK compilers to pack structures      */
/* For other compilers use the corresponding directive.                          */
/* For example, for IAR use the following directive                              */
/* typedef __packed struct {..}struct_name;                                      */
typedef struct __attribute__((packed))
{
	int16 alsCurrent;			/* Ambient light sensor current output */
	uint16 illuminance; 		/* Ambient light illuminance */	
}als_sensor_data;

//PIR

#define LED_ON                      (0u)
#define LED_OFF                     (1u)
#define MOTION_DETECTED             (1u)
#define MOTION_NOT_DETECTED         (0u)
#define THREE_FEET                  (3u)
#define TEN_FEET                    (10u)
#define TWENTY_FEET                 (20u)
#define ADC_CHANNEL_PIR             (0u)
#define SENSOR_RAW_INITIAL          (0)


/* High and low thresholds for the motion detection are determined 
	through experiments */ 
	
/* High Threshold for 3 feet detection (80% of positive peak count) */
#define PIR_WINDOW_HIGH_3FT         (60)
/* Low Threshold for 3 feet detection (80% of negative peak count) */
#define PIR_WINDOW_LOW_3FT          (-60)   
/* High Threshold for 10 feet detection (80% of positive peak count) */
#define PIR_WINDOW_HIGH_10FT        (30)
/* Low Threshold for 10 feet detection (80% of negative peak count) */
#define PIR_WINDOW_LOW_10FT         (-30)
/* High Threshold for 20 feet detection (80% of positive peak count) */    
#define PIR_WINDOW_HIGH_20FT        (60)
/* Low Threshold for 20 feet detection (80% of negative peak count) */   
#define PIR_WINDOW_LOW_20FT         (-60)
	
/* Structure that holds the sensor values                                        */
/* Use __attribute__((packed)) for GCC and MDK compilers to pack structures      */
/* For other compilers use the corresponding directive.                          */
/* For example, for IAR use the following directive                              */
/* typedef __packed struct {..}struct_name;                                      */
typedef struct __attribute__((packed))
{
    uint8 detectionDistance;      	/* PIR detection distance */
	int16 sensorRawValue;        	/* ADC result */
	int16 highThreshold;		    /* High threshold for motion detection */
	int16 lowThreshold;			    /* Low threshold for motion detection */
    uint8 motionDetected;		    /* Motion detection flag */
}pir_sensor_data;


/* Function Prototypes */
void InitResources(void );
CY_ISR(TIMEBASE_ISR);



int main()
{
    
    //ALS
    /* This variable is used to store the ADC result */
	int16 adcResult;
	
	/* These are used for firmware low pass filter input and output */
	int16 filterInput;
	int32 filterOutput = 0;
	
    /* Variable to store sensor current and light illuminance */
    int16 alsCurrent;
    uint16 illuminance;
    
	/* Variable to store the PWM Duty Cycle */
	unsigned int pwmDutyCycle;
    
    
    
    
    //PIR
    
	/* Sensor raw value */
	int16 sensorRawValue = 0;
	
	/* Motion detection thresholds */
	int16 highThreshold = PIR_WINDOW_HIGH_3FT;		                           
	int16 lowThreshold = PIR_WINDOW_LOW_3FT;
	
    /* Variable that stores the previous detection distance, used for checking if 
        the detection distance is changed by the master(BCP) */
    uint8 prevDetectionDistance = THREE_FEET;
    
    /* Variable to store the status returned by CyEnterCriticalSection() */
    uint8 interruptState = 0;
    
    /* Enable global interrupts */
    CyGlobalIntEnable;
    
    /* Initialize all the hardware resources */
    InitResources();
    
    /* Infinite Loop */
    for(;;)
    {        
        //ALS
        /* Check if the ADC data is ready */
		if(ADC_IsEndConversion(ADC_RETURN_STATUS))
		{			
			/* Get the ADC result */
			adcResult = ADC_GetResult16(ADC_CHANNEL_ALS);	
			
			/* Low pass filter the ADC result */
			filterInput = adcResult;
    		filterOutput = (filterInput + (FILTER_COEFFICIENT_ALS - 1)*filterOutput)/FILTER_COEFFICIENT_ALS;
    				
			/* Calculate the photodiode current */
			alsCurrent = (filterOutput * ALS_CURRENT_SCALE_FACTOR_NUMERATOR)/ALS_CURRENT_SCALE_FACTOR_DENOMINATOR; 
			
			/* If the calculated current is negative, limit it to zero */
			if(alsCurrent < 0)
			{
				alsCurrent = 0;
			}
			
			/* Calculate the light illuminance */
			illuminance = (alsCurrent * ALS_LIGHT_SCALE_FACTOR_NUMERATOR)/ALS_LIGHT_SCALE_FACTOR_DENOMINATOR;			
		
            /* Get the PWM duty cycle from the light illuminance value */
			pwmDutyCycle = ((unsigned int)illuminance*PWM_DUTY_SCALE)+PWM_DUTY_OFFSET;
	
			/* Limit the duty cycle */
			if(pwmDutyCycle>PWM_PWM_PERIOD_VALUE)
                pwmDutyCycle = PWM_PWM_PERIOD_VALUE;
			   
            
            
			
			/* Update the PWM duty cycle */
			PWM_WriteCompare(PWM_PWM_PERIOD_VALUE-pwmDutyCycle);	
            
			if(pwmDutyCycle < 600){ //Low and medium Ligth
            Led_Write(0);
            }else{//High Ligth
            Led_Write(1);
            }
		}
        
        //PIR
        /* If the master(BCP) changed the detection distance, change the second stage 
            amplifier (PGA) gain and the thresholds for the required detection distance*/
               
        /* Check if ADC data is ready */
        if(ADC_IsEndConversion(ADC_RETURN_STATUS))
        {
            /* Read ADC result */
            sensorRawValue = ADC_GetResult16(ADC_CHANNEL_PIR);
            
            /* Check if motion is detected */
            if((sensorRawValue > highThreshold) || 
               (sensorRawValue < lowThreshold))
            {
                               	
				/* Stop the timer */
                Timebase5s_Stop();
				
				/* Reload the counter */
				Timebase5s_WriteCounter(Timebase5s_TC_PERIOD_VALUE);
			
				/* Start the 5s timer */
                Timebase5s_Start();
				
				
				
               // Pin_LED_Write(LED_ON);
                m1_Write(1); //Move motor
                m2_Write(0);
            }
        } 
				
		/* Exit critical section */
        CyExitCriticalSection(interruptState);		
    }    
}


void InitResources(void)
{  
    //ALS
    
    /* Start the trans-impedance amplifier (TIA) */
	Opamp_TIA_Start();
    	
	/* Start PWM */
	PWM_Start();
    
    //PIR
    /* Enable LED timebase ISR */
    isr_Timebase5s_Start();
    isr_Timebase5s_StartEx(TIMEBASE_ISR);   
    
    /* Start the Scanning SAR ADC Component and start conversion */
    ADC_Start();
    ADC_StartConvert();
    
    /* Start the Reference Buffer */
    RefBuffer_Start();
    
    /* Start Programmable Voltage Reference */
    PVref_Start();
    
    /* Enable Programmable Voltage Reference */
    PVref_Enable();
    
    /* Start the first stage amplifier */
    PIRAmplifierStage1_Start();
    
    /* Start the second stage amplifier (PGA) */
    PIRAmplifierStage2_Start();    
    
 }


CY_ISR(TIMEBASE_ISR)
{
        
    m1_Write(0); //Move motor
    m2_Write(1);
    /* Stop the 5s timer */
    Timebase5s_Stop();
}

Credits

Yeferson yate monrroy

Yeferson yate monrroy

1 project • 0 followers

Comments