Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Nucleo_ServoKnob by
main.cpp
- Committer:
- PasKalou
- Date:
- 2016-05-29
- Revision:
- 1:f66f012c59de
- Parent:
- 0:3a3bfe92df7c
- Child:
- 2:204230a9177d
File content as of revision 1:f66f012c59de:
#include "mbed.h"
#include "Servo.h"
#include <math.h>
#define PI 3.14159265
//------------------------------------
// Hyperterminal configuration
// 9600 bauds, 8-bit data, no parity
//------------------------------------
Serial pc(SERIAL_TX, SERIAL_RX);
// configuration
Servo myservo0(D3); // Create the servo object
Servo myservo1(D5); // Create the servo object
Servo myservo2(D6); // Create the servo object
Servo myservo3(D9); // Create the servo object
AnalogIn knob0(A0); // Create the analog input object
AnalogIn knob1(A1); // Create the analog input object
AnalogIn knob2(A2); // Create the analog input object
AnalogIn knob3(A3); // Create the analog input object
DigitalIn monBouton(PC_13);
// fonction mapper
//
// quand i parcours l'intervalle [a ; b] alors j parcours l'intervalle [0 ; PI]
//
float mapper(float i, float a, float b, float c, float d)
{
float j;
j = ( ( d * (i - a) - c * (i - b) ) / (b - a) ) ;
return j;
}
// fonction ralentir
//
// retourne un coefficient compris entre 1 et 0
// avec une progression sinusoidale lorsque i parcours
// l'intervalle [a ; b] avec une progression linéaire
//
float accelerer(float i , float a , float b)
{
float j;
float angle;
if (i < a) {
j = 1;
} else if (i > b) {
j = 0;
} else {
angle = mapper (i , a , b , (-1 * PI) , 0 );
j = ((1 + cos(angle))/2);
}
return j;
}
float ralentir(float i , float a , float b)
{
float j;
float angle;
if (i < a) {
j = 1;
} else if (i > b) {
j = 0;
} else {
angle = mapper (i , a , b , 0 , PI );
j = ((1 + cos(angle))/2);
}
return j;
}
// fonction test
//
//
void test()
{
float j;
float a = 1 ;
float b = 0.5 ;
float nb = 100;
float coef ;
for (float i = 0 ; i< abs( (b-a) * nb ) ; i++) {
j = (i/nb);
coef = ralentir ( j , 0 , abs(b-a) );
myservo1.write (b - coef * (b-a));
wait_ms(10);
}
}
void aller(float fin0, float fin1)
{
float debut0 = myservo0.read();
// float fin0=0.5;
float debut1 = myservo1.read();
// float fin1=1;
float debut2 = debut0;
float fin2 = fin0;
int i;
for(i=0; i<100; i++) {
// remplacement du coefficient i/100 par un sinus
// et donc mouvement plus fluide
pc.printf("toto");
myservo1.write (debut1 + sin(i*PI/2.0/100.0) * (fin1-debut1));
wait_ms(2);
myservo0.write (debut0 + sin(i*PI/2.0/100.0) * (fin0-debut0));
wait_ms(2);
myservo2.write (debut2 + sin(i*PI/2.0/100.0) * (fin2-debut2));
wait_ms(2);
wait_ms(10);
}
myservo0.write(fin0);
wait_ms(15);
myservo1.write(fin1);
wait_ms(15);
myservo2.write(fin2);
wait_ms(15);
}
int main()
{
//calibration
myservo0.calibrate(0.00105, 150.0); // Calibrate the servo HDKJ D3015
myservo1.calibrate(0.00105, 150.0); // Calibrate the servo HDKJ D3015
myservo2.calibrate(0.00105, 150.0); // Calibrate the servo HDKJ D3015
myservo3.calibrate(0.00105, 150.0); // Calibrate the servo HDKJ D3015
//
// pour le servo tout merdique arduino :
// myservo1.calibrate(0.00075, 70.0); // Calibrate the servo GROOVE tout petit tout mini
//
// réinitialisation
aller(0.15,0.6);
wait_ms(3000);
aller(0.2,0.2);
aller(0.90,0.15);
aller(0.5,1);
wait_ms(3000);
//boucle
while(1) {
if (monBouton==0) {
pc.printf("\ntoto\n");
test();
}
/*
aller(0.5,1);
aller(0.4,0.9);
aller(0.6,0.8);
aller(0.5,0.5);
wait_ms(3000);
*/
/*
for(int i=0; i<100; i++) {
myservo0.write(i/100.0);
wait_ms(10);
myservo1 = i/100.0;
wait_ms(10);
}
for(int i=100; i>0; i--) {
myservo0 = i/100.0;
wait_ms(10);
myservo1 = i/100.0;
wait_ms(10);
}
val = knob0.read(); // Reads the value of the potentiometer (value between 0 and 1)
pc.printf("val = %f\n",val);
myservo0.write(val); // Sets the servo position according to the scaled value (0-1)
wait_ms(15); // Waits for the servo to get there
myservo0.position(0.0); // Sets the servo position according to the scaled value (0-1) wait_ms(1500); // Sets the servo position according to the scaled value (0-1)
wait_ms(15); // Sets the servo position according to the scaled value (0-1)
myservo1.position(-90.0); // Sets the servo position according to the scaled value (0-1) wait_ms(1500); // Sets the servo position according to the scaled value (0-1)
wait_ms(15); // Sets the servo position according to the scaled value (0-1)
myservo2.position(0); // Sets the servo position according to the scaled value (0-1)
wait_ms(15); // Sets the servo position according to the scaled value (0-1)
//
wait_ms(3000); // Sets the servo position according to the scaled value (0-1)
//
myservo0.position(-90.0); // Sets the servo position according to the scaled value (0-1) wait_ms(1500); // Sets the servo position according to the scaled value (0-1)
wait_ms(15); // Sets the servo position according to the scaled value (0-1)
myservo1.position(-60.0); // Sets the servo position according to the scaled value (0-1) wait_ms(1500); // Sets the servo position according to the scaled value (0-1)
wait_ms(15); // Sets the servo position according to the scaled value (0-1)
myservo2.position(-80); // Sets the servo position according to the scaled value (0-1)
wait_ms(15); // Sets the servo position according to the scaled value (0-1)
// Sets the servo position according to the scaled value (0-1)
//
wait_ms(3000); // Sets the servo position according to the scaled value (0-1)
//
myservo0.position(10.0); // Sets the servo position according to the scaled value (0-1) wait_ms(1500); // Sets the servo position according to the scaled value (0-1)
wait_ms(15); // Sets the servo position according to the scaled value (0-1)
myservo1.position(90.0); // Sets the servo position according to the scaled value (0-1) wait_ms(1500); // Sets the servo position according to the scaled value (0-1)
wait_ms(15); // Sets the servo position according to the scaled value (0-1)
myservo2.position(0); // Sets the servo position according to the scaled value (0-1)
wait_ms(3000); // Sets the servo position according to the scaled value (0-1)
// myservo0.position(-45.0); // Sets the servo position according to the scaled value (0-1)
// wait_ms(3500); // Sets the servo position according to the scaled value (0-1)
val = knob0.read(); // Reads the value of the potentiometer (value between 0 and 1)
myservo0.write(1); // Sets the servo position according to the scaled value (0-1)
myservo0.write(1); // Sets the servo position according to the scaled value (0-1)
wait_ms(15); // Sets the servo position according to the scaled value (0-1)
myservo1.write(1); // Sets the servo position according to the scaled value (0-1)
wait_ms(3000);
// Waits for the servo to get there
val = knob2.read(); // Reads the value of the potentiometer (value between 0 and 1)
myservo2.write(val); // Sets the servo position according to the scaled value (0-1)
wait_ms(15); // Waits for the servo to get there
val = knob3.read(); // Reads the value of the potentiometer (value between 0 and 1)
myservo3.write(val); // Sets the servo position according to the scaled value (0-1)
wait_ms(15); // Waits for the servo to get there
*/
}
}
