Bosch Seat Motor Java programming advice

So, my team has decided to use a bosch seat motor, with a DIO Kit to keep track of the motor’s position, from looking at previous articles here on Chief Delphi, I assume the following is correct, but as the wiring team is busy working on the robot wiring the kit, I can’t test the code for a while. Could you provide any assistance on whether or not I’m coding this correctly?

Counter motorCounter = new Counter(new DigitalInput(1)); // the DIO kit
private int position 0;

In the subsystem’s periodic:

public void periodic() {
if (going_forward) {
position += motorCounter.get();
} else {
position -= motorCounter.get();
motorCounter.reset(); // It seems as if many articles suggest doing this line every update frame,
why is that?
if(position > 90) // Assuming it’s in degrees and not radians
going_forward) = false;
going_forward) = true;

// Gets called by the command
public void moveArm()

From looking at previous posts here on Chief Delphi this seems correct, but as I don’t have access to the robot, I can’t test it out. And because my team is competing tomorrow, testing time will further be limited. Thank you for your time.


Just as a precursor, I’ve not used a DIO to keep track of motor position, but from your code, I think I have a pretty good idea of how it works.

Firstly, when posting code, wrap it in backticks (`) and CD will present the code nicely, eg



Much easier to read blocks of code.

I assume this is because you are adding/subtracting the value of the counter to the position each time, once it is added, you don’t need to add the same amount again (if that makes sense).

position will likely be the number of “ticks” the DIO kit has output, not an angle in degrees or radians. The vendor should say how many counts (ticks) per revolution the DIO kit outputs. From there, it’s just divide by 360 for degrees or 2pi for radians.

This should probably look like this:

if(position > 90) {
    going_forward = false;
} else {
    going_forward = true;

However I don’t think this is how you want to do it (although this depends on how the robot works). Looking at moveArm(), the arm will move in the direction of going_forward, which depends on its position, meaning you can’t reverse it.

What I think you want to do, is tell the arm which way to move, and based on that, either increment or decrement position.

For example, pass the direction you want to go to the moveArm method, ie

public void moveArm(boolean direction) {
  going_forward = direction; //Needed so the position tracker above knows what direction we are moving
  if(direction) {
  } else {

I would strongly recommend using an actual encoder to keep track of position, as the arm may not actually be moving in the direction you are telling it to, completely throwing out your position tracker. You can try to mitigate this by using limit switches, such that you know the position that the arm is in when you trigger them, and use them to “zero” your mechanism.

If anything is unclear or confusing, please let me know! :slight_smile:

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.