Skip to content

Instantly share code, notes, and snippets.

@antimatter15
Created April 20, 2012 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antimatter15/2431757 to your computer and use it in GitHub Desktop.
Save antimatter15/2431757 to your computer and use it in GitHub Desktop.
// Created on Sat Feb 25 2012
#include <stdio.h>
//this C macro makes it easy to drive a certain direction for a certain distance
#define INERCOM 70 //this is the inertial compensator coefficient.
//this is the percent to dampen the velocity coefficients to compensate for inertia
//it's probably better to underestimate than to overestimate because there's greater
//energy required/opportunity for error when you overshoot and need to backtrack
//y = 0.4519x - 0.9857 R-squared = 0.9981 ONE SECOND
//y = 0.0459x - 0.4519 R-squared = 0.8803 ONE DECISECOND
//y = 0.4471x - 0.5461 R-squared = 0.9994 ONE SECOND
//centered about origin
//y = 0.4455x R-squared = 0.9993
//y = 0.0446x R-squared = 0.8793
//y = 0.4491x R-squared = 0.9981
//y = angle recorded (deg); x = duration (seconds) * speed (mm/s)
//duration * speed * 0.4455 = angle
//angle / 0.4455 = duration * speed
//(deg) = (deg/mm) * (mm/s) * (s)
//magic coefficient = 360/(diameter (mm) * Math.PI)
//eg: when diameter = 10in (254mm) = 0.4511478701
//more precise measurement: 11in - 7.1mm * 2.3 = 263.1mm = 0.4355437439230887
#define ROT_COEF 0.4355
#define turn(x) _turn(x, 2)
#define drive(x) _drive(x, 10)
#define drive_core(r_speed, l_speed) create_drive_direct(r_speed * INERCOM / 100, l_speed * INERCOM / 100)
int __drive(int velocity, float duration){
set_create_distance (0);
int sign = 1;
if(velocity < 0){
velocity = -velocity;
sign = -sign;
}
if(duration < 0){
duration = -duration;
sign = -sign;
}
drive_core(sign * velocity, sign * velocity);
printf("Driving at %d for %f \n", velocity, duration);
sleep(duration);
create_stop();
return get_create_distance(0);
//printf("Got returned offset %d \n", distance - value);
//return distance - value;
}
int _vdrive(int distance, int velocity){
int value = __drive(velocity, (float) distance / (float) velocity);
printf("Got returned offset %d \n", distance - value);
return distance - value;
}
int _ddrive(int distance, float duration){
int value = __drive(((float) distance / duration), duration);
printf("Got returned offset %d \n", distance - value);
return distance - value;
}
int _drive(int distance, int uncertainty){
int residue;
if(abs(distance) > 200){
residue = _vdrive(distance, 500);
}else{
residue = _ddrive(distance, 0.5);
}
if(abs(residue) > uncertainty){
return _drive(residue, uncertainty);
}
return residue;
}
int __turn(float velocity, float duration){
create_stop();
set_create_total_angle(0);
int sign = 1;
if(velocity < 0){
velocity = -velocity;
sign = -sign;
}
if(duration < 0){
duration = -duration;
sign = -sign;
}
printf("Turning at %f and %f \n", velocity, duration);
drive_core(sign * velocity, -sign * velocity);
sleep(duration);
create_stop();
return get_create_total_angle(0);
}
//turn a certain number of degrees when velocity is held constant
int _vturn(int degrees, float velocity){
float duration = ((float) degrees / ROT_COEF) / velocity;
int value = __turn(velocity, duration);
printf("Residual %d/%d = %d \n", degrees, value, degrees - value);
return degrees - value;
}
//turn a certain number of degrees when duration is held constant
int _dturn(int degrees, float duration){
float velocity = ((float) degrees / ROT_COEF) / duration;
int value = __turn(velocity, duration);
printf("Residual %d/%d = %d \n", degrees, value, degrees - value);
return degrees - value;
}
#define abs(x) (x > 0 ? x : -x)
//apparently, calling get_create_total_angle causes the create to truncate values, so we use it sparingly.
int _turn(int degrees, int uncertainty){
int residue;
if(abs(degrees) > 30){
residue = _vturn(degrees, 250);
}else{
residue = _dturn(degrees, 0.5);
}
if(abs(residue) > uncertainty){
return _turn(residue, uncertainty);
}
return residue;
}
int release(){
set_servo_position(0, 2047-1000);
set_servo_position(1, 1000);
sleep(1);
}
int grab(){
set_servo_position(0, 2047);
set_servo_position(1, 0);
sleep(1);
}
//(4 /*distance of one rotation*/)*(0.11 * (360/60) /*time for one rotation*/)
//~26.4 inches height is seven inches
int up(){
mtp(1, 1000, 0);
}
int down(){
mtp(1, 1000, -2121);
}
int main()
{
while(create_connect()){
printf("Trying again in five seconds...");
sleep(5);
}
clear_motor_position_counter(1);
/*sleep(2);
grab();
sleep(2);
drive(-500);
down();
sleep(2);
release();
sleep(2);
up();
sleep(2);
*/
printf("Hello, World!\n");
return 0;
}
@Manjuphoenix
Copy link

What does this do?, I tried running this on my pc but there was a lot of error
Just curious to know since this involves more of physics!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment