View unanswered posts | View active topics It is currently Tue Sep 16, 2014 10:22 pm






Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
Teleop Code With Magnet? Help! 
Author Message
Rookie

Joined: Tue Nov 30, 2010 9:24 pm
Posts: 11
Post Teleop Code With Magnet? Help!
Ok, So im trying to write a teleop code that includes a magnet sensor, i have all of the teleop code right, it all works except the mag sensor code, whenever i add it to the teleop code, the robot goes haywire and robotc freezes. Here is my code, any help would be deeply appreciated.


#pragma config(Hubs, S1, HTMotor, HTMotor, HTMotor, HTServo)
#pragma config(Sensor, S2, Touch, sensorHiTechnicTouchMux)
#pragma config(Sensor, S3, Mag, sensorHiTechnicMagnetic)
#pragma config(Motor, motorA, motorA, tmotorNormal, openLoop, reversed)
#pragma config(Motor, motorB, motorB, tmotorNormal, PIDControl, encoder)
#pragma config(Motor, mtr_S1_C1_1, LeftD, tmotorNormal, openLoop, reversed, encoder)
#pragma config(Motor, mtr_S1_C1_2, RightD, tmotorNormal, openLoop, encoder)
#pragma config(Motor, mtr_S1_C2_1, Harvester, tmotorNormal, openLoop, reversed)
#pragma config(Motor, mtr_S1_C2_2, Elevator, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C3_1, Arm, tmotorNormal, openLoop)
#pragma config(Motor, mtr_S1_C3_2, motorI, tmotorNormal, openLoop)
#pragma config(Servo, srvo_S1_C4_1, Sorter, tServoStandard)
#pragma config(Servo, srvo_S1_C4_2, Door, tServoStandard)
#pragma config(Servo, srvo_S1_C4_3, Magdoor, tServoStandard)
#pragma config(Servo, srvo_S1_C4_4, Regdoor, tServoStandard)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tele-Operation Mode Code Template
//
// This file contains a template for simplified creation of an tele-op program for an FTC
// competition.
//
// You need to customize two functions with code unique to your specific robot.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////

#include "JoystickDriver.c" //Include file to "handle" the Bluetooth messages.


/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// initializeRobot
//
// Prior to the start of tele-op mode, you may want to perform some initialization on your robot
// and the variables within your program.
//
// In most cases, you may not have to add any code to this function and it will remain "empty".
//
/////////////////////////////////////////////////////////////////////////////////////////////////////

void initializeRobot()
{
servo[Magdoor] = 0;
servo[Regdoor] = 0;

return;
}


/////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Main Task
//
// The following is the main code for the tele-op robot operation. Customize as appropriate for
// your specific robot.
//
// Game controller / joystick information is sent periodically (about every 50 milliseconds) from
// the FMS (Field Management System) to the robot. Most tele-op programs will follow the following
// logic:
// 1. Loop forever repeating the following actions:
// 2. Get the latest game controller / joystick settings that have been received from the PC.
// 3. Perform appropriate actions based on the joystick + buttons settings. This is usually a
// simple action:
// * Joystick values are usually directly translated into power levels for a motor or
// position of a servo.
// * Buttons are usually used to start/stop a motor or cause a servo to move to a specific
// position.
// 4. Repeat the loop.
//
// Your program needs to continuously loop because you need to continuously respond to changes in
// the game controller settings.
//
// At the end of the tele-op period, the FMS will autonmatically abort (stop) execution of the program.
//
/////////////////////////////////////////////////////////////////////////////////////////////////////

void task Servos
{
while(true)
{
if(joy1Btn(6))
{
servo[Regdoor] = 0;
}
else
{
servo[Regdoor] = 148;
}

if(joy1Btn(5))
{
servo[Magdoor] = 127;
}
else
{
servo[Magdoor] = 255;
}
if(joy1Btn(8))
{
servo[Door] = 80;
}
else
{
servo[Door] = 148;
}

wait1Msec(1);
}}

void task Magnet

{

if(SensorValue[Mag] < 650)
{
servo[Sorter] = 127;
}
else if(SensorValue[Mag] > 662)
{
servo[Sorter] = 255;
}
else
{
servo[Sorter] = 0;
}


wait1Msec(1);
}

task main()
{
initializeRobot();

waitForStart(); // wait for start of tele-op phase
{
StartTask(Servos);
}
{
StartTask(Magnet);
}
while (true)
{
getJoystickSettings(joystick);
{
{
motor[LeftD] = joystick.joy1_y1;
motor[RightD] = joystick.joy1_y2;
motor[Elevator] = joystick.joy2_y2;
motor[Arm] = joystick.joy2_y1;
}


}
}}


Fri Feb 11, 2011 12:41 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Teleop Code With Magnet? Help!
You probably need a wait1Msec in the main task while loop. You are currently running a tight loop that may not give other tasks a chance to run. Also, you need to translate the joystick range to the motor range because the joystick range is -128 to 127 and the motor range is -100 to 100.
Try using the following code:
Code:
//
// The DEAD_BAND macro makes sure non-zero-centered joystick won't move the robot.
//
#define DEAD_BAND(n, t) ((abs(n) < (t))? 0: (n))
//
// The MOTOR_POWER macro translates the joystick range of (-128 to 127) to the motor
// power range of (-100 to 100) after applying DEAD_BAND to the joystick value.
//
#define MOTOR_POWER(n)  (DEAD_BAND(n, 15)*100/128)

motor[LeftD] = MOTOR_POWER(joystick.joy1_y1);
motor[RightD] = MOTOR_POWER(joystick.joy1_y2);
motor[Elevator] = MOTOR_POWER(joystick.joy2_y2);
motor[Arm] = MOTOR_POWER(joystick.joy2_y1);


Fri Feb 11, 2011 1:33 pm
Profile
Rookie

Joined: Tue Nov 30, 2010 9:24 pm
Posts: 11
Post Re: Teleop Code With Magnet? Help!
Ok, so i tried the wait1Msec, with different munbers, and it still didnt work, Did i do something wrong in creating its own seperate task or could my code be fallible. Any help or code would be appreciated :)


Fri Feb 11, 2011 9:14 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Teleop Code With Magnet? Help!
You don't need tasks to accomplish what you want to do. In fact, it will be much simpler if you convert all your tasks into subroutines and do something like this:
Code:
//
// The DEAD_BAND macro makes sure non-zero-centered joystick won't move the robot.
//
#define DEAD_BAND(n, t) ((abs(n) < (t))? 0: (n))
//
// The MOTOR_POWER macro translates the joystick range of (-128 to 127) to the motor
// power range of (-100 to 100) after applying DEAD_BAND to the joystick value.
//
#define MOTOR_POWER(n)  (DEAD_BAND(n, 15)*100/128)

void processServos()
{
    if(joy1Btn(6))
    {
        servo[Regdoor] = 0;
    }
    else
    {
        servo[Regdoor] = 148;
    }

    if(joy1Btn(5))
    {
        servo[Magdoor] = 127;
    }
    else
    {
        servo[Magdoor] = 255;
    }

    if(joy1Btn(8))
    {
        servo[Door] = 80;
    }
    else
    {
        servo[Door] = 148;
    }
}

void processMagnet()
{
    if(SensorValue[Mag] < 650)
    {
        servo[Sorter] = 127;
    }
    else if(SensorValue[Mag] > 662)
    {
        servo[Sorter] = 255;
    }
    else
    {
        servo[Sorter] = 0;
    }
}

task main()
{
    initializeRobot();
    waitForStart(); // wait for start of tele-op phase

    while (true)
    {
        getJoystickSettings(joystick);
        processServos();
        processMagnet();
        motor[LeftD] = MOTOR_POWER(joystick.joy1_y1);
        motor[RightD] = MOTOR_POWER(joystick.joy1_y2);
        motor[Elevator] = MOTOR_POWER(joystick.joy2_y2);
        motor[Arm] = MOTOR_POWER(joystick.joy2_y1);
        wait1Msec(10);
    }
}


Fri Feb 11, 2011 10:25 pm
Profile
Rookie

Joined: Sun Dec 05, 2010 11:58 am
Posts: 28
Post Re: Teleop Code With Magnet? Help!
Yeah, we had a few problems with multiple tasks. When we put everything into one task, they were solved.
There's really no reason to have extra tasks for the servos and magnets. It doesn't make them run faster, and unless the code gets incredibly long and complex, the cycle rate is pretty fast.
The only thing we use multiple tasks for is monitoring the output of sensors.


Fri Feb 25, 2011 2:09 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Teleop Code With Magnet? Help!
jorge_the_awesome wrote:
The only thing we use multiple tasks for is monitoring the output of sensors.

Even that you can do without task. Unless you are comfortable with multi-tasking programming (protecting shared data with hogCPU/releaseCPU), you may want to avoid using task. So far, I haven't found a need to create separate tasks.


Fri Feb 25, 2011 2:25 pm
Profile
Rookie

Joined: Sun Dec 05, 2010 11:58 am
Posts: 28
Post Re: Teleop Code With Magnet? Help!
Well, you need another task if you want to average the last set of values, in order to keep the time spacing between the values constant, right?


Fri Feb 25, 2011 2:58 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Teleop Code With Magnet? Help!
I don't know what averaging you are doing, but if you are integrating, you can explicitly read the timestamps between loops for calculating the integration.


Fri Feb 25, 2011 3:15 pm
Profile
Rookie

Joined: Sun Dec 05, 2010 11:58 am
Posts: 28
Post Re: Teleop Code With Magnet? Help!
I don't know integrals....
For the IR sensor, we had problems with rapidly fluctuating values, so we tried sampling the values ever n milliseconds and averaging the last m values. To keep n constant, we used a task.
I guess we could have used a function, but it would have been harder, I think.


Fri Feb 25, 2011 3:36 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Teleop Code With Magnet? Help!
That's interesting... My experience with the IR sensor is that it is usually not noisy, so there isn't really a need to process the data before using them (except for interpolation between zones if you want to improve the accuracy). Occasionally, I may lose the IR signal (i.e. getting a zero), but I never get fluctuating numbers. Do you have a lot of IR sources in the room? Even if you have fluctuating numbers on the IR seeker, I don't think averaging them is the answer. And if you do average them, it is not a time domain average so a constant time period is not really necessary. So my conclusion is still: you can avoid using tasks.


Fri Feb 25, 2011 7:20 pm
Profile
Rookie

Joined: Sun Dec 05, 2010 11:58 am
Posts: 28
Post Re: Teleop Code With Magnet? Help!
Yeah, the averaging was to get rid of zeroes.
I guess the values didn't fluctuate that much.
We haven't used the IR in a long time, because its not accurate enough for autonomous.


Wed Mar 02, 2011 9:27 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Teleop Code With Magnet? Help!
It could be accurate if you use the narrower zone. See the following thread.
http://robotc.net/forums/viewtopic.php? ... acy#p11686


Thu Mar 03, 2011 1:24 am
Profile
Rookie

Joined: Tue Oct 19, 2010 1:16 am
Posts: 9
Post Re: Teleop Code With Magnet? Help!
Sorry to bring this back up.. but I have been reading around, trying to get my magnetic sensor to work during teleop. I have tryed a few of the things said around this thread, but when I go to run it, I get a yello x, saying *Warning*:Unreferenced function 'processMagnet'. Is there something Im forgetting to do?


Fri Mar 04, 2011 1:34 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Teleop Code With Magnet? Help!
The "Unreferenced function" warning is benign. It just means that the file you have included contains a function that is never used by any code which is often the case if you include the driver library where it has many functions but you are not using them all.


Fri Mar 04, 2011 5:13 am
Profile
Rookie

Joined: Sun Dec 05, 2010 11:58 am
Posts: 28
Post Re: Teleop Code With Magnet? Help!
I just realized that we had the same problem. After a certain number of tasks in teleOp, the servos go haywire and start moving randomly between 2 points. No servos were referenced from multiple tasks. Without changing the code, the problem was solved when we copy-pasted everything into one task.

Does anyone know what causes it? Could it be the asynchronous commands sent to the same controller?

But then why did our first teleOp work with multiple tasks?


Fri Mar 04, 2011 8:07 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 19 posts ]  Go to page 1, 2  Next

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.