|
Hello, Im just recently getting back into playing with Legos. I bought my
first house and had to fix it up before I could play. BrickOS was a lot easier
to get into this time around thanks to BricxCC. BrickOS itself is better
documented and has new features namely support of the Lego remote. Which brings
me to my question how do I use it inside my program? This code is a simplified
version of code that I wrote to control motor speed and direction on my marble
rail.
I would like to implement code that every time LRKEY_A1 is pressed rc_b += 1 and
every time LRKEY_A2 += -1. Any help or push in the right direction would be
greatly appreciated. Thanks in advance Chris
#include <conio.h>
#include <unistd.h>
#include <dsensor.h>
#include <dmotor.h>
#include <time.h>
int main()
{
int set_b = 0;
//distance apart variables
int mr = 0;
int rc_b = 0;
int diff_b = 0;
//time apart variables
int long time_b = 0;
int speed_b = 0;
ds_active(&SENSOR_1);
ds_rotation_on(&SENSOR_1);
ds_active(&SENSOR_2);
ds_rotation_on(&SENSOR_2);
ds_rotation_set(&SENSOR_1,0);
ds_rotation_set(&SENSOR_2,0);
msleep(10);
////////////////////////////////////////////////////////////////////////////
motor_b_speed(150);
motor_b_dir(rev);
msleep(100);
while(ROTATION_2 != set_b)
{
set_b = ROTATION_2;
msleep(250);
}
ds_rotation_set(&SENSOR_2,- 35);
msleep(10);
while(1)
{
msleep(1);
rc_b = ROTATION_1;
// While marble rail and controller are not equal
if (rc_b != mr)
{
// Find difference apart if both are pos or neg
//inverse mr and ad them together
if (mr < 1 && rc_b < 1 || mr > -1 && rc_b > -1)diff_b = mr * -1 + rc_b;
// Find difference when one is positive & one is negative
else
{
// Inverse mr then add to rc
if(mr < 0)diff_b = mr * -1 + rc_b;
// If marble rail is positive
//inverse rc then add to mr
else diff_b = rc_b * -1 + mr;
}
//make sure we get the absolute value
if(diff_b < 0)diff_b *= -1;
//time apart + distanc = speed
speed_b=((get_system_up_time() - time_b) * 1 / 10) + (diff_b * 25);
// go forword
if(mr < rc_b)
{
if(speed_b > 255) speed_b = 255;
motor_b_speed(speed_b);
motor_b_dir(fwd);
}
//do same as above but reverse dir
else
{
speed_b =((speed_b * 6) / 10);//less power for down
if(speed_b > 255) speed_b = 255;
motor_b_speed(speed_b);
motor_b_dir(rev);
}
}
//marble rail & controller are the same
else
{
motor_b_speed(255);
motor_b_dir(brake);
//set time_b to curent time
time_b = get_system_up_time();
}
}
}
|
|
1 Message in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|