Moving to src for 1.5 library format
This commit is contained in:
611
src/Hippie.cpp
Normal file
611
src/Hippie.cpp
Normal file
@ -0,0 +1,611 @@
|
||||
#include "Arduino.h"
|
||||
|
||||
#include "Hippie.h"
|
||||
#include <Oscillator.h>
|
||||
|
||||
|
||||
|
||||
void Hippie::init(int YL, int YR, int RL, int RR, int Buzzer) {
|
||||
|
||||
servo_pins[0] = YL;
|
||||
servo_pins[1] = YR;
|
||||
servo_pins[2] = RL;
|
||||
servo_pins[3] = RR;
|
||||
|
||||
attachServos();
|
||||
isHippieResting=false;
|
||||
|
||||
|
||||
for (int i = 0; i < 4; i++) servo_position[i] = 90;
|
||||
|
||||
|
||||
//Buzzer & noise sensor pins:
|
||||
pinBuzzer = Buzzer;
|
||||
|
||||
pinMode(Buzzer,OUTPUT);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
//-- ATTACH & DETACH FUNCTIONS ----------------------------------//
|
||||
///////////////////////////////////////////////////////////////////
|
||||
void Hippie::attachServos(){
|
||||
servo[0].attach(servo_pins[0]);
|
||||
servo[1].attach(servo_pins[1]);
|
||||
servo[2].attach(servo_pins[2]);
|
||||
servo[3].attach(servo_pins[3]);
|
||||
}
|
||||
|
||||
void Hippie::detachServos(){
|
||||
servo[0].detach();
|
||||
servo[1].detach();
|
||||
servo[2].detach();
|
||||
servo[3].detach();
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
//-- BASIC MOTION FUNCTIONS -------------------------------------//
|
||||
///////////////////////////////////////////////////////////////////
|
||||
void Hippie::_moveServos(int time, int servo_target[]) {
|
||||
|
||||
attachServos();
|
||||
if(getRestState()==true){
|
||||
setRestState(false);
|
||||
}
|
||||
|
||||
if(time>10){
|
||||
for (int i = 0; i < 4; i++) increment[i] = ((servo_target[i]) - servo_position[i]) / (time / 10.0);
|
||||
final_time = millis() + time;
|
||||
|
||||
for (int iteration = 1; millis() < final_time; iteration++) {
|
||||
partial_time = millis() + 10;
|
||||
for (int i = 0; i < 4; i++) servo[i].SetPosition(servo_position[i] + (iteration * increment[i]));
|
||||
while (millis() < partial_time); //pause
|
||||
}
|
||||
}
|
||||
else{
|
||||
for (int i = 0; i < 4; i++) servo[i].SetPosition(servo_target[i]);
|
||||
}
|
||||
for (int i = 0; i < 4; i++) servo_position[i] = servo_target[i];
|
||||
}
|
||||
|
||||
|
||||
void Hippie::oscillateServos(int A[4], int O[4], int T, double phase_diff[4], float cycle=1){
|
||||
|
||||
for (int i=0; i<4; i++) {
|
||||
servo[i].SetO(O[i]);
|
||||
servo[i].SetA(A[i]);
|
||||
servo[i].SetT(T);
|
||||
servo[i].SetPh(phase_diff[i]);
|
||||
}
|
||||
double ref=millis();
|
||||
for (double x=ref; x<=T*cycle+ref; x=millis()){
|
||||
for (int i=0; i<4; i++){
|
||||
servo[i].refresh();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Hippie::_execute(int A[4], int O[4], int T, double phase_diff[4], float steps = 1.0){
|
||||
|
||||
attachServos();
|
||||
if(getRestState()==true){
|
||||
setRestState(false);
|
||||
}
|
||||
|
||||
|
||||
int cycles=(int)steps;
|
||||
|
||||
//-- Execute complete cycles
|
||||
if (cycles >= 1)
|
||||
for(int i = 0; i < cycles; i++)
|
||||
oscillateServos(A,O, T, phase_diff);
|
||||
|
||||
//-- Execute the final not complete cycle
|
||||
oscillateServos(A,O, T, phase_diff,(float)steps-cycles);
|
||||
}
|
||||
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
//-- HOME = Hippie at rest position -------------------------------//
|
||||
///////////////////////////////////////////////////////////////////
|
||||
void Hippie::home(){
|
||||
|
||||
if(isHippieResting==false){ //Go to rest position only if necessary
|
||||
|
||||
int homes[4]={90, 90, 90, 90}; //All the servos at rest position
|
||||
_moveServos(500,homes); //Move the servos in half a second
|
||||
|
||||
detachServos();
|
||||
isHippieResting=true;
|
||||
}
|
||||
}
|
||||
|
||||
bool Hippie::getRestState(){
|
||||
|
||||
return isHippieResting;
|
||||
}
|
||||
|
||||
void Hippie::setRestState(bool state){
|
||||
|
||||
isHippieResting = state;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////
|
||||
//-- PREDETERMINED MOTION SEQUENCES -----------------------------//
|
||||
///////////////////////////////////////////////////////////////////
|
||||
|
||||
//---------------------------------------------------------
|
||||
//-- Hippie movement: Jump
|
||||
//-- Parameters:
|
||||
//-- steps: Number of steps
|
||||
//-- T: Period
|
||||
//---------------------------------------------------------
|
||||
void Hippie::jump(float steps, int T){
|
||||
|
||||
int up[]={90,90,165,15};
|
||||
_moveServos(T,up);
|
||||
int down[]={90,90,90,90};
|
||||
_moveServos(T,down);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
//-- Hippie Test Positions (bring feets and hips in certain position)
|
||||
//---------------------------------------------------------
|
||||
void Hippie::test_pos(){
|
||||
|
||||
int left_feet_up[4]={90,0,90,30}; //watch from view of robot: [3] = left leg ... by + value: turn right
|
||||
_moveServos(1000,left_feet_up); // [4] = right leg ... by + value: right side up
|
||||
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------
|
||||
//-- Hippie gait: Walking (forward or backward)
|
||||
//-- Parameters:
|
||||
//-- * steps: Number of steps
|
||||
//-- * T : Period
|
||||
//-- * Dir: Direction: FORWARD / BACKWARD
|
||||
//---------------------------------------------------------
|
||||
void Hippie::walk(float steps, int T, int dir){
|
||||
|
||||
//-- Oscillator parameters for walking
|
||||
//-- Hip sevos are in phase
|
||||
//-- Feet servos are in phase
|
||||
//-- Hip and feet are 90 degrees out of phase
|
||||
//-- -90 : Walk forward
|
||||
//-- 90 : Walk backward
|
||||
//-- Feet servos also have the same offset (for tiptoe a little bit)
|
||||
int A[4]= {30, 30, 40, 40}; //20
|
||||
int O[4] = {0, 0, 4, 30}; //-4
|
||||
double phase_diff[4] = {0, 0, DEG2RAD(dir * -90), DEG2RAD(dir * -90)};
|
||||
|
||||
if ( dir == -1) { double phase_diff[4] = {0, 0, DEG2RAD(dir * 90), DEG2RAD(dir * 90)}; }
|
||||
|
||||
//-- Let's oscillate the servos!
|
||||
_execute(A, O, T, phase_diff, steps);
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------
|
||||
//-- Hippie gait: Turning (left or right)
|
||||
//-- Parameters:
|
||||
//-- * Steps: Number of steps
|
||||
//-- * T: Period
|
||||
//-- * Dir: Direction: LEFT / RIGHT
|
||||
//---------------------------------------------------------
|
||||
void Hippie::turn(float steps, int T, int dir){
|
||||
|
||||
//-- Same coordination than for walking (see Hippie::walk)
|
||||
//-- The Amplitudes of the hip's oscillators are not igual
|
||||
//-- When the right hip servo amplitude is higher, the steps taken by
|
||||
//-- the right leg are bigger than the left. So, the robot describes an
|
||||
//-- left arc
|
||||
int A[4]= {30, 30, 20, 20};
|
||||
int O[4] = {0, 0, 4, 30};
|
||||
double phase_diff[4] = {0, 0, DEG2RAD(-90), DEG2RAD(-90)};
|
||||
|
||||
if (dir == LEFT) {
|
||||
A[0] = 50; //-- Left hip servo
|
||||
A[1] = 10; //-- Right hip servo
|
||||
}
|
||||
else {
|
||||
A[0] = 10;
|
||||
A[1] = 50;
|
||||
A[2] = 40;
|
||||
}
|
||||
|
||||
//-- Let's oscillate the servos!
|
||||
_execute(A, O, T, phase_diff, steps);
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------
|
||||
//-- Hippie gait: Lateral bend
|
||||
//-- Parameters:
|
||||
//-- steps: Number of bends
|
||||
//-- T: Period of one bend
|
||||
//-- dir: RIGHT=Right bend LEFT=Left bend
|
||||
//---------------------------------------------------------
|
||||
void Hippie::bend (int steps, int T, int dir){
|
||||
|
||||
//Parameters of all the movements. Default: Left bend
|
||||
int bend1[4]={90, 90, 62, 35};
|
||||
int bend2[4]={90, 90, 62, 105+60};
|
||||
int homes[4]={90, 90, 90, 90};
|
||||
|
||||
//Time of one bend, constrained in order to avoid movements too fast.
|
||||
//T=max(T, 600);
|
||||
|
||||
//Changes in the parameters if right direction is chosen
|
||||
if(dir==-1)
|
||||
{
|
||||
bend1[2]=180-35;
|
||||
bend1[3]=180-60; //Not 65. Hippie is unbalanced
|
||||
bend2[2]=180-105;
|
||||
bend2[3]=180-60;
|
||||
}
|
||||
|
||||
//Time of the bend movement. Fixed parameter to avoid falls
|
||||
int T2=800;
|
||||
|
||||
//Bend movement
|
||||
for (int i=0;i<steps;i++)
|
||||
{
|
||||
_moveServos(T2/2,bend1);
|
||||
_moveServos(T2/2,bend2);
|
||||
delay(T*0.8);
|
||||
_moveServos(500,homes);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//--------------------------------------------------------
|
||||
//-- New Walk Move
|
||||
//-- number of steps
|
||||
//-- T: Period of one steps
|
||||
//-- dir: direction of movement
|
||||
//--------------------------------------------------------
|
||||
void Hippie::new_walk(int dir, float steps, int T){
|
||||
if(dir==1){
|
||||
// Positions of walking
|
||||
int Pos_A[4] = {90,90,180, 90};
|
||||
int Pos_B[4] = {90,90,180, 180};
|
||||
int Pos_C[4] = {90,45,90, 180};
|
||||
int Pos_D[4] = {45,45,90,90};
|
||||
|
||||
int Pos_E[4] = {45,45,30,30};
|
||||
int Pos_F[4] = {150,150,30,90};
|
||||
int Pos_G[4] = {150,150,90,90};
|
||||
|
||||
// run movements
|
||||
for (int i=0;i<steps;i++)
|
||||
{
|
||||
_moveServos(T/2, Pos_A);
|
||||
_moveServos(T/3, Pos_B);
|
||||
//delay(T);
|
||||
_moveServos(T/2, Pos_C);
|
||||
_moveServos(T/2, Pos_D);
|
||||
//delay(T);
|
||||
_moveServos(T/2, Pos_E);
|
||||
_moveServos(T/2, Pos_F);
|
||||
_moveServos(T/2, Pos_G);
|
||||
|
||||
//delay(T*3);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Backwards
|
||||
if (dir == 2){
|
||||
int Pos_1[4] = {90,90,180, 90};
|
||||
int Pos_2[4] = {90,90,180, 180};
|
||||
int Pos_3[4] = {90,145,90, 180};
|
||||
int Pos_4[4] = {145,145,90,90};
|
||||
|
||||
int Pos_5[4] = {145,145,30,30};
|
||||
int Pos_6[4] = {45,45,30,90};
|
||||
int Pos_7[4] = {45,45,90,90};
|
||||
|
||||
// run movements
|
||||
for (int i=0;i<steps;i++)
|
||||
{
|
||||
_moveServos(T/2, Pos_1);
|
||||
_moveServos(T/3, Pos_2);
|
||||
//delay(T);
|
||||
_moveServos(T/2, Pos_3);
|
||||
_moveServos(T/2, Pos_4);
|
||||
//delay(T);
|
||||
_moveServos(T/2, Pos_5);
|
||||
_moveServos(T/2, Pos_6);
|
||||
_moveServos(T/2, Pos_7);
|
||||
|
||||
//delay(T*3);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------
|
||||
//-- New Turn Move
|
||||
//-- number of steps
|
||||
//-- T: Period of one steps
|
||||
//-- dir: direction of movement
|
||||
//--------------------------------------------------------
|
||||
void Hippie::new_turn(int dir, float steps, int T){
|
||||
//LEFT
|
||||
if (dir==1) {
|
||||
// Positions of turning
|
||||
int Pos_A[4] = {90,90,90, 30};
|
||||
int Pos_B[4] = {90,90,30, 30};
|
||||
int Pos_C[4] = {0,90,30,90};
|
||||
int Pos_D[4] = {0,90,90,90};
|
||||
int Pos_E[4] = {90,90,90,90};
|
||||
|
||||
// run movements
|
||||
for (int i=0;i<steps;i++)
|
||||
{
|
||||
_moveServos(T/2, Pos_A);
|
||||
_moveServos(T/3, Pos_B);
|
||||
//delay(T);
|
||||
_moveServos(T/2, Pos_C);
|
||||
_moveServos(T/2, Pos_D);
|
||||
_moveServos(T/2, Pos_E);
|
||||
}
|
||||
}
|
||||
|
||||
//RIGHT
|
||||
if (dir==2) {
|
||||
// Positions of turning
|
||||
int Pos_1[4] = {90,90,180, 90};
|
||||
int Pos_2[4] = {90,90,180, 180};
|
||||
int Pos_3[4] = {90,180,90, 180};
|
||||
int Pos_4[4] = {90,180,90,90};
|
||||
int Pos_5[4] = {90,90,90,90};
|
||||
|
||||
// run movements
|
||||
for (int i=0;i<steps;i++)
|
||||
{
|
||||
_moveServos(T/2, Pos_1);
|
||||
_moveServos(T/3, Pos_2);
|
||||
//delay(T);
|
||||
_moveServos(T/2, Pos_3);
|
||||
_moveServos(T/2, Pos_4);
|
||||
_moveServos(T/2, Pos_5);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//---------------------------------------------------------
|
||||
//-- Hippie gait: Shake a leg
|
||||
//-- Parameters:
|
||||
//-- steps: Number of shakes
|
||||
//-- T: Period of one shake
|
||||
//-- dir: RIGHT=Right leg LEFT=Left leg
|
||||
//---------------------------------------------------------
|
||||
void Hippie::shakeLeg (int steps,int T,int dir){
|
||||
|
||||
//This variable change the amount of shakes
|
||||
int numberLegMoves=4;
|
||||
|
||||
//Parameters of all the movements. Default: Right leg
|
||||
int shake_leg1[4]={90, 90, 58, 35-15};
|
||||
int shake_leg2[4]={90, 90, 58, 120+30};
|
||||
int shake_leg3[4]={90, 90, 58, 60-30};
|
||||
int homes[4]={90, 90, 90, 90};
|
||||
|
||||
//Changes in the parameters if left leg is chosen
|
||||
if(dir==-1)
|
||||
{
|
||||
shake_leg1[2]=180-35;
|
||||
shake_leg1[3]=180-58;
|
||||
shake_leg2[2]=180-120;
|
||||
shake_leg2[3]=180-58;
|
||||
shake_leg3[2]=180-60;
|
||||
shake_leg3[3]=180-58;
|
||||
}
|
||||
|
||||
//Time of the bend movement. Fixed parameter to avoid falls
|
||||
int T2=1000;
|
||||
//Time of one shake, constrained in order to avoid movements too fast.
|
||||
T=T-T2;
|
||||
T=max(T,200*numberLegMoves);
|
||||
|
||||
for (int j=0; j<steps;j++)
|
||||
{
|
||||
//Bend movement
|
||||
_moveServos(T2/2,shake_leg1);
|
||||
_moveServos(T2/2,shake_leg2);
|
||||
|
||||
//Shake movement
|
||||
for (int i=0;i<numberLegMoves;i++)
|
||||
{
|
||||
_moveServos(T/(2*numberLegMoves),shake_leg3);
|
||||
_moveServos(T/(2*numberLegMoves),shake_leg2);
|
||||
}
|
||||
_moveServos(500,homes); //Return to home position
|
||||
}
|
||||
|
||||
delay(T);
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------
|
||||
//-- Hippie movement: up & down
|
||||
//-- Parameters:
|
||||
//-- * steps: Number of jumps
|
||||
//-- * T: Period
|
||||
//-- * h: Jump height: SMALL / MEDIUM / BIG
|
||||
//-- (or a number in degrees 0 - 90)
|
||||
//---------------------------------------------------------
|
||||
void Hippie::updown(float steps, int T, int h){
|
||||
|
||||
//-- Both feet are 180 degrees out of phase
|
||||
//-- Feet amplitude and offset are the same
|
||||
//-- Initial phase for the right foot is -90, so that it starts
|
||||
//-- in one extreme position (not in the middle)
|
||||
int A[4]= {0, 0, h, h};
|
||||
int O[4] = {0, 0, h, -h+50};
|
||||
double phase_diff[4] = {0, 0, DEG2RAD(-90), DEG2RAD(90)};
|
||||
|
||||
//-- Let's oscillate the servos!
|
||||
_execute(A, O, T, phase_diff, steps);
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------
|
||||
//-- Hippie movement: swinging side to side
|
||||
//-- Parameters:
|
||||
//-- steps: Number of steps
|
||||
//-- T : Period
|
||||
//-- h : Amount of swing (from 0 to 50 aprox)
|
||||
//---------------------------------------------------------
|
||||
void Hippie::swing(float steps, int T, int h){
|
||||
|
||||
//-- Both feets are in phase. The offset is half the amplitude
|
||||
//-- It causes the robot to swing from side to side
|
||||
int A[4]= {0, 0, h, h};
|
||||
int O[4] = {0, 0, h/2-20, -h/2+50-20};
|
||||
double phase_diff[4] = {0, 0, DEG2RAD(0), DEG2RAD(0)};
|
||||
|
||||
//-- Let's oscillate the servos!
|
||||
_execute(A, O, T, phase_diff, steps);
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------
|
||||
//-- Hippie movement: swinging side to side without touching the floor with the heel
|
||||
//-- Parameters:
|
||||
//-- steps: Number of steps
|
||||
//-- T : Period
|
||||
//-- h : Amount of swing (from 0 to 50 aprox)
|
||||
//---------------------------------------------------------
|
||||
void Hippie::tiptoeSwing(float steps, int T, int h){
|
||||
|
||||
//-- Both feets are in phase. The offset is not half the amplitude in order to tiptoe
|
||||
//-- It causes the robot to swing from side to side
|
||||
int A[4]= {0, 0, h, h};
|
||||
int O[4] = {0, 0, h-20, -h+50};
|
||||
double phase_diff[4] = {0, 0, 0, 0};
|
||||
|
||||
//-- Let's oscillate the servos!
|
||||
_execute(A, O, T, phase_diff, steps);
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------
|
||||
//-- Hippie gait: Jitter
|
||||
//-- Parameters:
|
||||
//-- steps: Number of jitters
|
||||
//-- T: Period of one jitter
|
||||
//-- h: height (Values between 5 - 25)
|
||||
//---------------------------------------------------------
|
||||
void Hippie::jitter(float steps, int T, int h){
|
||||
|
||||
//-- Both feet are 180 degrees out of phase
|
||||
//-- Feet amplitude and offset are the same
|
||||
//-- Initial phase for the right foot is -90, so that it starts
|
||||
//-- in one extreme position (not in the middle)
|
||||
//-- h is constrained to avoid hit the feets
|
||||
h=min(25,h);
|
||||
int A[4]= {h, h, 0, 0};
|
||||
int O[4] = {0, 0, 0, 0};
|
||||
double phase_diff[4] = {DEG2RAD(-90), DEG2RAD(90), 0, 0};
|
||||
|
||||
//-- Let's oscillate the servos!
|
||||
_execute(A, O, T, phase_diff, steps);
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------
|
||||
//-- Hippie gait: Ascending & turn (Jitter while up&down)
|
||||
//-- Parameters:
|
||||
//-- steps: Number of bends
|
||||
//-- T: Period of one bend
|
||||
//-- h: height (Values between 5 - 15)
|
||||
//---------------------------------------------------------
|
||||
void Hippie::ascendingTurn(float steps, int T, int h){
|
||||
|
||||
//-- Both feet and legs are 180 degrees out of phase
|
||||
//-- Initial phase for the right foot is -90, so that it starts
|
||||
//-- in one extreme position (not in the middle)
|
||||
//-- h is constrained to avoid hit the feets
|
||||
h=min(13,h);
|
||||
int A[4]= {h, h, h, h};
|
||||
int O[4] = {0, 0, h+4, -h+40};
|
||||
double phase_diff[4] = {DEG2RAD(-90), DEG2RAD(90), DEG2RAD(-90), DEG2RAD(90)};
|
||||
|
||||
//-- Let's oscillate the servos!
|
||||
_execute(A, O, T, phase_diff, steps);
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------
|
||||
//-- Hippie gait: Moonwalker. Hippie moves like Michael Jackson
|
||||
//-- Parameters:
|
||||
//-- Steps: Number of steps
|
||||
//-- T: Period
|
||||
//-- h: Height. Typical valures between 15 and 40
|
||||
//-- dir: Direction: LEFT / RIGHT
|
||||
//---------------------------------------------------------
|
||||
void Hippie::moonwalker(float steps, int T, int h, int dir){
|
||||
|
||||
//-- This motion is similar to that of the caterpillar robots: A travelling
|
||||
//-- wave moving from one side to another
|
||||
//-- The two Hippie's feet are equivalent to a minimal configuration. It is known
|
||||
//-- that 2 servos can move like a worm if they are 120 degrees out of phase
|
||||
//-- In the example of Hippie, the two feet are mirrored so that we have:
|
||||
//-- 180 - 120 = 60 degrees. The actual phase difference given to the oscillators
|
||||
//-- is 60 degrees.
|
||||
//-- Both amplitudes are equal. The offset is half the amplitud plus a little bit of
|
||||
//- offset so that the robot tiptoe lightly
|
||||
|
||||
int A[4]= {0, 0, h, h};
|
||||
int O[4] = {0, 0, h/2+2, -h/2 -2+60};
|
||||
int phi = -dir * 90;
|
||||
double phase_diff[4] = {0, 0, DEG2RAD(phi), DEG2RAD(-60 * dir + phi)};
|
||||
|
||||
//-- Let's oscillate the servos!
|
||||
_execute(A, O, T, phase_diff, steps);
|
||||
}
|
||||
|
||||
|
||||
//----------------------------------------------------------
|
||||
//-- Hippie gait: Crusaito. A mixture between moonwalker and walk
|
||||
//-- Parameters:
|
||||
//-- steps: Number of steps
|
||||
//-- T: Period
|
||||
//-- h: height (Values between 20 - 50)
|
||||
//-- dir: Direction: LEFT / RIGHT
|
||||
//-----------------------------------------------------------
|
||||
void Hippie::crusaito(float steps, int T, int h, int dir){
|
||||
|
||||
int A[4]= {25, 25, h, h};
|
||||
int O[4] = {0, 0, h/2+ 4, -h/2 - 4+30};
|
||||
double phase_diff[4] = {90, 90, DEG2RAD(0), DEG2RAD(-60 * dir)};
|
||||
|
||||
//-- Let's oscillate the servos!
|
||||
_execute(A, O, T, phase_diff, steps);
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------
|
||||
//-- Hippie gait: Flapping
|
||||
//-- Parameters:
|
||||
//-- steps: Number of steps
|
||||
//-- T: Period
|
||||
//-- h: height (Values between 10 - 30)
|
||||
//-- dir: direction: FOREWARD, BACKWARD
|
||||
//---------------------------------------------------------
|
||||
void Hippie::flapping(float steps, int T, int h, int dir){
|
||||
|
||||
int A[4]= {12+10, 12+10, h, h};
|
||||
int O[4] = {0, 0, h - 10, -h + 10+60};
|
||||
double phase_diff[4] = {DEG2RAD(0), DEG2RAD(180), DEG2RAD(-90 * dir), DEG2RAD(90 * dir)};
|
||||
|
||||
//-- Let's oscillate the servos!
|
||||
_execute(A, O, T, phase_diff, steps);
|
||||
}
|
80
src/Hippie.h
Normal file
80
src/Hippie.h
Normal file
@ -0,0 +1,80 @@
|
||||
#ifndef Hippie_h
|
||||
#define Hippie_h
|
||||
|
||||
#include <Oscillator.h>
|
||||
|
||||
//-- Constants
|
||||
#define FORWARD 1
|
||||
#define BACKWARD -1
|
||||
#define LEFT 1
|
||||
#define RIGHT -1
|
||||
#define SMALL 5
|
||||
#define MEDIUM 15
|
||||
#define BIG 30
|
||||
|
||||
|
||||
class Hippie
|
||||
{
|
||||
public:
|
||||
|
||||
//-- Hippie initialization
|
||||
void init(int YL, int YR, int RL, int RR, int Buzzer=PIN_Buzzer);
|
||||
|
||||
//-- Attach & detach functions
|
||||
void attachServos();
|
||||
void detachServos();
|
||||
|
||||
//-- Predetermined Motion Functions
|
||||
void _moveServos(int time, int servo_target[]);
|
||||
void oscillateServos(int A[4], int O[4], int T, double phase_diff[4], float cycle);
|
||||
|
||||
//-- HOME = Hippie at rest position
|
||||
void home();
|
||||
bool getRestState();
|
||||
void setRestState(bool state);
|
||||
|
||||
//-- Predetermined Motion Functions
|
||||
void jump(float steps=1, int T = 600);
|
||||
|
||||
void walk(float steps=4, int T=1000, int dir = FORWARD);
|
||||
void turn(float steps=4, int T=1000, int dir = LEFT);
|
||||
void bend (int steps=1, int T=1400, int dir=LEFT);
|
||||
void shakeLeg (int steps=1, int T = 2000, int dir=RIGHT);
|
||||
|
||||
void updown(float steps=1, int T=1000, int h = 40);
|
||||
void swing(float steps=1, int T=1000, int h=40);
|
||||
void tiptoeSwing(float steps=1, int T=900, int h=40);
|
||||
void jitter(float steps=1, int T=500, int h=50);
|
||||
void ascendingTurn(float steps=1, int T=900, int h=50);
|
||||
|
||||
void moonwalker(float steps=1, int T=900, int h=50, int dir=LEFT);
|
||||
void crusaito(float steps=1, int T=900, int h=20, int dir=FORWARD);
|
||||
void flapping(float steps=1, int T=1000, int h=50, int dir=FORWARD);
|
||||
|
||||
void test_pos();
|
||||
|
||||
void new_walk(int dir = FORWARD, float steps =4, int T=750);
|
||||
void new_turn(int dir = LEFT, float steps =2, int T=1000);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
Oscillator servo[4];
|
||||
|
||||
int servo_pins[4];
|
||||
int servo_trim[4];
|
||||
int servo_position[4];
|
||||
|
||||
int pinBuzzer;
|
||||
|
||||
unsigned long final_time;
|
||||
unsigned long partial_time;
|
||||
float increment[4];
|
||||
|
||||
bool isHippieResting;
|
||||
|
||||
void _execute(int A[4], int O[4], int T, double phase_diff[4], float steps);
|
||||
|
||||
};
|
||||
|
||||
#endif
|
125
src/Oscillator.cpp
Normal file
125
src/Oscillator.cpp
Normal file
@ -0,0 +1,125 @@
|
||||
//--------------------------------------------------------------
|
||||
//-- Oscillator.pde
|
||||
//-- Generate sinusoidal oscillations in the servos
|
||||
//--------------------------------------------------------------
|
||||
//-- (c) Juan Gonzalez-Gomez (Obijuan), Dec 2011
|
||||
//-- GPL license
|
||||
//--------------------------------------------------------------
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "Oscillator.h"
|
||||
#if defined(ESP32)
|
||||
#include <ESP32_Servo.h>
|
||||
#else
|
||||
#include <Servo.h>
|
||||
#endif
|
||||
|
||||
//-- This function returns true if another sample
|
||||
//-- should be taken (i.e. the TS time has passed since
|
||||
//-- the last sample was taken
|
||||
bool Oscillator::next_sample()
|
||||
{
|
||||
|
||||
//-- Read current time
|
||||
_currentMillis = millis();
|
||||
|
||||
//-- Check if the timeout has passed
|
||||
if(_currentMillis - _previousMillis > _TS) {
|
||||
_previousMillis = _currentMillis;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//-- Attach an oscillator to a servo
|
||||
//-- Input: pin is the arduino pin were the servo
|
||||
//-- is connected
|
||||
void Oscillator::attach(int pin, bool rev)
|
||||
{
|
||||
//-- If the oscillator is detached, attach it.
|
||||
|
||||
//-- Attach the servo and move it to the home position
|
||||
_servo.attach(pin);
|
||||
_servo.write(90);
|
||||
|
||||
//-- Initialization of oscilaltor parameters
|
||||
_TS=30;
|
||||
_T=2000;
|
||||
__N = _T/_TS;
|
||||
_inc = 2*M_PI/__N;
|
||||
|
||||
_previousMillis=0;
|
||||
|
||||
//-- Default parameters
|
||||
_A=45;
|
||||
_phase=0;
|
||||
_phase0=0;
|
||||
_O=0;
|
||||
_stop=false;
|
||||
|
||||
//-- Reverse mode
|
||||
_rev = rev;
|
||||
|
||||
}
|
||||
|
||||
//-- Detach an oscillator from his servo
|
||||
void Oscillator::detach()
|
||||
{
|
||||
//-- If the oscillator is attached, detach it.
|
||||
if(_servo.attached())
|
||||
_servo.detach();
|
||||
|
||||
}
|
||||
|
||||
/*************************************/
|
||||
/* Set the oscillator period, in ms */
|
||||
/*************************************/
|
||||
void Oscillator::SetT(unsigned int T)
|
||||
{
|
||||
//-- Assign the new period
|
||||
_T=T;
|
||||
|
||||
//-- Recalculate the parameters
|
||||
__N = _T/_TS;
|
||||
_inc = 2*M_PI/__N;
|
||||
};
|
||||
|
||||
/*******************************/
|
||||
/* Manual set of the position */
|
||||
/******************************/
|
||||
|
||||
void Oscillator::SetPosition(int position)
|
||||
{
|
||||
_servo.write(position+_trim);
|
||||
};
|
||||
|
||||
|
||||
/*******************************************************************/
|
||||
/* This function should be periodically called */
|
||||
/* in order to maintain the oscillations. It calculates */
|
||||
/* if another sample should be taken and position the servo if so */
|
||||
/*******************************************************************/
|
||||
void Oscillator::refresh()
|
||||
{
|
||||
|
||||
//-- Only When TS milliseconds have passed, the new sample is obtained
|
||||
if (next_sample()) {
|
||||
|
||||
//-- If the oscillator is not stopped, calculate the servo position
|
||||
if (!_stop) {
|
||||
//-- Sample the sine function and set the servo pos
|
||||
_pos = round(_A * sin(_phase + _phase0) + _O);
|
||||
if (_rev) _pos=-_pos;
|
||||
Serial.println(_pos+90+_trim);
|
||||
_servo.write(_pos+90+_trim);
|
||||
}
|
||||
|
||||
//-- Increment the phase
|
||||
//-- It is always increased, even when the oscillator is stop
|
||||
//-- so that the coordination is always kept
|
||||
_phase = _phase + _inc;
|
||||
|
||||
}
|
||||
}
|
72
src/Oscillator.h
Normal file
72
src/Oscillator.h
Normal file
@ -0,0 +1,72 @@
|
||||
//--------------------------------------------------------------
|
||||
//-- Oscillator.pde
|
||||
//-- Generate sinusoidal oscillations in the servos
|
||||
//--------------------------------------------------------------
|
||||
//-- (c) Juan Gonzalez-Gomez (Obijuan), Dec 2011
|
||||
//-- GPL license
|
||||
//--------------------------------------------------------------
|
||||
#ifndef Oscillator_h
|
||||
#define Oscillator_h
|
||||
|
||||
#if defined(ESP32)
|
||||
#include <ESP32_Servo.h>
|
||||
#else
|
||||
#include <Servo.h>
|
||||
#endif
|
||||
|
||||
//-- Macro for converting from degrees to radians
|
||||
#ifndef DEG2RAD
|
||||
#define DEG2RAD(g) ((g)*M_PI)/180
|
||||
#endif
|
||||
|
||||
class Oscillator
|
||||
{
|
||||
public:
|
||||
Oscillator(int trim=0) {_trim=trim;};
|
||||
void attach(int pin, bool rev =false);
|
||||
void detach();
|
||||
|
||||
void SetA(unsigned int A) {_A=A;};
|
||||
void SetO(unsigned int O) {_O=O;};
|
||||
void SetPh(double Ph) {_phase0=Ph;};
|
||||
void SetT(unsigned int T);
|
||||
void SetTrim(int trim){_trim=trim;};
|
||||
int getTrim() {return _trim;};
|
||||
void SetPosition(int position);
|
||||
void Stop() {_stop=true;};
|
||||
void Play() {_stop=false;};
|
||||
void Reset() {_phase=0;};
|
||||
void refresh();
|
||||
|
||||
private:
|
||||
bool next_sample();
|
||||
|
||||
private:
|
||||
//-- Servo that is attached to the oscillator
|
||||
Servo _servo;
|
||||
|
||||
//-- Oscillators parameters
|
||||
unsigned int _A; //-- Amplitude (degrees)
|
||||
unsigned int _O; //-- Offset (degrees)
|
||||
unsigned int _T; //-- Period (miliseconds)
|
||||
double _phase0; //-- Phase (radians)
|
||||
|
||||
//-- Internal variables
|
||||
int _pos; //-- Current servo pos
|
||||
int _trim; //-- Calibration offset
|
||||
double _phase; //-- Current phase
|
||||
double _inc; //-- Increment of phase
|
||||
double __N; //-- Number of samples
|
||||
unsigned int _TS; //-- sampling period (ms)
|
||||
|
||||
long _previousMillis;
|
||||
long _currentMillis;
|
||||
|
||||
//-- Oscillation mode. If true, the servo is stopped
|
||||
bool _stop;
|
||||
|
||||
//-- Reverse mode
|
||||
bool _rev;
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user