Commit 2a3eccd4 authored by Arnaud Blanchard's avatar Arnaud Blanchard

C++ form

parent 6e124a1b
......@@ -12,38 +12,12 @@
#include <time.h>
#include <math.h>
#include <vector>
#define GPIO_MAX 32
#define FLAG(pos) (1<<pos)
typedef struct Ohm_input
{
uint32_t flag;
uint32_t tau;
uint32_t rest_ns, spike_ns;
uint8_t pin;
uint32_t activated;
uint32_t *buffer;
uint32_t previous_iteration;
int32_t index;
int64_t stop_ns;
ohm_input_type();
inline void rest();
inline void spike();
}Ohm_input;
typedef struct
{
uint32_t us_timestamps, us_timestamps_input;
uint32_t flag;
uint8_t pin;
uint8_t activated;
float average;
FILE *file;
uint8_t activated_input;
FILE *file_input;
}volt_output_type;
using namespace std;
typedef struct bl_raspi{
......@@ -72,19 +46,20 @@ typedef struct bl_raspi{
}bl_raspi;
static const int CHUNK_ITERATIONS = 128;
uint32_t it_since_clock=0;
uint32_t current_ns, previous_ns;
float ns_per_it=1;
bl_raspi raspi;
Ohm_input *ohm_inputs=nullptr;
vector <Ohm_input> ohm_inputs;
vector <Volt_output> volt_outputs;
int run_loop=1;
volatile int bl_raspi_iterations_nb;
int buffer_size;
inline void update_time(){
struct timespec timestamp;
int32_t current_ns_per_it;
int32_t delta_ns;
int i;
clock_gettime(CLOCK_MONOTONIC, &timestamp);
current_ns=timestamp.tv_nsec;
......@@ -93,19 +68,18 @@ inline void update_time(){
previous_ns=current_ns;
it_since_clock=0;
FOR( i, bl_raspi_RC_inputs_nb){
if (ohm_inputs[i].activated){
if (ohm_inputs[i].stop_ns - current_ns> 0) ohm_inputs[i].activated = (float)(ohm_inputs[i].stop_ns - current_ns)/ns_per_it;
else ohm_inputs[i].activated=1;
for( auto &input : ohm_inputs){
if (input.activated){
if (input.stop_ns - current_ns> 0) input.activated = (float)(input.stop_ns - current_ns)/ns_per_it;
else input.activated=1;
}
}
}
ohm_input_type::ohm_input_type():index(0){
Ohm_input::Ohm_input(int pin, int us_tau, uint32_t *buffer):index(0), flag(FLAG(pin)), pin(pin), buffer(buffer), activated(1){
}
inline void ohm_input_type::rest(){
inline void Ohm_input::rest(){
raspi.set_pin_dir(pin, raspi.INPUT);
update_time();
buffer[index]=rest_ns-spike_ns;
......@@ -115,7 +89,7 @@ inline void ohm_input_type::rest(){
// fprintf(stderr, "%lld %f\n", current_ns-spike_ns, ns_per_it);
}
inline void ohm_input_type::spike(){
inline void Ohm_input::spike(){
raspi.set_pin_dir(pin, raspi.OUTPUT);
tau=10000;
update_time();
......@@ -128,6 +102,10 @@ inline void ohm_input_type::spike(){
}
Volt_output::Volt_output(int pin):index(0), flag(FLAG(pin)), pin(pin), buffer(buffer){
}
volatile uint32_t *map_memory(uint32_t base_addr, size_t size){
int mem_fd;
volatile uint32_t *mapped_addr;
......@@ -193,20 +171,11 @@ void bl_raspi::set_pin_dir(int pinnum, int dir){
}
void bl_raspi_add_RC_input(int pin, int us_tau, uint32_t *buffer){
ohm_input_type input;
input.flag=FLAG(pin);
input.tau=us_tau;
input.pin=pin;
input.activated=1;
input.buffer=buffer;
APPEND_ITEM(&ohm_inputs, &bl_raspi_RC_inputs_nb, &input);
ohm_inputs.emplace_back(pin, us_tau, buffer);
}
void bl_raspi_add_voltage_output(int pin, char const *filename, char const* filename_input){
volt_output_type output;
output.flag=FLAG(pin);
output.pin=pin;
APPEND_ITEM(&volt_outputs, &bl_raspi_volt_outputs_nb, &output);
volt_outputs.emplace_back(pin);
}
void *thread_loop(void*){
......@@ -225,40 +194,35 @@ void *thread_loop(void*){
// initial_us=3000000*(int)(*raspi.ptime_us/3000000)-3000000;
update_time();
FOR(i, bl_raspi_RC_inputs_nb){
*raspi.gpfset0|=ohm_inputs[i].flag; //Set pin high
raspi.set_pin_mode(ohm_inputs[i].pin, raspi.PULL_OFF);
raspi.set_pin_dir(ohm_inputs[i].pin, raspi.OUTPUT);
ohm_inputs[i].activated=1;
ohm_inputs[i].stop_ns=current_ns+ohm_inputs[i].tau*1000;
ohm_inputs[i].spike_ns=current_ns;
ohm_inputs[i].rest_ns=current_ns;
for(auto &input : ohm_inputs){
*raspi.gpfset0|=input.flag; //Set pin high
raspi.set_pin_mode(input.pin, raspi.PULL_OFF);
raspi.set_pin_dir(input.pin, raspi.OUTPUT);
input.stop_ns=current_ns+input.tau*1000;
}
FOR(i, bl_raspi_volt_outputs_nb) {
*raspi.gpfset0|=volt_outputs[i].flag; //Set pin high
raspi.set_pin_mode(volt_outputs[i].pin, raspi.PULL_OFF);
raspi.set_pin_dir(volt_outputs[i].pin, raspi.INPUT);
volt_outputs[i].activated=0;
for(auto &output : volt_outputs){
*raspi.gpfset0|=output.flag; //Set pin high
raspi.set_pin_mode(output.pin, raspi.PULL_OFF);
raspi.set_pin_dir(output.pin, raspi.INPUT);
}
while(run_loop){
FOR(i, bl_raspi_RC_inputs_nb){
if (ohm_inputs[i].activated) {
ohm_inputs[i].activated--;
if (ohm_inputs[i].activated==0){
ohm_inputs[i].rest();
for(auto &input : ohm_inputs){
if (input.activated) {
input.activated--;
if (input.activated==0){
input.rest();
}
}
else {
if ((*(raspi.gplev0) & ohm_inputs[i].flag)==0){
ohm_inputs[i].spike();
if ((*(raspi.gplev0) & input.flag)==0){
input.spike();
}
}
}
//Output volts
FOR (i, bl_raspi_volt_outputs_nb){
// FOR (i, bl_raspi_volt_outputs_nb){
/*COmputing current ( not yet working)
if (volt_outputs[0].activated_input==0 && ((result & volt_outputs[0].flag)==0)){
gpio_channel.floats[6]=current_ns-volt_outputs[0].ns_timestamps_input;
......@@ -268,7 +232,7 @@ void *thread_loop(void*){
}
volt_outputs[0].activated_input=1;
}*/
delta_ns=current_ns-volt_outputs[i].us_timestamps;
/* delta_ns=current_ns-volt_outputs[i].us_timestamps;
if (delta_ns > 10){
volt_outputs[i].average+=(volt_outputs[i].activated-volt_outputs[i].average)*(delta_ns/100);
if (bl_raspi_volt_outputs[i] > rand()/(RAND_MAX/255)){// volt_outputs[i].average*100){ //Comparing average time up with the requested one//){
......@@ -285,14 +249,14 @@ void *thread_loop(void*){
}
volt_outputs[i].us_timestamps=current_ns;
}
}
}*/
bl_raspi_iterations_nb++;
it_since_clock++;
if (it_since_clock == (1<<16)) update_time();
}
//Stop voltages before living for security
FOR(i, bl_raspi_volt_outputs_nb) raspi.set_pin_dir(volt_outputs[i].pin, raspi.INPUT);
FOR(i, bl_raspi_RC_inputs_nb) raspi.set_pin_dir(ohm_inputs[i].pin, raspi.INPUT);
for(auto &output : volt_outputs) raspi.set_pin_dir(output.pin, raspi.INPUT);
for(auto &input : ohm_inputs) raspi.set_pin_dir(input.pin, raspi.INPUT);
return NULL;
}
......@@ -302,10 +266,10 @@ void bl_raspi_start_loop(uint32_t *RC_results, uchar const *voltage_commands){
struct sched_param sched_param;
run_loop=1;
if (RC_results==NULL) bl_raspi_RC_inputs=MANY_ALLOCATIONS(bl_raspi_RC_inputs_nb, uint32_t);
/* if (RC_results==NULL) bl_raspi_RC_inputs=MANY_ALLOCATIONS(ohm_inputs.size(), uint32_t);
else bl_raspi_RC_inputs=RC_results;
if (voltage_commands==NULL) bl_raspi_volt_outputs=MANY_ALLOCATIONS(bl_raspi_volt_outputs_nb, uchar);
else bl_raspi_volt_outputs=voltage_commands;
else bl_raspi_volt_outputs=voltage_commands;*/
/* BLC_PTHREAD_CHECK(pthread_attr_init(&attr), "Init pthread attribute");
BLC_PTHREAD_CHECK(pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED), NULL);
......
......@@ -2,11 +2,50 @@
#define BL_RASPI_H
#include <stdint.h>
#include <vector>
extern uint32_t *bl_raspi_RC_inputs;
extern unsigned char const *bl_raspi_volt_outputs;
extern int bl_raspi_RC_inputs_nb;
extern int bl_raspi_volt_outputs_nb;
typedef struct Ohm_input
{
uint32_t flag;
uint32_t tau;
uint32_t rest_ns, spike_ns;
uint8_t pin;
uint32_t activated;
uint32_t *buffer;
uint32_t previous_iteration;
int32_t index;
int64_t stop_ns;
Ohm_input(int pin, int us_tau, uint32_t *buffer);
inline void rest();
inline void spike();
}Ohm_input;
typedef struct Volt_output
{
uint32_t us_timestamps, us_timestamps_input;
uint32_t flag;
uint8_t pin;
uint8_t activated;
float average;
FILE *file;
uint8_t activated_input;
FILE *file_input;
int index;
uint32_t *buffer;
Volt_output(int pin);
}Volt_output;
extern std::vector <Ohm_input> ohm_inputs;
extern std::vector <Volt_output> volt_outputs;
//extern uint32_t *bl_raspi_RC_inputs;
//extern unsigned char const *bl_raspi_volt_outputs;
//extern int bl_raspi_RC_inputs_nb;
//extern int bl_raspi_volt_outputs_nb;
extern int buffer_size;
/** Number of iterations of the loop. Useful to check the rate of the refreshing loop */
......
......@@ -138,8 +138,6 @@ int main(int argc, char **argv){
previous_p1=neurons.uchars[1];
neurons.uchars[0]=d.update(delta);
neurons.uchars[1]=p1.update(left_pos);
neurons.uchars[2]=p2.update(right_pos);
......
......@@ -77,7 +77,7 @@ int main(int argc, char **argv){
bl_raspi_add_voltage_output(RIGHT_MOTOR_PIN, NULL, NULL);*/
motor_channel.create_or_open(volts_channel_name, BLC_CHANNEL_WRITE, 'UIN8', 'NDEF', 1, 2);
sensor_channel.create_or_open(gpio_channel_name, BLC_CHANNEL_WRITE, 'UI32', 'NDEF', 1, bl_raspi_RC_inputs_nb);
sensor_channel.create_or_open(gpio_channel_name, BLC_CHANNEL_WRITE, 'UI32', 'NDEF', 1, ohm_inputs.size());
sensor_channel.publish();
neurons_channel.create_or_open("/pinobo.neurons", BLC_CHANNEL_WRITE, 'UIN8', 'NDEF', 1, 3);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment