27Loco wrote:I started to do some simple servo movements but I am facing some problems.
As long as I am doing everything in the main function the code works fine but when I try something like the following:
- Code: Select all
void WalkForward(dx_stance_t s) {//some code here}
int main void() {
// init stuff
WalkForward(s) // not working
}
I just created a simple C function copying the code from main into that function but nothin happens.
What are the reasons?
Thanks for the help
#includes
....
#define tdel 15 // delay timer
#define RF 3 // right foot
#define LF 4 // left foot
#define RL 1 // right leg
#define LL 2 // left leg
void WalkForward(dx_stance_t *s) // walk straight forward
{
// step left
s->servo[RF].position -= 65;
s->servo[LF].position -= A45;
dx_set_stance(s);
timer_delay(tdel);
s->servo[LL].position -= A90;
s->servo[RL].position -= A90;
dx_set_stance(s);
timer_delay(tdel);
s->servo[RF].position += 65;
s->servo[LF].position += A45;
dx_set_stance(s);
timer_delay(tdel);
// step right
s->servo[RF].position += A45;
s->servo[LF].position += 65;
dx_set_stance(s);
timer_delay(tdel);
s->servo[LL].position += A90;
s->servo[RL].position += A90;
dx_set_stance(s);
timer_delay(tdel);
s->servo[LF].position -= 65;
s->servo[RF].position -= A45;
dx_set_stance(s);
timer_delay(tdel);
}
// Reset Servos to 0°
void ResetServo(dx_stance_t *s)
{
printf("Reset Servo from Position 1: %d, 2: %d, 3: %d, 4:%d\n"
, s->servo[RL].position
, s->servo[LL].position
, s->servo[RF].position
, s->servo[LF].position);
s->servo[LL].position = 0;
s->servo[LF].position = 0;
timer_delay(tdel);
s->servo[RL].position = 0;
s->servo[RF].position = 0;
timer_delay(tdel);
}
const robot_t FLASH robot = {
60, // Temp max
{
// Servo 1-4 is limited to approx half the rotational range and half the
// torque. In practice you would set the range limits such that the
// robot didn't try and overstress the servos.
//
// The easiest way to do this is to use the supervisor to output the
// servo positions (using i) while manually moving them to the extreme
// values
{ 1, MODEL_AX12, -256, 256, TORQUE_MAX / 2 },
{ 2, MODEL_AX12, -256, 256, TORQUE_MAX / 2 },
{ 3, MODEL_AX12, -256, 256, TORQUE_MAX / 2 },
{ 4, MODEL_AX12, -256, 256, TORQUE_MAX / 2 },
{ DX_NOID }
}
};
static dx_adc_t imu_cal = {0, {
[0] = {256, (-308L - 22) * 256L, 192},
[1] = {256, (-308L - 24) * 256L, 192},
[2] = {256, (-308L - 4) * 256L, 192},
[3] = {256, (-308L - 11) * 256L, 256},
[4] = {256, (-308L - 13) * 256L, 256},
[5] = {0, 0, 128}
} };
int main(void) {
static dx_stance_t s = { 0, { [4] = { DX_NOID } } };
uint8_t irc = 0; // IR center
static dx_imu_t imu = { &imu_cal };
s.servo[0].id = DX_NOSERVO; // Prevent accidentally trying to init servo 0
for (int i = 1; i <= 4; i++) {
s.servo[i].id = i;
s.servo[i].position = 0;
}
printf("Set compliance\n");
for (int i = 1; i <= 4; i++) {
dx_set_compliance(i, 30, 1, 0);
}
// Print IMU outputs for one second
for (int i = 0; i < HZ; i++) {
// dx_get_huv_imu(120, &imu);
printf("(%d %d %d)\n", imu.accel.x, imu.accel.y, imu.accel.z);
timer_delay(1);
}
printf("Ready!\n");
for (int i = 1; i <= 4; i++) {
dx_set_torque_enable(i, 1);
}
while (1) {
if (button_pressed(BUTTON_UP)) {
printf("Button up pressed\n");
// first step
s.servo[RF].position += A45;
s.servo[LF].position += 65;
dx_set_stance(&s);
timer_delay(tdel);
s.servo[LL].position += A45;
s.servo[RL].position += A45;
dx_set_stance(&s);
timer_delay(tdel);
s.servo[LF].position -= 65;
s.servo[RF].position -= A45;
dx_set_stance(&s);
timer_delay(tdel);
printf("Start walking forward\n");
while ( irc < 130 ) {
WalkForward(&s);
irc = dx_get_IR_data_center(100);
}
printf("Obstacle detected: %d\n", irc);
ResetServo(&s);
// set back to 0
irc = 0;
} else if (button_pressed(BUTTON_DOWN)) {
ResetServo(&s);
}
fflush(); // Ensure all of the printf() is complete before looping, inherent delay
} // while end
}
const robot_t FLASH robot = {
60, // Temp max
{
{ 1, MODEL_AX12, -256, 256, TORQUE_MAX / 2 },
{ 2, MODEL_AX12, -256, 256, TORQUE_MAX / 2 },
{ 3, MODEL_AX12, -256, 256, TORQUE_MAX / 2 },
{ 4, MODEL_AX12, -256, 256, TORQUE_MAX / 2 },
{ 100, MODEL_AXS1 },
{ DX_NOID }
}
};
// Reset Servos to 0°
void ResetServo(dx_stance_t *s)
{
s->servo[LL].position = 0;
s->servo[LF].position = 0;
dx_set_stance(s);
timer_delay(tdel);
s->servo[RL].position = 0;
s->servo[RF].position = 0;
dx_set_stance(s);
timer_delay(tdel);
}
{ 0, { [6] = { DX_NOID } } };
27Loco wrote:Changed the ranges temporarily to SERVO_MIN and MAX. Just to be sure that this is not the problem. There is no line in the code where it stops unexpected.
Your Example code works perfectly so the AX12 are also working.
Dimitris wrote:I've just tried compiling the example file today but I get errors when running make. I'm using Linux and have make, gcc and avr-gcc installed.
CFLAGS_AVR = ${CFLAGS_ALL} -I${LIBAVR} -Os -ffreestanding \
-funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums \
-ffunction-sections
LDFLAGS_AVR = -I${LIBAVR} -Wl,--gc-sections,--relax
define AVR_COMPILE
${AVR_CC} $(call CFLAGS_${1}) $(call LDFLAGS_${1}) -o ${3} ${2}
endef