Why is my nested while loop not working? - while-loop

I'm currently programming in RobotC, for a Vex 2.0 Cortex. I'm using encoders to make my robot go straight.
This is my code:
void goforwards(int time)
{
int Tcount = 0;
int speed1 = 40;
int speed2 = 40;
int difference = 10;
motor[LM] = speed1;
motor[RM] = speed2;
while (Tcount < time)
{
nMotorEncoder[RM] = 0;
nMotorEncoder[LM] = 0;
while(nMotorEncoder[RM]<1000)
{
int REncoder = -nMotorEncoder[RM];
int LEncoder = -nMotorEncoder[LM];
if (LEncoder > REncoder)
{
motor[LM] = speed1 - difference;
motor[RM] = speed2 + difference;
if (motor[RM]<= 0)
{
motor[RM] = 40;
motor[LM] = 40;
}
}
if (LEncoder < REncoder)
{
motor[LM] = speed1 + difference;
motor[RM] = speed2 - difference;
if (motor[RM]<= 0)
{
motor[RM] = 40;
motor[LM] = 40;
}
Tcount ++;
}
}
}
}
task main()
{
goforwards(10);
}
For reference, these are my Pragma settings:
#pragma config(I2C_Usage, I2C1, i2cSensors)
#pragma config(Sensor, dgtl2, , sensorDigitalIn)
#pragma config(Sensor, dgtl7, , sensorDigitalOut)
#pragma config(Sensor, I2C_1, , sensorQuadEncoderOnI2CPort, , AutoAssign )
#pragma config(Sensor, I2C_2, , sensorQuadEncoderOnI2CPort, , AutoAssign )
#pragma config(Motor, port1, RM, tmotorVex393_HBridge, openLoop, reversed, encoderPort, I2C_2)
#pragma config(Motor, port10, LM, tmotorVex393_HBridge, openLoop, encoderPort, I2C_1)
When I excecute the code, the Robot's encoder values are very close, but the robot stops moving when they reach 1000. I thought the code I wrote should return the values of the encoders back to 0 after they reach 1 thousand, and thereby the code should re-iterate in the shell loop 10 times (in this case). What have I done wrong?

You are updating Tcount at the wrong place. Do it Just at the end of the outer loop.
What you have written now makes Tcount increase everytime it moves ahead. By the time it reaches 1000 steps, Tcount is already 1000.
Your times is 10. So `Tcount >= time and it wont enter the outer while loop again.

It seems that the Control Variable of the inner loop (i.e. nMotorEncoder[RM]) is never updated, this means that the inner loop will iterate forever. That is, you will never go back to the body of external loop

Related

How to stop the search in CP Optimizer if the upper bound doesn't get better after x seconds

I'm wondering if there exists a way in CP Optimizer 12.10 to stop the search if the upper bound (of a minimization problem) doesn't get better after "x" seconds. This would be especially helpful when trying to solve a difficult instance of an NP-Hard problem and the search is stuck in one incumbent solution.
I know there exist some parameters to control the search as cp.param.TimeLimit (I'm using that) or cp.param.FailLimit but that is not that I need for my problem.
Any help would be highly appreciated.
what you can do is use a main block (flow control) if you rely on the OPL API and in this main block you give a time limit of x seconds, then you get the bound and the current objective, you save them if you think you should go further or stop if you're ok with the solution.
Let me share a full example out of the lifegame example in the OPL product. And remember John Conway once again.
using CP;
int n=20;
int Half=n div 2;
range FirstHalf = 1..Half;
range LastHalf = n-Half+1..n;
range States = 0..1;
range Bord = 0..(n+1);
range Interior = 1..n;
range obj = 0..(n*n);
tuple neighbors {
int row;
int col;
}
{neighbors} Neighbor =
{<(-1),(-1)>,<(-1),0>,<(-1),1>,<0,(-1)>,<0,1>,<1,(-1)>,<1,0>,<1,1>};
dvar int Life[Bord][Bord] in States;
dvar int Obj in obj;
dvar int x[0..(n+2)*(n+2)-1];
maximize Obj;
subject to {
forall(i,j in Bord) Life[i][j]==x[i*(n+2)+j];
//ct1:
Obj == sum( i , j in Bord )
Life[i][j];
forall( i , j in Interior ) {
ct21:
2*Life[i][j] - sum( nb in Neighbor )
Life[i+nb.row][j+nb.col] <= 0;
ct22:
3*Life[i][j] + sum( nb in Neighbor )
Life[i+nb.row][j+nb.col] <= 6;
forall( ordered n1 , n2 , n3 in Neighbor ) {
ct23:
-Life[i][j]+Life[i+n1.row][j+n1.col]
+Life[i+n2.row][j+n2.col]
+Life[i+n3.row][j+n3.col]
-sum( nb in Neighbor : nb!=n1 && nb!=n2 && nb!=n3 )
Life[i+nb.row][j+nb.col] <= 2;
}
}
forall( j in Bord ) {
ct31:
Life[0][j] == 0;
ct32:
Life[j][0] == 0;
ct33:
Life[j][n+1] == 0;
ct34:
Life[n+1][j] == 0;
}
forall( i in Bord : i<n ) {
ct41:
Life[i][1]+Life[i+1][1]+Life[i+2][1] <= 2;
ct42:
Life[1][i]+Life[1][i+1]+Life[1][i+2] <= 2;
ct43:
Life[i][n]+Life[i+1][n]+Life[i+2][n] <= 2;
ct44:
Life[n][i]+Life[n][i+1]+Life[n][i+2] <= 2;
}
ct5:
sum( i in FirstHalf , j in Bord )
Life[i][j] >=
sum( i in LastHalf , j in Bord )
Life[i][j];
ct6:
sum( i in Bord , j in FirstHalf )
Life[i][j] >=
sum( i in Bord , j in LastHalf )
Life[i][j];
}
main
{
thisOplModel.generate();
cp.param.timelimit=10;
var obj=0;
var bound=0;
var oldobj=0;
var oldbound=0;
while (1==1)
{
cp.solve();
obj=cp.getObjValue();
bound=cp.getObjBound();
writeln("bound and obj =",bound," ",obj);
if (Opl.abs((oldobj-oldbound-obj+bound))<=0) break;
oldobj=obj;
oldbound=bound;
}
}
which gives
bound and obj =398 181
bound and obj =398 180
bound and obj =398 180
// Script execution finished with status 1.

Sprite Smooth movement and facing position according to movement

i'm trying to make this interaction with keyboard for movement using some sprites and i got stuck with two situations.
1) The character movement is not going acording to the animation itself (it only begin moving after one second or so while it's already being animated). What i really want it to do is, to move without a "initial acceleration feeling" that i get because of this problem
2) I can't think of a way to make the character face the position it should be facing when the key is released. I'll post the code here, but since it need images to work correctly and is not so small i made a skecth available at this link if you want to check it out: https://www.openprocessing.org/sketch/439572
PImage[] reverseRun = new PImage [16];
PImage[] zeroArray = new PImage [16];
void setup(){
size(800,600);
//Right Facing
for(int i = 0; i < zeroArray.length; i++){
zeroArray[i] = loadImage (i + ".png");
zeroArray[i].resize(155,155);
}
//Left Facing
for( int z = 0; z < reverseRun.length; z++){
reverseRun[z] = loadImage ( "mirror" + z + ".png");
reverseRun[z].resize(155,155);
}
}
void draw(){
frameRate(15);
background(255);
imageMode(CENTER);
if(x > width+10){
x = 0;
} else if (x < - 10){
x = width;}
if (i >= zeroArray.length){
i = 3;} //looping to generate constant motiion
if ( z >= reverseRun.length){
z = 3;} //looping to generate constant motiion
if (isRight) {
image(zeroArray[i], x, 300);
i++;
} //going through the images at the array
else if (isLeft) {
image(reverseRun[z],x,300);
z++;
} going through the images at the array
else if(!isRight){
image(zeroArray[i], x, 300);
i = 0; } //"stoped" sprite
}
}
//movement
float x = 300;
float y = 300;
float i = 0;
float z = 0;
float speed = 25;
boolean isLeft, isRight, isUp, isDown;
void keyPressed() {
setMove(keyCode, true);
if (isLeft ){
x -= speed;
}
if(isRight){
x += speed;
}
}
void keyReleased() {
setMove(keyCode, false);
}
boolean setMove(int k, boolean b) {
switch (k) {
case UP:
return isUp = b;
case DOWN:
return isDown = b;
case LEFT:
return isLeft = b;
case RIGHT:
return isRight = b;
default:
return b; }
}
The movement problem is caused by your operating system setting a delay between key presses. Try this out by going to a text editor and holding down a key. You'll notice that a character shows up immediately, followed by a delay, followed by the character repeating until you release the key.
That delay is also happening between calls to the keyPressed() function. And since you're moving the character (by modifying the x variable) inside the keyPressed() function, you're seeing a delay in the movement.
The solution to this problem is to check which key is pressed instead of relying solely on the keyPressed() function. You could use the keyCode variable inside the draw() function, or you could keep track of which key is pressed using a set of boolean variables.
Note that you're actually already doing that with the isLeft and isRight variables. But you're only checking them in the keyPressed() function, which defeats the purpose of them because of the problem I outlined above.
In other words, move this block from the keyPressed() function so it's inside the draw() function instead:
if (isLeft ){
x -= speed;
}
if(isRight){
x += speed;
}
As for knowing which way to face when the character is not moving, you could do that using another boolean value that keeps track of which direction you're facing.
Side note: you should really try to properly indent your code, as right now it's pretty hard to read.
Shameless self-promotion: I wrote a tutorial on user input in Processing available here.

ROBOTC - Programming Autonomous With Integrated Encoders

I have an X Drive that is coded in ROBOTC. My team and I have the integrated motor encoders already on the robot (for the autonomous period). However the code for them to run is incorrect. The current autonomous code is below. When I run it, it just goes forward forever and at different speeds.
I have looked at multiple tutorials, but none of them work. Does anyone have the code to make the motors (393 Motors) go at a count of 720?
#pragma config(I2C_Usage, I2C1, i2cSensors)
#pragma config(Sensor, I2C_1, sensorQuadEncoderOnI2CPort, AutoAssign)
#pragma config(Motor, port2, FL, tmotorVex393_MC29, PIDControl, encoderPort, I2C_1)
#pragma config(Motor, port3, BR, tmotorVex393_MC29, PIDControl, reversed, encoderPort, I2C_1)
#pragma config(Motor, port8, BL, tmotorVex393_MC29, PIDControl, encoderPort, I2C_1)
#pragma config(Motor, port9, FR, tmotorVex393_MC29, PIDControl, reversed, encoderPort, I2C_1)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
task main()
{
// Autonomous with Integrated Encoders
nMotorPIDSpeedCtrl[FL] = mtrSpeedReg;
nMotorPIDSpeedCtrl[FR] = mtrSpeedReg;
nMotorPIDSpeedCtrl[BL] = mtrSpeedReg;
nMotorPIDSpeedCtrl[BR] = mtrSpeedReg;
//Clears motor values
nMotorEncoder[FL] = 0;
nMotorEncoder[FR] = 0;
nMotorEncoder[BL] = 0;
nMotorEncoder[BR] = 0;
//Forward
motor[FL] = 63;
motor[FR] = 63;
motor[BL] = 63;
motor[BR] = 63;
while(nMotorEncoder[FL] < 720) {
}
//Clears motor values
nMotorEncoder[FL] = 0;
nMotorEncoder[FR] = 0;
nMotorEncoder[BL] = 0;
nMotorEncoder[BR] = 0;
}
You need to explicitly stop the motors (not just zero out the encoders) after the while loop. Otherwise the robot doesn't know to stop; it just knows that it passed the encoder target.
So this code should work for you:
//Clears motor values
nMotorEncoder[FL] = 0;
nMotorEncoder[FR] = 0;
nMotorEncoder[BL] = 0;
nMotorEncoder[BR] = 0;
motor[FL] = 63;
motor[FR] = 63;
motor[BL] = 63;
motor[BR] = 63;
//Forward
while(nMotorEncoder[FL] < 720) {
}
//stops motors
motor[FL] = 0;
motor[FR] = 0;
motor[BL] = 0;
motor[BR] = 0;
//Clears motor encoder values
nMotorEncoder[FL] = 0;
nMotorEncoder[FR] = 0;
nMotorEncoder[BL] = 0;
nMotorEncoder[BR] = 0;

STM32F4 PWM ramp

I'm working on a project where I want to ramp the pwm duty cycle from 0 to 50%. My period is 16000 counts or 1ms (16MHz default timer count). For some reason, instead of updating the duty cycle each period, it updates it much slower. I wonder if it's because I'm calculating the new duty cycle within the timer interrupt? Here's what I'm using:
void TIM4_IRQHandler()
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
if (loop <= 8000) {
TIM4 -> CCR1 = CCR_i;
uint16_t y = CCR_i;
CCR_i = y + 1;
int x = loop;
loop = x + 1;
}
if (loop == 8001) {
TIM4 -> CCR1 = 0;
uint16_t x = CCR_i;
CCR_i = x + 1;
int c = loop;
loop = c + 1;
}
if (loop > 8001) {
int t;
for(t = 0; t < 10; t++){
// wait
}
GPIO_SetBits(GPIOG, GPIO_Pin_8);
//Stop2();
TIM_ITConfig(TIM4, TIM_IT_Update, DISABLE);
NVIC_DisableIRQ(TIM4_IRQn);
}
}
}
blast, it looks like I was being silly - the timer is doing exactly what I want it to - it just takes 8 seconds with a period of 1ms to ramp to a pulsewidth of 500us adding 62.5ns per period.

My program doesn't get into my second for loop

While doing some work for my lab in university
I am creating this function where there is a for loop inside another one.
It is not important to know what the method is used for. I just can't figure out why the program doesn't enter the second for loop. This is the code:
public void worseFit(int[] array){
int tempPosition = -1;
int tempWeight = 101 ;
for (int x = 0; x < (array.length - 1); x++){
if (allCrates.getSize() < 1){
Crate crate = new Crate();
crate.addWeight(array[0]);
allCrates.add(crate);
} else{
for( int i = 1; i < (allCrates.getSize() - 1); i++ ){
Crate element = allCrates.getElement(i);
int weight = element.getTotalWeight();
if (weight < tempWeight){
tempWeight = weight;
tempPosition = i;
Crate crate = new Crate();
if (weight + tempWeight <= 100){
crate.addWeight(weight + tempWeight);
allCrates.setElement(i, crate);
} else {
crate.addWeight(weight);
allCrates.setElement(allCrates.getSize(), crate);
} // if
} // if
} // for
} // if
} // for
} // worseFit
Once the program enters the else part of the code it goes straight
away back to the beginning of the first for loop.
Would anyone know how to solve this problem?
There seems to be some discrepancies with the expected values of allCrates.getSize().
If allCrates.getSize() returns 2, it will go to the second for loop, but not run it, as i < allCrates.getSize() - 1 will result in false
You might want to use <= instead of <
Initialize the variable i in your second loop to 0 instead of 1. Because if your getSize() returns 1 the it will not enter the if part and after entering the else part the for loop condition will evaluate to false and hence your for loop will not be executed.