Close

rf + servo shield intro,, more log later

A project log for Quadruped learning project

Quadruped robot learning.

tsmspacetsmspace 02/03/2017 at 07:160 Comments

I will have to make a "real" log to discuss my activity, but this will be a short intro because I'm not prepared to make a good log entry.

I've switched to working on my quadruped model instead of the cardboard leg,, but ALSO, am on a work trip so don't have much of my normal electronics toys with me, and also don't have a very good place to work on things.

At first when switching to my quadruped, I made a one leg movement code, driven by hard-wired joysticks, and managed to result after various different test codes and code adjustments in a leg that moves oriented around the robot's center as xyz origin.

I struggled for days (including days off from work) on making my nrf2401 transmitters work again, in the end I still have to wiggle wires to make it work, several wires are broken, but I'm sure they do work, and I think transit here just caused some ruffled feathers that took me a long time to track down. (I kept playing with the code, and the wasn't able to swap parts like I'm used to, because they are all at home).

At present I have a code which can use two buttons to select each of two legs, and drives one leg well according to xyz input, although needing constraint work,, and causes the other leg to move erratically when selected. I will just have to share everything later. ,,,, actually,, I'll just post the bad codes here, which won't work properly, only as buggy problems....

sorry, no videos at the moment,, I could make one but it's not a good video worthy part of the experiment, and I haven't been using video while on my trip so would take extra effort.

transmitter code

#include<SPI.h>

#include"RF24.h"

#define joyStick_x A0

#define joyStick_y A1

#define joyStick_z A2

#define leftFrontSelect 4

#define rightFrontSelect 2

const int lf_on = 5;

const int rf_on = 3;

int lfs_button = 0;

int rfs_button = 0;

int lfs_led = 0;

int rfs_led = 0;

RF24 radio (9,10);

byte addresses [][10] = {"0"};

struct dataStruct

{

int Xposition;

int Yposition;

int Zposition;

int lf_select;

int rf_select;

} myData;

void setup() {

// put your setup code here, to run once:

pinMode(leftFrontSelect, INPUT);

pinMode(rightFrontSelect, INPUT);

pinMode(lf_on, OUTPUT);

pinMode(rf_on, OUTPUT);

digitalWrite(lf_on, LOW);

digitalWrite(rf_on, LOW);

Serial.begin(115200);

radio.begin();

radio.setChannel(115);

radio.setDataRate(RF24_250KBPS);

radio.setPALevel(RF24_PA_HIGH);

radio.openWritingPipe(addresses[0]);

radio.startListening();

}

void loop() {

// put your main code here, to run repeatedly:

lfs_button = digitalRead(leftFrontSelect);

rfs_button = digitalRead(rightFrontSelect);

if(lfs_button == HIGH)

{

Serial.println(lfs_button);

if (lfs_led == 0)

{

digitalWrite(lf_on, HIGH);

digitalWrite(rf_on, LOW);

lfs_led = 1;

rfs_led = 0;

Serial.println(lfs_led);

}

}

if(rfs_button == HIGH)

{

if(rfs_led == 0)

{

digitalWrite(rf_on, HIGH);

digitalWrite(lf_on, LOW);

rfs_led = 1;

lfs_led = 0;

}

}

radio.stopListening();

myData.Xposition = analogRead(joyStick_x);

myData.Yposition = analogRead(joyStick_y);

myData.Zposition = analogRead(joyStick_z);

myData.lf_select = lfs_led;

myData.rf_select = rfs_led;

Serial.println(myData.lf_select);

radio.write(&myData, sizeof(myData));

radio.startListening();

}

______________________________________

Receiver code

#include<Wire.h>

#include<Adafruit_PWMServoDriver.h>

#include<math.h>

#include<SPI.h>

#include"RF24.h"

RF24 radio (9,10);

byte addresses [][10] = {"0"};

int xJoystick;

int yJoystick;

int zJoystick;

int lf_button;

int rf_button;

struct dataStruct {

int Xposition;

int Yposition;

int Zposition;

int lf_select;

int rf_select;

} myData;

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

int SERVOMIN = 150;

int SERVOMAX = 600;

int coxa = 56;

int femur = 40;

int tibia = 60;

int camber = 10;

float x;

float y;

float z;

float rf_x = 90;

float rf_y = 40;

float rf_z = 50;

float lf_x = 90;

float lf_y = 40;

float lf_z = 50;

void setup() {

// put your setup code here, to run once:

Serial.begin(115200);

radio.begin();

radio.setChannel(115);

radio.setDataRate(RF24_250KBPS);

radio.setPALevel(RF24_PA_LOW);

radio.openReadingPipe(1, addresses[0]);

radio.startListening();

pwm.begin();

Serial.begin(115200);

pwm.setPWMFreq(60);

yield();

}

void loop() {

// put your main code here, to run repeatedly:

if (radio.available()){

{

while (radio.available())

radio.read(&myData, sizeof(myData));

}

}

xJoystick=myData.Xposition;

yJoystick=myData.Yposition;

zJoystick=myData.Zposition;

lf_button=myData.lf_select;

rf_button=myData.rf_select;

Serial.println(xJoystick);

float xchange = map(xJoystick, 0, 1023, -4, 4);

float ychange = map(yJoystick, 0, 1023, -4, 4);

float zchange = map(zJoystick, 0, 1023, -4, 4);

if (lf_button == HIGH && rf_button == LOW)

{

x = lf_x; y = lf_y; z = lf_z;

}

if (rf_button == HIGH && lf_button == LOW)

{

x = rf_x; y = rf_y; z = rf_z;

}

x = x + xchange; y = y + ychange; z = z + zchange;

float G = atan((x-12.5)/(y-41.5));

float D = sqrt (((x-12.5)*(x-12.5)) + ((y-41.5)*(y-41.5)));

float L = sqrt (((D-coxa)*(D-coxa))+(z*z));

float A = acos(((femur*femur)+(tibia*tibia)-(L*L))/(2*femur*tibia));

float B = (acos(((femur*femur)+(L*L)-(tibia*tibia))/(2*femur*L))+acos(((L*L)+(z*z)-((D-coxa)*(D-coxa)))/(2*L*z)));

G = degrees(G);

if (G<= 0) G = 180 + G;

A = degrees(A);

B = degrees(B);

if (lf_button == HIGH)

{

G = map(G, -40,160,-20,180);

G = map(G,0,180,150,600);

A = map(A,180,0,150,600);

B = map(B, 0, 180, 150, 600);

pwm.setPWM(0,0,G);

pwm.setPWM(1,0,B);

pwm.setPWM(2,0,A);

lf_x = x; lf_y=y; lf_z = z;

}

if (rf_button == HIGH)

{

G = map(G, 0, 200, -20, 180);

G = map(G, 0, 180, 150, 600);

A = map(A, 0, 180, 150, 600);

B = map(B, 180, 0, 150, 600);

pwm.setPWM(3, 0, G);

pwm.setPWM(4, 0, B);

pwm.setPWM(5, 0, A);

rf_x = x; rf_y = y; rf_z = z;

}

}

Discussions