6 years ago.  This question has been closed. Reason: Duplicate question

Remote Controlled Robotic Snake Servo

Hello, I'm trying to control the movement of the snake using a remote control but my programme does not work(the part with the pulse width). Can I have some help? (I'm still a student so I might take a while to understand)

  1. include "mbed.h"
  2. include "math.h"
  3. define PI 3.14159265358979323846264338327950288419716939937510582
  4. define amplitude 600
  5. define PeriodJoint 2
  6. define NoJoint 13
  7. define NoCycles 1.5
  8. define Resolution 0.01

DigitalOut myled1(LED1); DigitalOut myled2(LED2); PwmOut PWM(p21); Serial remote(NC,p27);p28 transmit, p27 receive Serial motor1(p9,NC);p9 transmit, p10 receive

signed int MotorAngle; int MotorID; unsigned short int straightPosition[] = {7700,8300, 7900,8200, 8200,7650, 7800,7850, 7200,7550, 7500,7800,7500};

{7700,7400, 7700,7700, 7500,7000,8000,7100, 7000,7650, 8900,7650,7050}; 1 and 5

unsigned short int currentposition, currentpositions; unsigned short int currentposition1[] = {0,0, 0,0, 0,0, 0,0, 0,0, 0,0,0}; unsigned short int currentposition2[] = {0,0, 0,0, 0,0, 0,0, 0,0, 0,0,0}; float Timing, kSpeed, kShape;

void motor_update(unsigned char Id, unsigned short int Position) { unsigned short int id,lo,hi; motor1.format(8,Serial::Even,1); motor1.baud(115200);

id=0x80|Id; hi=(Position>>7)&0x007F; lo=Position&0x007F;

motor1.putc(id); motor1.putc(hi); motor1.putc(lo);

wait(0.001); }

void straight(void) { for(MotorID=0;MotorID<=12;MotorID++) { motor_update(MotorID, straightPosition[MotorID]); currentposition1[MotorID] = straightPosition[MotorID]; currentposition2[MotorID] = straightPosition[MotorID]; wait(0.01); } } int main() { PWM.period(0.0142);

kSpeed = 2*PI/PeriodJoint; kShape = 2*PI*NoCycles/NoJoint;

myled1 = 0; myled2 = 0;

straight();

wait(1);

if(PWM.pulsewidth==0.002) { PWM = 0.14; myled1 = 1; myled2 = 1; for(Timing = 0.01;Timing<=12;Timing=Timing+0.01) { for(MotorID=0;MotorID<=12;MotorID++) { MotorAngle = (amplitude*sin((kSpeed*Timing)-(kShape*(MotorID+1)))); currentposition = (MotorAngle+currentposition1[MotorID]); motor_update(MotorID, currentposition); wait(0.0005); } } } if(PWM.pulsewidth<0.001) { PWM = 0.07; myled1 = 0; myled2 = 1; wait(1);

straight();

wait(1);

for(Timing = 0.01;Timing<=12;Timing=Timing+0.01) { for(MotorID=0;MotorID<=12;MotorID++) { MotorAngle = (amplitude*sin((kSpeed*Timing)+(kShape*(MotorID+1)))); currentpositions = (MotorAngle+currentposition2[MotorID]); motor_update(MotorID, currentpositions); wait(0.0005); } } } if(PWM.pulsewidth==0.0014) { PWM = 0.10; myled1 = 1; myled2 = 0; wait(2);

straight(); wait(2); } }