robotics - Arduino Making Spiral -
i have robot sweeper creating detects walls , turns when getting distance not hit. movements pretty random , device start out making small circle , growing larger one. seems getting stuck in 1 size circle maybe little growth. created multiple size circle functions not seem taking hold. ahead of time. no matter how small appreciated.
#include<newping.h> #define motor_a 0 #define motor_b 1 #define trigger_pin 5 #define echo_pin 4 #define max_distance 200 #define cw 0 #define ccw 1 const byte pwma = 3; const byte pwmb = 11; const byte dira = 12; const byte dirb = 13; newping sonar (trigger_pin, echo_pin, max_distance); void setup() { // put setup code here, run once: setupardumoto(); } void loop() { delay(50); unsigned int us= sonar.ping(); if(us/us_roundtrip_cm>50||us/us_roundtrip_cm==0) { forward(); curve(); //stopardumoto(motor_a); // stopardumoto(motor_b); } else if(us/us_roundtrip_cm>=90) { smallercurve(); } else if(us/us_roundtrip_cm>=110) { smallestcurve(); } else if(us/us_roundtrip_cm<20) { //turnright(100); delay(500); } // put main code here, run repeatedly: } void driveardumoto(byte motor, byte dir, byte spd) { if(motor == motor_a) { digitalwrite(dira, dir); analogwrite(pwma, spd); } else if(motor==motor_b) { digitalwrite(dirb,dir); analogwrite(pwmb, spd); } } void curve() { driveardumoto(motor_a, cw, 200); driveardumoto(motor_b, cw, 150); } void smallercurve() { driveardumoto(motor_a, cw, 200); driveardumoto(motor_b, cw, 120); } void smallestcurve() { driveardumoto(motor_a, cw, 200); driveardumoto(motor_b, cw, 100); } void forward() { driveardumoto(motor_a,cw,170); driveardumoto(motor_b,cw,170); } void turnright(byte spd) { stopardumoto(motor_b); driveardumoto(motor_a,cw,250); } void turnleft(byte spd) { stopardumoto(motor_a); driveardumoto(motor_b,cw,250); } void stopardumoto(byte motor) { driveardumoto(motor, 0,0); } void setupardumoto() { pinmode(pwma,output); pinmode(pwmb,output); pinmode(dira,output); pinmode(dirb,output); digitalwrite(pwma, low); digitalwrite(pwmb, low); digitalwrite(dira, low); digitalwrite(dirb, low); }
it's late think code execute smallercurve();
or smallestcurve();
because if us/us_roundtrip_cm
greater 50, code in first if
execute , jump code us/us_roundtrip_cm >= 90
, us/us_roundtrip_cm >= 110
when between 0 , 20 execute different things. take care of else-if
, once have executed 1 block of code in if
jump on else
, , first else
take rest of code. in fact, compiler doesn't compile else
's because unreacheable code.
and, moreover, can improve code simple things. careful function drive ardumoto
, don't use frequently, forward
won't have time move robot. use delay between movements.
Comments
Post a Comment