Skip to content

Commit

Permalink
Merge branch 'jorge-dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
jlamperez committed May 30, 2016
2 parents 9c1e064 + a1bd0ed commit 465c476
Show file tree
Hide file tree
Showing 8 changed files with 831 additions and 5 deletions.
4 changes: 3 additions & 1 deletion RPI_SmartServo/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CC = gcc
CFLAGS =-Wall -lwiringPi -lrt -lpthread
RM := rm -rf

OBJECTS = pwm.o pid.o adc.o registers.o main.o ads1115.o i2c.o timer.o service.o server.o rpiservo.o
OUT = SmartServo
Expand Down Expand Up @@ -43,4 +44,5 @@ main.o: main.c
$(CC) $(CFLAGS) -c $< -o $@

clean:
rm *o hello
-$(RM) *.o all
.PHONY: all clean dependents
13 changes: 9 additions & 4 deletions RPI_SmartServo/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@
/**
* TIMER configuration.
*/
#define CONFIG_TIMER 100000000 //100ms
//#define CONFIG_TIMER 100000000 //100ms
//#define CONFIG_TIMER 10000000 //10ms

#define CONFIG_TIMER 50000000 //50ms Good
//#define CONFIG_TIMER 30000000 //30ms Falla
//#define CONFIG_TIMER 40000000 //40ms Falla
//#define CONFIG_TIMER 45000000 //45ms Falla
/**
* Debugger configuration.
*/
Expand Down Expand Up @@ -155,8 +158,10 @@
#define CONFIG_PID_MIN_OUTPUT (-CONFIG_PID_MAX_OUTPUT)
// Default PID values
#define CONFIG_DEFAULT_PID_DEADBAND 0x01
#define CONFIG_DEFAULT_PID_PGAIN 0x0028
#define CONFIG_DEFAULT_PID_DGAIN 0x0023
#define CONFIG_DEFAULT_PID_PGAIN 0x0015
#define CONFIG_DEFAULT_PID_DGAIN 0x0000
//#define CONFIG_DEFAULT_PID_PGAIN 0x0028
//#define CONFIG_DEFAULT_PID_DGAIN 0x0023
#define CONFIG_DEFAULT_PID_IGAIN 0x0000
// Max and min seek values
#define CONFIG_DEFAULT_MIN_SEEK 0x0001
Expand Down
24 changes: 24 additions & 0 deletions Timer/src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CC = gcc
CFLAGS =-Wall -lwiringPi -lrt -lpthread

OBJECTS = ads1115.o i2c.o Timer.o
OUT = TimerADS1115


#all: target -> This is the default target for makefiles.
all : $(OBJECTS)
$(CC) $(CFLAGS) -o $(OUT) $(OBJECTS)


ads1115.o: ads1115.c
$(CC) $(CFLAGS) -c $< -o $@

i2c.o: i2c.c
$(CC) $(CFLAGS) -c $< -o $@

Timer.o: Timer.c
$(CC) $(CFLAGS) -c $< -o $@


clean:
rm *o hello
167 changes: 167 additions & 0 deletions Timer/src/Timer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
============================================================================
Name : Timer.c
Author : Jorge
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/

#include <time.h>
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include "ads1115.h"

#define CLOCKID CLOCK_REALTIME
#define SIG SIGRTMIN

#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} while (0)


sigset_t mask;

void ADC_handler(int sig, siginfo_t *si, void *uc) {
static int cont=0;
// POSITION (Potenciometer)
uint16_t pos = ADS1115_readADC_singleEnded(POSITION_CHANNEL);
uint16_t temp = ADS1115_readADC_singleEnded(TEMP_CHANNEL);
uint16_t battery = ADS1115_readADC_singleEnded(BATTERY_CHANNEL);
uint16_t current = ADS1115_readADC_singleEnded(CURRENT_CHANNEL);

printf("ADS1115 pos: %d, temp: %d, battery: %d, current: %d \n", pos, temp, battery, current);


printf("handler %i \n", cont);
// printf("Time: %d seconds\n", (double)clock());
cont++;
}

/**
*
* Create the timer.
*
* @param freq_nanosecs nanoseconds value.
*
* @return None.
*
* @note None.
*
*/
void TIMER_create(long long freq_nanosecs)
{
timer_t timerid;

struct sigevent sev;
struct itimerspec its;

/* Create the timer */

sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIG;
sev.sigev_value.sival_ptr = &timerid;

if (timer_create(CLOCKID, &sev, &timerid) == -1)
errExit("timer_create");
#ifdef CONFIG_DEBUGGER
printf("timer ID is 0x%lx\n", (long) timerid);
#endif
its.it_value.tv_sec = freq_nanosecs / 1000000000;
its.it_value.tv_nsec = freq_nanosecs % 1000000000;
its.it_interval.tv_sec = its.it_value.tv_sec;
its.it_interval.tv_nsec = its.it_value.tv_nsec;

if (timer_settime(timerid, 0, &its, NULL) == -1)
errExit("timer_settime");
}

/**
*
* Timer signal handler.
*
* @return None.
*
* @note None.
*
*/
void TIMER_signalHandler()
{
struct sigaction sa;

/* Establish handler for timer signal */
#ifdef CONFIG_DEBUGGER
printf("Establishing handler for signal %d\n", SIG);
#endif
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = ADC_handler;
sigemptyset(&sa.sa_mask);

if (sigaction(SIG, &sa, NULL) == -1)
errExit("sigaction");
}
/**
*
* Timer signal block.
*
* @return None.
*
* @note None.
*
*/
void TIMER_signalBlock()
{
/* Block timer signal temporarily */
#ifdef CONFIG_DEBUGGER
printf("Blocking signal %d\n", SIG);
#endif
sigemptyset(&mask);
sigaddset(&mask, SIG);

if (sigprocmask(SIG_SETMASK, &mask, NULL) == -1)
errExit("sigprocmask");
}
/**
*
* Timer signal unblock.
*
* @return None.
*
* @note None.
*
*/
void TIMER_signalUnblock()
{
/* Unlock the timer signal, so that timer notification
can be delivered */
#ifdef CONFIG_DEBUGGER
printf("Unblocking signal %d\n", SIG);
#endif
if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1)
errExit("sigprocmask");
}



int main(void) {

long long freq_nanosecs= 10000000;//10ms
// long long freq_nanosecs= 100000000;//100ms
// long long freq_nanosecs= 1000000000;//1s

ADS1115_init(ADS1115_ADDRESS);
//Establish handler for timer signal.
TIMER_signalHandler();
//Block timer signal temporarily
TIMER_signalBlock(&mask);
//Timer Create
TIMER_create(freq_nanosecs);
//Unlock the timer signal
TIMER_signalUnblock(&mask);

while(1){

}
}
Loading

0 comments on commit 465c476

Please sign in to comment.