Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partage de code entre Arduino et Processing #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
ifeq ($(OS),Windows_NT)
LIBRARY_EXT=dll
JAVA_INCLUDE=...
PLATFORM=win32
else
LIBRARY_EXT=so
JAVA_INCLUDE=/usr/lib/jvm/java-8-openjdk/include
PLATFORM=linux
endif

SWIG=swig
CPP=g++
COPTS=-fpic -fno-strict-aliasing

LIBRARY=Dessin
PROCESSING_CORE_JAR=/usr/share/processing/core/library/core.jar

SWIGDIR=src/swig
BUILDDIR=$(SWIGDIR)/build
LIBDIR=$(SWIGDIR)/lib
GENDIR=$(SWIGDIR)/gen
OBJDIR=$(SWIGDIR)/objs
SRCDIR=$(SWIGDIR)/src

SIMULATEUR=src/simulateur

.PHONY: all
all: $(LIBDIR)/$(LIBRARY).$(LIBRARY_EXT) $(LIBDIR)/$(LIBRARY).jar
mkdir -p $(SIMULATEUR)/code
cp $^ $(SIMULATEUR)/code

.PHONY: clean
clean:
rm -rf "$(GENDIR)" "$(OBJDIR)" "$(LIBDIR)" "$(BUILDDIR)" \
$(SWIGDIR)/$(LIBRARY)_wrap.*

$(SWIGDIR)/$(LIBRARY)_wrap.cxx: $(SWIGDIR)/$(LIBRARY).i
mkdir -p $(GENDIR)
$(SWIG) -java -c++ -outdir src/swig/gen "$^"

$(OBJDIR)/declarations.o: $(SRCDIR)/declarations.*
mkdir -p $(OBJDIR)
$(CPP) -c $(COPTS) $(SRCDIR)/declarations.cpp \
-o "$@"

$(OBJDIR)/$(LIBRARY)_wrap.o: $(SWIGDIR)/$(LIBRARY)_wrap.cxx
mkdir -p $(OBJDIR)
$(CPP) -c $(COPTS) "$^" \
-I$(JAVA_INCLUDE) \
-I$(JAVA_INCLUDE)/$(PLATFORM) \
-o "$@"

$(LIBDIR)/$(LIBRARY).$(LIBRARY_EXT): $(OBJDIR)/$(LIBRARY)_wrap.o $(OBJDIR)/declarations.o
mkdir -p $(LIBDIR)
$(CPP) -shared $^ -o "$@"

$(BUILDDIR)/$(LIBRARY).class: $(SWIGDIR)/$(LIBRARY)_wrap.cxx
mkdir -p $(BUILDDIR)
javac -d $(BUILDDIR) \
-cp $(PROCESSING_CORE_JAR) \
$(GENDIR)/*.java

$(LIBDIR)/$(LIBRARY).jar: $(BUILDDIR)/$(LIBRARY).class
mkdir -p $(LIBDIR)
jar cvfm \
"$@" \
"$(SRCDIR)/MANIFEST.MF" \
-C $(BUILDDIR) \
.
78 changes: 33 additions & 45 deletions src/programme/cinematique.h
Original file line number Diff line number Diff line change
@@ -1,61 +1,43 @@

bool isInside(float x, float y){
if( x<MIN_X+COR_X ){
Serial.print("OUT X");
Serial.print(x);
Serial.print("<");
Serial.print(MIN_X+COR_X);
Serial.println();
if( x < MIN_X+COR_X ){
LOG("OUT X", x, "<", MIN_X + COR_X);
return false;
}
if( x>MAX_X+COR_X ){
Serial.print("OUT X");
Serial.print(x);
Serial.print(">");
Serial.print(MAX_X+COR_X);
Serial.println();
if( x > MAX_X+COR_X ){
LOG("OUT X", x, ">", MAX_X + COR_X);
return false;
}
if( y<MIN_Y + COR_Y ){
Serial.print("OUT Y");
Serial.print(y);
Serial.print("<");
Serial.print(MIN_Y);
Serial.println();
if( y < MIN_Y+COR_Y ){
LOG("OUT Y", y, "<", MIN_Y + COR_Y);
return false;
}
if( y>MAX_Y + COR_Y){
Serial.print("OUT Y");
Serial.print(y);
Serial.print(">");
Serial.print(MAX_Y+ COR_Y);
Serial.println();
if( y > MAX_Y + COR_Y){
LOG("OUT Y", y, ">", MAX_Y + COR_Y);
return false;
}
return true;

if ( (x>MIN_X)&&(x<MAX_X)&&(y>MIN_Y)&&(y<MAX_Y) ) return true;
else return false;
}

bool toAngle(float x, float y){
// Merci Julien Marchand et François Tièche !!!
float r, t, alpha, beta, gamma, halfPerimeter, angleR1, angleR2;
float r, t, alpha, beta, gamma, halfPerimeter, angleR1, angleR2;
bool lefty = false; // pas utile avec la config mécanique choisie

old1 = angle1;
old2 = angle2;

x+=COR_X;y+=COR_Y; // Application correctifs translation
// Application correctifs translation
x+=COR_X;
y+=COR_Y;

r = sqrt(x*x+y*y);
r = sqrt(x*x + y*y);
alpha = atan2(y, x);
halfPerimeter = (lg1+lg2+r)/2;
halfPerimeter = (lg1+lg2+r) / 2.;

// sin(beta/2)
t = (halfPerimeter-lg1)*(halfPerimeter-r)/(lg1*r);
if (t < 0.0) {
Serial.println("INACESSIBLE");
LOG("INACCESSIBLE\n");

return false;
}
Expand All @@ -65,7 +47,7 @@ bool toAngle(float x, float y){
// sin(gamma/2)
t = (halfPerimeter-lg1)*(halfPerimeter-lg2)/(lg1*lg2);
if (t < 0.0) {
Serial.println("INACCESSIBLE");
LOG("INACCESSIBLE\n");
return false;
}

Expand All @@ -78,15 +60,21 @@ bool toAngle(float x, float y){
if (isInside(x,y)) {
angle1 = angleR1 * 180 / M_PI;
angle2 = angleR2 * 180 / M_PI;
angle1+=COR_S1;angle2+=COR_S2; // Application correctifs angles
if (angle2<ANGLE_MIN) angle2 = ANGLE_MIN; // Evite les collisionsq
}
else {
Serial.println("HORS DE LA FEUILLE");
return false;
// Application correctifs angles
angle1 += COR_S1;
angle2 += COR_S2;

if (angle2<ANGLE_MIN) {
// Evite les collisions
angle2 = ANGLE_MIN;
}

LOG(x, y);
return true;
}
if (debug) log(x,y);
return true;

LOG("HORS DE LA FEUILLE\n");
return false;
}

void moveServos()
Expand Down Expand Up @@ -152,8 +140,8 @@ void initServos()
s1.write(angle1);
s2.write(angle2);
delay(20);
if (debug) Serial.print(".");
LOG(".");
}
if (debug) Serial.println("INIT");
LOG("INIT\n");
}

44 changes: 33 additions & 11 deletions src/programme/declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <EEPROM.h>
#include <math.h>

#pragma once

// CONSTANTES
const int S1=7, S2=8; // Sorties arduino S1:bras, S2:avant-bras
const int VITESSE=0; // Pause en ms après un mouvement
Expand All @@ -16,7 +18,7 @@ const float MAX_X=130;
const float MIN_Y=10;
const float MAX_Y=205;

// VARIABLES
// VARIABLES
Servo s1, s2; // s1: épaule (en A), s2: coude (en B)
float lg1=149, lg2=130; // longueur des bras (mm) lg1 = AB (bras), lg2 = BC (avant-bras)
float angle1, angle2; // position servos en degrés de s1 et s2
Expand All @@ -26,15 +28,35 @@ float par = M_PI/180; // angle en radians entre deux calculs de points (1°)
bool microsec = true; // Mode précis (0.1°) si true
bool debug = true; // Affiche les déplacements dans le moniteur série

void log(char msg[], float t, float x, float y){
Serial.print(msg);Serial.print(" - t:");Serial.print(t);Serial.print(":(");Serial.print(x);
Serial.print(","); Serial.print(y);Serial.println(")");
}
// DEBOGGAGE

void log(float x, float y){
Serial.print("angles: S1:");Serial.print(angle1);Serial.print(" S2:");Serial.println(angle2);
Serial.print(" (x,y) : (");Serial.print(x);Serial.print(",");Serial.print(y);Serial.println(")\n");
}
#ifdef DEBUG
#define LOG(...) logging(__VA_ARGS__)

// Variations autour du logging.
void logging(char const msg[], float t, float x, float y){
Serial.print(msg);Serial.print(" - t:");Serial.print(t);Serial.print(":(");Serial.print(x);Serial.print(","); Serial.print(y);Serial.println(")");
Serial.println();
}

void logging(float x, float y){
Serial.print("angles: S1:");Serial.print(angle1);Serial.print(" S2:");Serial.println(angle2);
Serial.print(" (x,y) : (");Serial.print(x);Serial.print(",");Serial.print(y);Serial.println(")");
Serial.println();
}

void logging(char const msg[], float x, char const op[], float y) {
Serial.print("info: ");Serial.print(msg);Serial.print(x);Serial.print(op);Serial.print(y);Serial.println();
Serial.println();
}

void logging(char const msg[]) {
Serial.print(msg);
}

#else
#define LOG
#endif

void lectureCorrectif(){
delay(500);
Expand All @@ -48,7 +70,7 @@ void lectureCorrectif(){

int etendue = MAX_US-MIN_US;
int cor_epaule = epaule-1500; // écarts en us
int cor_coude = coude-1500;
int cor_coude = coude-1500;

COR_S1 = cor_epaule * 180./etendue +SHIFT_S1_ANGLE; //écarts en degrés
COR_S2 = cor_coude * 180./etendue;
Expand All @@ -58,4 +80,4 @@ void lectureCorrectif(){
Serial.print("), Cor en degrés : (");Serial.print(COR_S1);Serial.print(",");Serial.print(COR_S2); Serial.print(")");
// Serial.println("ok");Serial.println("ok");Serial.println("ok");Serial.println("ok");Serial.println("ok");Serial.println("ok");
}

18 changes: 18 additions & 0 deletions src/programme/dessine_liste.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Le code qui suit est propre à dessins.h
void dessineListe() {
int max = sizeof(liste)/sizeof(int);
float x,y;

// tableau de points dans la mémoire programme
// https://www.arduino.cc/en/Reference/PROGMEM
x = (int)pgm_read_word(liste)/100;
y = (int)pgm_read_word(liste+1)/100;
toAngle(x, y);
for (int i=0; i<max-1; i+=2) {
x = (int)pgm_read_word(liste+i)/100;
y = (int)pgm_read_word(liste+i+1/100);
toAngle(x, y);
moveServos();
LOG("LISTE", 0, x, y);
}
}
Loading