Commit d7f46bf0 authored by Arnuad Blanchard's avatar Arnuad Blanchard

Connection with jupyter notebook

parent 10a5e241
......@@ -16,10 +16,12 @@ project(raspinobo)
find_package(blc_channel REQUIRED)
find_package(blc_program REQUIRED)
find_library(BCM_HOST bcm_host PATHS /opt/vc/lib/ )
add_definitions(${BL_DEFINITIONS} -std=c++14 -Wall -W -fdiagnostics-color=always -march=native)
include_directories(${BL_INCLUDE_DIRS})
include_directories(${BL_INCLUDE_DIRS} /opt/vc/include)
add_executable(raspinobo raspinobo.cpp bl_raspi.cpp bl_raspi_core.cpp)
target_link_libraries(raspinobo ${BL_LIBRARIES})
target_link_libraries(raspinobo ${BL_LIBRARIES} ${BCM_HOST})
if (NOT APPLE)
#Give the raw capability to the target /dev/mem
......
Usefull tools
=============
Check temperature : `vcgencmd measure_temp`
Check the frequency: `sudo cat /sys/devices/system/cpu/cpufreq/policy0/cpuinfo_cur_freq`
Python 3.7
==========
```
sudo apt-get install build-essential libc6-dev
sudo apt-get install libncurses5-dev libncursesw5-dev libreadline6-dev
sudo apt-get install libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev
sudo apt-get install libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
tar -zxvf Python-3.7.0.tgz
cd Python-3.7.0
./configure # 3 min 13 s
# Let's use 4 threads
make -j4 # 8 min 29 s
sudo make install # ~ 4 min
cd ..
sudo rm -fr ./Python-3.7.0*
# upgrade:
sudo pip3 install -U pip
sudo pip3 install -U setuptools
```
Setup the raspberry has an access point
=======================================
More details here in the original tuatorial ( https://github.com/SurferTim/documentation/blob/6bc583965254fa292a470990c40b145f553f6b34/configuration/wireless/access-point.md )
Check the frequency:
`sudo cat /sys/devices/system/cpu/cpufreq/policy0/cpuinfo_cur_freq`
Check temperature:
`/opt/vc/bin/vcgencmd measure_temp`
Video tracking
==============
......
......@@ -19,15 +19,11 @@
static const int DEBUG_BUFFER_SIZE=256;
#define DEBUG_VALUE(value) do{debug_channel.floats[debug_index++]=value; if (debug_index==DEBUG_BUFFER_SIZE) debug_index=0;}while(0)
#define FLAG(pos) (1<<pos)
#define DELTA_NS(current_ns, previous_ns) ((previous_ns > current_ns) ? 1000000000-previous_ns+current_ns : current_ns-previous_ns)
using namespace std;
uint32_t it_since_clock=0;
static uint32_t it_since_clock=0;
uint32_t current_ns, previous_ns;
float ns_per_it=1, previous_ns_per_it, var_ns_per_it;
uint32_t min_ns_per_it=1000;
......@@ -45,13 +41,14 @@ inline void update_time(){
struct timespec timestamp;
// current_ns_per_it;
float current_ns_per_it, error;
int32_t delta_ns;
uint32_t delta_ns;
clock_gettime(CLOCK_MONOTONIC, &timestamp);
clock_gettime(CLOCK_MONOTONIC_COARSE, &timestamp);
current_ns=timestamp.tv_nsec;
delta_ns=DELTA_NS(current_ns, previous_ns);
// DEBUG_VALUE(current_ns);
if (it_since_clock){
current_ns_per_it=(delta_ns+1)/it_since_clock;
// if (current_ns_per_it/ns_per_it)
......@@ -66,19 +63,10 @@ inline void update_time(){
previous_ns=current_ns;
DEBUG_VALUE(ns_per_it);
// DEBUG_VALUE(error*error);
// DEBUG_VALUE(var_ns_per_it);
DEBUG_VALUE(ns_per_it);
for( auto &input : ohm_inputs){
// DEBUG_VALUE(input.activated);
if (input.activated){
delta_ns=DELTA_NS(current_ns, input.spike_ns);
// DEBUG_VALUE(delta_ns);
// DEBUG_VALUE(input.tau);
if (delta_ns >= input.tau) input.activated=1;
else input.activated=(input.tau-delta_ns)/ns_per_it+1;
}
......@@ -86,40 +74,41 @@ inline void update_time(){
}
Ohm_input::Ohm_input(int pin, int us_tau, uint32_t *buffer):index(0), flag(FLAG(pin)), pin(pin), buffer(buffer), activated(us_tau*1000){
tau=300000;
tau=3*1000*us_tau;
}
inline void Ohm_input::rest(){
raspi.set_pin_dir(pin, raspi.INPUT);
update_time();
buffer[index]=current_ns;
//update_time();
/* buffer[index]=current_ns;
index++;
if (index==buffer_size) index=0;
if (index==buffer_size) index=0;*/
rest_ns=current_ns;
activated=0;
}
inline void Ohm_input::spike(){
raspi.set_pin_dir(pin, raspi.OUTPUT);
update_time();
// update_time();
spike_ns=current_ns;
buffer[index]=DELTA_NS(current_ns, rest_ns);
index++;
if (index==buffer_size) index=0;
activated=tau/ns_per_it;;
buffer[0]=DELTA_NS(current_ns, rest_ns);
/*index++;
if (index==buffer_size) index=0;*/
// activated=tau/ns_per_it;
activated=1;
}
Volt_output::Volt_output(int pin):index(0), flag(FLAG(pin)), pin(pin), buffer(buffer){
}
Volt_output::Volt_output(int pin, uint8_t *value_ptr):index(0), flag(FLAG(pin)), pin(pin), buffer(buffer), value_ptr(value_ptr){
}
void bl_raspi_add_RC_input(int pin, int us_tau, uint32_t *buffer){
ohm_inputs.emplace_back(pin, us_tau, buffer);
}
void bl_raspi_add_voltage_output(int pin, char const *filename, char const* filename_input){
volt_outputs.emplace_back(pin);
void bl_raspi_add_voltage_output(int pin, uint8_t *value_ptr, char const *filename, char const* filename_input){
volt_outputs.emplace_back(pin, value_ptr);
}
void *thread_loop(void*){
......@@ -129,7 +118,6 @@ void *thread_loop(void*){
uint32_t result;
int i;
uint32_t initial_ns, delta_ns;
int32_t previous_ns, current_ns;
int32_t var_ns_per_iteration;
int32_t error;
......@@ -142,6 +130,7 @@ void *thread_loop(void*){
*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.activated=1;
}
for(auto &output : volt_outputs){
*raspi.gpfset0|=output.flag; //Set pin high
......@@ -149,15 +138,15 @@ void *thread_loop(void*){
raspi.set_pin_dir(output.pin, raspi.INPUT);
}
uint32_t results;
while(run_loop){
// results =*(raspi.gplev0);
clock_gettime(CLOCK_MONOTONIC, &timestamp);
current_ns=timestamp.tv_nsec;
for(auto &input : ohm_inputs){
if (input.activated) {
input.activated--;
if (input.activated==0){
input.rest();
}
if (input.activated){
delta_ns=DELTA_NS(current_ns, input.spike_ns);
if (delta_ns >= input.tau) input.rest();
}
else {
if ((*(raspi.gplev0) & input.flag)==0){
......@@ -165,9 +154,19 @@ void *thread_loop(void*){
}
}
}
// if (bl_raspi_iterations_nb==0){
for(auto &output : volt_outputs){
if (*output.value_ptr > rand()/(RAND_MAX/255)){
raspi.set_pin_dir(output.pin, raspi.OUTPUT);
}
else{
raspi.set_pin_dir(output.pin, raspi.INPUT);
}
}
//}
//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;
......@@ -196,8 +195,8 @@ void *thread_loop(void*){
}
}*/
bl_raspi_iterations_nb++;
it_since_clock++;
if (it_since_clock == 32) update_time();
//it_since_clock++;
//if (it_since_clock == 32) update_time();
}
//Stop voltages before living for security
for(auto &output : volt_outputs) raspi.set_pin_dir(output.pin, raspi.INPUT);
......@@ -207,23 +206,23 @@ void *thread_loop(void*){
void bl_raspi_start_loop(uint32_t *RC_results, uchar const *voltage_commands){
pthread_t thread;
pthread_attr_t attr={0};
pthread_attr_t attr={{(0)}};
struct sched_param sched_param;
run_loop=1;
debug_channel.create_or_open("/raspinobo.debug", BLC_CHANNEL_WRITE, 'FL32', 'NDEF', 1, DEBUG_BUFFER_SIZE);
/* if (RC_results==NULL) bl_raspi_RC_inputs=MANY_ALLOCATIONS(ohm_inputs.size(), 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;*/
/* BLC_PTHREAD_CHECK(pthread_attr_init(&attr), "Init pthread attribute");
/*BLC_PTHREAD_CHECK(pthread_attr_init(&attr), "Init pthread attribute");
BLC_PTHREAD_CHECK(pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED), NULL);
BLC_PTHREAD_CHECK(pthread_attr_setschedpolicy(&attr, SCHED_FIFO), NULL);
BLC_PTHREAD_CHECK(pthread_attr_setschedpolicy(&attr, SCHED_RR), NULL);
sched_param.sched_priority=18;
BLC_PTHREAD_CHECK(pthread_attr_setschedparam(&attr, &sched_param), NULL);*///
BLC_PTHREAD_CHECK(pthread_attr_setschedparam(&attr, &sched_param), NULL);*/
BLC_PTHREAD_CHECK(pthread_create(&thread, &attr, thread_loop, NULL), "Creating bl_raspi thread");
}
......
......@@ -32,8 +32,8 @@ typedef struct Volt_output
uint8_t activated_input;
int index;
uint32_t *buffer;
Volt_output(int pin);
uint8_t *value_ptr;
Volt_output(int pin, uint8_t *valure_ptr);
}Volt_output;
......@@ -50,7 +50,7 @@ extern int buffer_size;
extern volatile int bl_raspi_iterations_nb;
void bl_raspi_add_RC_input(int pin, int us_tau, uint32_t *buffer);
void bl_raspi_add_voltage_output(int pin, char const *filename, char const* filename_input);
void bl_raspi_add_voltage_output(int pin, uint8_t *value_ptr, char const *filename, char const* filename_input);
void bl_raspi_start_loop(uint32_t *RC_results, unsigned char const* voltage_commands);
void bl_raspi_stop_loop();
......
#include "bl_raspi_core.h"
#include "blc_core.h"
//#include "mmapGpio.h"
#include <sys/ioctl.h> //ioctl
#include <sys/mman.h>
#include <fcntl.h> //O_RDWR, ...
......@@ -12,11 +11,14 @@
#include <time.h>
#include <math.h>
#include <bcm_host.h>
#define GPIO_MAX 32
#define FLAG(pos) (1<<pos)
using namespace std;
static unsigned peripheral_address;
volatile uint32_t *map_memory(uint32_t base_addr, size_t size){
int mem_fd;
......@@ -41,14 +43,15 @@ volatile uint32_t *map_memory(uint32_t base_addr, size_t size){
return mapped_addr;
}
bl_raspi::bl_raspi(){
peripheral_address= bcm_host_get_peripheral_address();
fprintf(stderr, "%x\n", peripheral_address);
//maps timers
system_timers_map=map_memory(system_timers_base_addr, 0x1C);
system_timers_map=map_memory(peripheral_address + system_timers_addr_offset, 0x1C);
ptime_us=system_timers_map+1; //+1 is 4bytes because ptimes is 32(4*8)bits step
//map gpio
gpio_map=map_memory(gpio_base_addr, 0xB4);
gpio_map=map_memory(peripheral_address+gpio_addr_offset, 0xB4);
gpfsels=gpio_map;
gpfset0=gpio_map+7; // 1C/4 because 32(4*8)bits step
gpclr0=gpio_map+10;
......
......@@ -4,8 +4,8 @@
#include <stdint.h>
typedef struct bl_raspi{
static const unsigned int system_timers_base_addr = 0x3F003000;
static const unsigned int gpio_base_addr = 0x3F200000;// gpio registers base address for raspberry 2 et 3 0x20200000 sinon
static const unsigned int system_timers_addr_offset = 0x3000;
static const unsigned int gpio_addr_offset = 0x200000;// gpio registers base address for raspberry 2 et 3 0x20200000 sinon
//Three pulling resistor (~50KOhm modes in input direction
static const unsigned int PULL_OFF = 0;
......
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Raspinobo\n",
"=========\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "__call__() takes from 1 to 2 positional arguments but 3 were given",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-2-4511475ed8c0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;31m#fig = go.FigureWidget(data=go.Bar(y=[2, 3, 1]))\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 20\u001b[0m \u001b[0;31m#fig\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 21\u001b[0;31m \u001b[0minteract\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdrive\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwidgets\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mIntSlider\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmin\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m255\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m255\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstep\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 22\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mTypeError\u001b[0m: __call__() takes from 1 to 2 positional arguments but 3 were given"
]
}
],
"source": [
"from ipywidgets import interact, interactive, fixed, interact_manual\n",
"import plotly.graph_objects as go\n",
"\n",
"import ipywidgets as widgets\n",
"import blpy_channel\n",
"import os\n",
"\n",
"def drive(x):\n",
" if (x > 0):\n",
" motors.array[0]=x\n",
" motors.array[1]=0\n",
" else:\n",
" motors.array[0]=0\n",
" motors.array[1]=-x\n",
" \n",
"motors = blpy_channel.BlcChannel(\"/pinobo.motors\", os.O_RDWR)\n",
"sensors = blpy_channel.BlcChannel(\"/pinobo.sensors\", os.O_RDONLY)\n",
"\n",
"#fig = go.FigureWidget(data=go.Bar(y=[2, 3, 1]))\n",
"#fig\n",
"interact(drive, widgets.IntSlider(min=-255, max=255, step=1, value=0))\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
......@@ -17,7 +17,6 @@
static const int SOUND_PIN=13;
static const int LEFT_POT_PIN=6;
static const int RIGHT_POT_PIN=12;
static const int LEFT_MOTOR_PIN=26;
......@@ -29,10 +28,10 @@ float gaussian(float mu, float var){
}
static uchar gaussian_cdf(uint32_t s, float mu, float var){
return (1+erf((s-mu)/sqrt(2*var)))*255/2; //var+1 is to be sure it is not null
return static_cast<uchar>((1+erf((s-mu)/sqrt(2*var)))*255/2); //var+1 is to be sure it is not null
}
blc_channel motor_channel, sensor_channel, neurons_channel;
static blc_channel motor_channel, sensor_channel, neurons_channel;
int main(int argc, char **argv){
blc_channel gain_channel, target_channel, buffer_channel;
......@@ -68,11 +67,10 @@ int main(int argc, char **argv){
buffer_channel.create_or_open("/buffer", BLC_CHANNEL_WRITE, 'UI32', 'NDEF', 2, 2, buffer_size/2);
bl_raspi_add_RC_input(SOUND_PIN, 1, buffer_channel.uints32);//"left_pot_file.bin")
/*
bl_raspi_add_RC_input(LEFT_POT_PIN, 333, NULL);//"left_pot_file.bin");
bl_raspi_add_RC_input(RIGHT_POT_PIN, 333, NULL); //"right_pot_file.bin");
bl_raspi_add_voltage_output(LEFT_MOTOR_PIN, NULL, NULL);
bl_raspi_add_voltage_output(RIGHT_MOTOR_PIN, NULL, NULL);*/
blc_channel buffer_left_channel("/left_buffer", BLC_CHANNEL_WRITE, 'UI32', 'NDEF', 1, buffer_size);
blc_channel buffer_right_channel("/right_buffer", BLC_CHANNEL_WRITE, 'UI32', 'NDEF', 1, buffer_size);
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, ohm_inputs.size());
......@@ -86,6 +84,11 @@ int main(int argc, char **argv){
motor_channel.uchars[0]=0;
motor_channel.uchars[1]=0;
bl_raspi_add_RC_input(LEFT_POT_PIN, 300, &sensor_channel.uints32[0]);//"left_pot_file.bin");
bl_raspi_add_RC_input(RIGHT_POT_PIN, 300, &sensor_channel.uints32[1]); //"right_pot_file.bin");
bl_raspi_add_voltage_output(LEFT_MOTOR_PIN, &motor_channel.uchars[0], NULL, NULL);
bl_raspi_add_voltage_output(RIGHT_MOTOR_PIN, &motor_channel.uchars[1], NULL, NULL);
bl_raspi_start_loop(sensor_channel.uints32, motor_channel.uchars);
previous_iterations_nb=0;
......@@ -115,8 +118,6 @@ int main(int argc, char **argv){
uchar left_motor;
uchar right_motor;
}line;
//SYSTEM_ERROR_CHECK(record_file=fopen("pinobo.data", "w"), NULL, "Opening 'pinobo.data'");
struct timespec timestamp;
uint32_t previous_ns, delay_ns;
......@@ -129,10 +130,8 @@ int main(int argc, char **argv){
if (timestamp.tv_nsec > previous_ns) delay_ns = timestamp.tv_nsec-previous_ns;
else delay_ns = 1000000000 - previous_ns + timestamp.tv_nsec;
previous_ns=timestamp.tv_nsec;
fprintf(stderr, "Freq:%.4fMHz\n n0:%d n0mu:%f n0var:%f", (float)(bl_raspi_iterations_nb-previous_iterations_nb)/((float)(delay_ns/1000)), neurons_channel.uchars[2], neurons[0].mu, neurons[0].var);
fprintf(stderr, "Freq:%.4fMHz\n n0:%d n0mu:%f n0var:%f", (float)(bl_raspi_iterations_nb-previous_iterations_nb)/((float)(delay_ns/1000)), neurons_channel.uchars[2], neurons[0].mu, neurons[0].var);
previous_iterations_nb=bl_raspi_iterations_nb;
}
bl_raspi_stop_loop();
......
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