Notifications
Clear all

Keypad control not working

156 Posts
3 Users
24 Likes
9,542 Views
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 
Posted by: @robotbuilder

@kobusven95

My suggestion is the method I used in another project would work? Essentially each turn table position is given a step number. So for example if there were 9 positions you just list the step value for each position.

int stepPositions[] = {1365,1536,1707,1877,0,171,341,512,683};

Then you can get the desired position for each key value.

desiredPosition = stepPositions[keyValue];

You also have stored the currentPosition (which started at zero).

I supplied a function to do the calculations based on the currently available information. It's somewhat variable because he says the track layout can be established to match the step count rotation. It will definitely turn into an array in the long run, but for the time being it's "flexible" 🙂

stepCount = currentPosition - desiredPosition;
moveStepper(stepCount);

currentPosition = desiredPosition;  // update current position

Ideally it's not that easy. As I described earlier, you'd want to take make the SHORTEST turn to get to the new shed. So, if you were pointing at shed 1 and wanted to go to shed 8, you'd want to turn backwards past 0 to get there. Going 2-3-4-5-6-7-8 would mean going almost all the way around the circle.

And, with a BYJ stepper you DO NOT want to travel any farther than you need to 🙂

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2043
 

@will

Ideally it's not that easy. As I described earlier, you'd want to take make the SHORTEST turn to get to the new shed.

My duster example does just that. Another factor might be if you want to reverse the direction of the train before it enters a shed or exits the turn table.

 

int tool;
int Direction2;
int stepCount;

void MOVE_DISC_TO_TOOL_POSITION(){
  stepCount = abs(newAngle-currentAngle);
    
  if (newAngle < currentAngle){
    Direction2 = 0;
  }else{
    Direction2 = 1;
  }
  
  if (abs(newAngle-currentAngle)>2047){
    stepCount = 4096 - stepCount;
    if (Direction2==0){
      Direction2 = 1;
    }else{
      Direction2 = 0;
    }
  }

  moveStepper(stepCount,Direction2);

  currentAngle = newAngle;  // update current position
}

 

 

 

 


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 

@robotbuilder 

stepCount = currentPosition - desiredPosition;

If currentPosition is shed 1 and desired position is shed 2, then we should be moving clockwise ...

your array [1] contains 1536 and [2] 1707

So stepCount = (1536-1707) = -171 (or -30 degrees approximately)

If currentPosition is shed 1 and desired position is shed 8, then we should be moving widdershins ...

your array [1] contains 1536 and [8] 683

So stepCount = (1536-683 = 853 (or 150 degrees approximately)

 

Both results appear to be in the wrong direction. I know I'm missing something here, what is it ?

 

I was figuring to calculate the direction by finding the step count difference and then testing if the absolute value > 1012 (i.e. half a revolution) then reset it to (step count - 1012) to swap both direction and distance.

 

Anything seems possible when you don't know what you're talking about.


   
kobusven95 reacted
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 
Posted by: @robotbuilder

@will

Ideally it's not that easy. As I described earlier, you'd want to take make the SHORTEST turn to get to the new shed.

My duster example does just that. Another factor might be if you want to reverse the direction of the train before it enters a shed or exits the turn table.

Very sneaky robot builder, you edited the post and added the code after I started my reply 🙂 🙂

Anything seems possible when you don't know what you're talking about.


   
kobusven95 reacted
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 
Posted by: @robotbuilder

@will

  if (abs(newAngle-currentAngle)>2047){
    stepCount = 4096 - stepCount;
    if (Direction2==0){
      Direction2 = 1;
    }else{
      Direction2 = 0;
    }
  }

I'm curious, under what condition(s) could the difference between any two positions within the circle ever exceed one revolution ?

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2043
 

   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 

@robotbuilder @kobusven95

Well, the first thing I see is that there aren't 10 values in the shedPos array. That's maybe because you haven't read his description of how the shed locations are set. I supplied a sub to calculate those values given the selected key, but never got a confirmation or reply about whether they were correct.

Also, he's using the Arduino library "Stepper" which will move by a specified number of steps, so the moveStepper subroutine isn't required (sorry about that).

Also, the Stepper library decides whether to move clockwise or widdershins based the sign of the number of steps to move, so +10 moves 10 steps clockwise and -10 moves 10 steps widdershins.

Also, he's declared a variable stepsPerRevolution which is set to the step count for a full rotation. Please use it instead of the literal numbers. It'll be easier to make a global change from 2048 to the 4096 that you've suggested before if we ever need to. It will also helping the case that the steer motor is upgraded to a before motor with a different step angle. Nicer to just have 1 place that needs to be changed.

I'm not sure here, did you want me to adapt that code into the sketch to use the keypad ? I'll be happy to do so if you would prefer.

I have been sending bits out as tidbits to be added to the main sketch because I don't know who has the current version (if anybody). I think we should adopt a "blessed by all parties" version and then work on separate parts to avoid duplication of effort (and the waste of time thereby).

I mentioned that to kobusven95 but never got an answer.

So, I'm kind of loathe to make any more changes until we're all satisfied that we have a single accepted version of the sketch.

How do you think we should proceed ?

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2043
 

   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2535
 
Posted by: @robotbuilder FYI @kobusven95

@will

So, I'm kind of loathe to make any more changes until we're all satisfied that we have a single accepted version of the sketch.

How do you think we should proceed ?

This is all just puzzle solving to me and has its own reward. You are probably better at code logic without the hardware to test it. Not much I can do without having the keypad and LCD display shield to experiment with. I am sure if we had the actual hardware it would be easier and more satisfying to give a complete solution to whatever was required.

Yeah, and if we were all three in the same room with ahh that stuff we'd be finished in a day 🙂

I was working out the logic. You can have as many or as few sheds as you like. I intended another version with 8 sheds and an exit with the same layout @kobusven95 wanted. I was also thinking about using the turn table to reverse the direction of the train.

I think reversing the train is already covered. Seems to me that shed 9 is 180 degrees (i.e reversal).

Also I asked in a previous post why you're checking for values > 4096 (did you mean 2048 ?).

But I do check for a difference of > 2047
abs(newAngle-oldAngle)>2047

It has something to do with the cross over where 4096 resets to zero. Moving over that zero point means the angle measurement flicks between a low value to a very high value anticlockwise and from a high value to a low value going clockwise. So I am checking for two scenarios to decide if to move clockwise or anticlockwise as the shortest direction. I am in fact math challenged so maybe it works by luck. I will try and figure out my logic from back then when not so tired.

Since the Stepper class being used has no direction except for the sign of the step count, we just need to test the absolute value of the difference between the start and end sheds against half revolution. If it's greater than that we reverse the sign and reset the step count to (stepsPerRevolution-difference). Same effect as your code except that it integrates the direction by using the sign. So no move will be greater than stepsPerRevolution/2 and be no less than -stepsPerRevolution/2. So more like:

long stepCount = (shedPos[newShed]-shedPos[currentShed]);

if (abs(stepCount)>stepsPerRevolution/2)

    stepCount = (stepCount<0) ? stepCount+stepsPerRevolution/2 : stepCount-stepsPerRevolution/2;

StepMot.step(stepCount);

position = shedPos[currentShed];

 

I think, it's way past my bed time 🙂

Anything seems possible when you don't know what you're talking about.


   
ReplyQuote
(@kobusven95)
Member
Joined: 2 years ago
Posts: 60
Topic starter  

@robotbuilder according to the stepper documentation, it is only 2048.


   
ReplyQuote
(@kobusven95)
Member
Joined: 2 years ago
Posts: 60
Topic starter  

@will  "but never got a confirmation or reply about whether they were correct." I have answered about the "working on one sketch" thinking that we should wait to here from RB first to see what he have, before committing changes.

 


   
ReplyQuote
(@kobusven95)
Member
Joined: 2 years ago
Posts: 60
Topic starter  

@will @robotbuilder   Hi Guys, just busy with training today, and the rest of the week. Only available after 17:00 SAST. But I will have a catch up during the day. Is it possible to get all that should be added to the script into one place? I am not sure any longer that should and should not be tested and added. Then I will go ahead and add and test, if it is OK with you guys?


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2043
 
Posted by: @kobusven95

@robotbuilder according to the stepper documentation, it is only 2048.

I must have been using it in half step mode.

Step angle:
Half step mode: 0.0879°
Full step mode: 0.176°
Steps per revolution
Half step mode: 4096
Full step mode: 2048

 


   
kobusven95 reacted
ReplyQuote
(@kobusven95)
Member
Joined: 2 years ago
Posts: 60
Topic starter  

@will @robotbuilder "reverse the direction of the train before it enters a shed or exits the turn table." this is correct - the 180 degree turn solves that. shall I start from the bottom and copy all of the bits and pieces out on the script? Going to create a new version.


   
ReplyQuote
(@kobusven95)
Member
Joined: 2 years ago
Posts: 60
Topic starter  

@will "I expect you'll eventually need to change that BYJ stepper motor for something faster like a NEMA." does not rely need to, as the speed is OK - remember if it is too fast, it becomes unrealistic. There are a lot of guys who will crush you of your display is not utterly realistic, unfortunately i am not one of the, but it is cool to see the things happening in a realistic way. 🙂


   
ReplyQuote
Page 6 / 11