Post your NXJ projects, project ideas, etc here!
Moderators: roger, 99jonathan, imaqine
by abalis3 » Fri Jan 20, 2012 9:05 pm
Ok so I'm not very experienced and I wasn't taught java very well so lets just say I don't know alot. Anyway, I was trying to make a program to control a pan tilt bot (Motor.A) is pan and (Motor.B) is tilt. I had the program working with just dragging the mouse back and forth on the gray box in the gui and then decided to make some buttons to see what I could get them to do. The nxt program quit button works and I'm trying to just debug one button to start being the "calibrate Tacho" button. for the sake of simplicity I'm just trying to get that button to make the LCD say "it works", but it doesn't. Ive been mixing numbers around and I think theres one off somewhere I just can't seem to find it. I also apologize to those of you who are real sticklers when it comes to indenting but im working on it. also in the program on the nxt itself the variable pan is just used for the input from the computer. It is not only for pan. Im not even sure why I did that.:
- Code: Select all
import lejos.pc.comm.*;
import java.io.*;
import javax.swing.*;
public class pcmainclass {
public static void main(String[] args){
NXTConnector connA = new NXTConnector();
connA.connectTo();
DataOutputStream dos = connA.getDataOut();
try{
;
Gui mainWindow = new Gui();
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setSize(500,570);
mainWindow.setResizable(false);
mainWindow.setVisible(true);
int xRecieved = mainWindow.getXAxis();
int yRecieved = mainWindow.getYAxis();
while(xRecieved != 500){
xRecieved = mainWindow.getXAxis();
if((xRecieved >= 200 && xRecieved <= 280) || (xRecieved <= 0) || (xRecieved >= 490 && xRecieved !=500 && xRecieved != 61000
&& xRecieved != 62000 && xRecieved != 63000 && xRecieved != 64000 && xRecieved != 65000)){
dos.writeInt(0);
dos.flush();
}
else if(xRecieved >= 281){
dos.writeInt((xRecieved - 265));
dos.flush();
}
else if(xRecieved <= 199){
dos.writeInt(0 - ((210 - xRecieved)));
dos.flush();
}
else if(xRecieved == 500){
dos.writeInt(500);
dos.flush();
}
else if (xRecieved == 61000 || xRecieved == 62000 || xRecieved == 63000 || xRecieved == 64000 || xRecieved == 65000){
dos.writeInt(xRecieved);
dos.flush();
}
yRecieved = mainWindow.getYAxis();
if((yRecieved >= 180 && yRecieved <= 260) || (yRecieved <= 0) || (yRecieved >= 450 && yRecieved !=500)){
dos.writeInt(60000);
dos.flush();
}
else if(yRecieved >= 261){
dos.writeInt((yRecieved - 245) * 100);
dos.flush();
}
else if(yRecieved <= 179){
dos.writeInt((0 - (195 - yRecieved)) * 100);
dos.flush();
}
}dos.writeInt(500);
dos.flush();
}catch(Exception e){}
}
}
- Code: Select all
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Gui extends JFrame{
private JPanel mousepanel;
private JPanel pane;
private JLabel statusbar;
private int xAxis;
private int yAxis;
private int xSpeed;
private int ySpeed;
private JButton quitProgram;
private JButton resetTacho;
private JButton preset1;
private JButton preset2;
private JButton preset3;
private JButton preset4;
public Gui(){
super("Motor Control Program");
Dimension panelsize = new Dimension(485,445);
pane = new JPanel(new GridBagLayout());
GridBagConstraints panelConstraints = new GridBagConstraints();
panelConstraints.gridx = 0;
panelConstraints.gridy = 0;
panelConstraints.gridwidth = 3;
panelConstraints.weighty = 0;
panelConstraints.anchor = GridBagConstraints.CENTER;
mousepanel = new JPanel();
mousepanel.setBackground(Color.GRAY);
mousepanel.setPreferredSize(panelsize);
mousepanel.setMinimumSize(panelsize);
pane.add(mousepanel, panelConstraints);
//------------------------------------------
GridBagConstraints statusConstraints = new GridBagConstraints();
statusConstraints.gridx = 0;
statusConstraints.gridy = 1;
statusConstraints.gridwidth = 3;
statusConstraints.anchor = GridBagConstraints.CENTER;
statusConstraints.fill = GridBagConstraints.HORIZONTAL;
statusbar = new JLabel("Click (and drag) above to move light");
pane.add(statusbar, statusConstraints);
//-------------------------------------------
GridBagConstraints preset1Constraints = new GridBagConstraints();
preset1Constraints.gridx = 0;
preset1Constraints.gridy = 2;
preset1Constraints.gridwidth = 1;
preset1Constraints.anchor = GridBagConstraints.CENTER;
preset1Constraints.fill = GridBagConstraints.BOTH;
preset1Constraints.ipadx = 80;
preset1 = new JButton("Preset1");
pane.add(preset1, preset1Constraints);
//--------------------------------------
GridBagConstraints preset2Constraints = new GridBagConstraints();
preset2Constraints.gridx = 1;
preset2Constraints.gridy = 2;
preset2Constraints.gridwidth = 1;
preset2Constraints.anchor = GridBagConstraints.CENTER;
preset2Constraints.fill = GridBagConstraints.BOTH;
preset2Constraints.ipadx = 80;
preset2 = new JButton("Preset2");
pane.add(preset2, preset2Constraints);
//--------------------------------------
GridBagConstraints preset3Constraints = new GridBagConstraints();
preset3Constraints.gridx = 2;
preset3Constraints.gridy = 2;
preset3Constraints.gridwidth = 1;
preset3Constraints.anchor = GridBagConstraints.CENTER;
preset3Constraints.fill = GridBagConstraints.BOTH;
preset3Constraints.ipadx = 80;
preset3 = new JButton("Preset3");
pane.add(preset3, preset3Constraints);
//-------------------------------------
GridBagConstraints preset4Constraints = new GridBagConstraints();
preset4Constraints.gridx = 0;
preset4Constraints.gridy = 3;
preset4Constraints.gridwidth = 1;
preset4Constraints.anchor = GridBagConstraints.CENTER;
preset4Constraints.fill = GridBagConstraints.BOTH;
preset4Constraints.ipadx = 60;
preset4 = new JButton("Preset4");
pane.add(preset4, preset4Constraints);
//--------------------------------------
GridBagConstraints resetConstraints = new GridBagConstraints();
resetConstraints.gridx = 1;
resetConstraints.gridy = 3;
resetConstraints.gridwidth = 1;
resetConstraints.anchor = GridBagConstraints.CENTER;
resetConstraints.fill = GridBagConstraints.BOTH;
resetConstraints.ipadx = 40;
resetTacho = new JButton("Calibrate Tacho");
pane.add(resetTacho, resetConstraints);
//----------------------------------------
GridBagConstraints quitConstraints = new GridBagConstraints();
quitConstraints.gridx = 2;
quitConstraints.gridy = 3;
quitConstraints.gridwidth = 1;
quitConstraints.anchor = GridBagConstraints.CENTER;
quitConstraints.fill = GridBagConstraints.BOTH;
quitConstraints.ipadx = 30;
quitProgram = new JButton("Quit NXT Program");
pane.add(quitProgram, quitConstraints);
//-------------------------------------
Handlerclass handler = new Handlerclass();
mousepanel.addMouseListener(handler);
mousepanel.addMouseMotionListener(handler);
preset1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
xAxis = 61000;
}
});
preset2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
xAxis = 62000;
}
});
preset3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
xAxis = 63000;
}
});
preset4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
xAxis = 64000;
}
});
resetTacho.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
xAxis = 65000;
}
});
quitProgram.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
xAxis = 500;
}
});
add(pane);
}
private class Handlerclass implements MouseListener, MouseMotionListener{
public void mouseClicked(MouseEvent event){
xAxis = event.getX();
yAxis = event.getY();
if(xAxis <= 280 && xAxis >= 200){
if(yAxis >= 180 && yAxis <= 260){
statusbar.setText("0,0");
}
else{
if(yAxis >= 261){
ySpeed = yAxis - 245;
}
else if(yAxis <= 179){
ySpeed = 0 - (195 - yAxis);
}
statusbar.setText("0," + ySpeed);
}
}
if(xAxis > 280 || xAxis < 200){
if(yAxis >= 180 && yAxis <=260){
if(xAxis >= 281){
xSpeed = xAxis - 265;
}
else if(xAxis <= 199){
xSpeed = 0 - (210 - xAxis);
}
statusbar.setText(xSpeed + ",0");
}
else{
if(yAxis >= 261){
ySpeed = yAxis - 245;
}
else if(yAxis <= 179){
ySpeed = 0 - (195 - yAxis);
}
if(xAxis >= 281){
xSpeed = xAxis - 265;
}
else if(xAxis <= 199){
xSpeed = 0 - (210 - xAxis);
}
statusbar.setText(xSpeed + "," + ySpeed);
}
}
}
public void mousePressed(MouseEvent event){
}
public void mouseReleased(MouseEvent event){
}
public void mouseEntered(MouseEvent event){
}
public void mouseExited(MouseEvent event){
xAxis = 240;
yAxis = 240;
statusbar.setText(0 + "," + 0);
}
public void mouseDragged(MouseEvent event){
xAxis = event.getX();
yAxis = event.getY();
if(xAxis != 500 && xAxis != 61000 && xAxis != 62000 && xAxis != 63000 && xAxis != 64000 && xAxis != 65000){
if(xAxis >= 200 && xAxis <= 280){
xSpeed = 0;
}
if(xAxis >= 281 && xAxis <= 485){
xSpeed = xAxis - 265;
}else if(xAxis > 485){
xSpeed = 0;
}
if(xAxis <= 199 && xAxis >= 0){
xSpeed = 0 - (210 - xAxis);
}
else if(xAxis < 0){
xSpeed = 0;
}
if(yAxis >= 180 && yAxis <= 260){
ySpeed = 0;
}
if(yAxis >= 261 && yAxis <= 445){
ySpeed = -1 * (yAxis - 245);
}
else if(yAxis > 445){
ySpeed = 0;
}
if(yAxis <= 179 && yAxis >=0){
ySpeed = -1 * (0 - (195 - yAxis));
}
else if(yAxis < 0){
ySpeed = 0;
}
statusbar.setText(xSpeed + "," + ySpeed);
}
}
public void mouseMoved(MouseEvent event){
}
}
public int getXAxis(){
if((xAxis > 485 || xAxis < 0) && xAxis != 500 && xAxis != 61000 && xAxis != 62000 && xAxis != 63000 && xAxis != 64000 && xAxis != 65000){
return 0;
}
else{
return xAxis;
}
}
public int getYAxis(){
if(yAxis > 445 || yAxis < 0){
return 0;
}else{
return yAxis;
}
}
}
- Code: Select all
import java.io.*;
import lejos.nxt.comm.*;
import lejos.nxt.*;
public class mainclass {
static int absvalue = 0;
public static void main(String[] args) {
try{
NXTConnection connection = USB.waitForConnection();
DataInputStream dis = connection.openDataInputStream();
int pan = dis.readInt();
Motor.A.resetTachoCount();
Motor.B.resetTachoCount();
while(pan != 500){
pan = dis.readInt();
if((pan > 500 || pan < -500) && pan != 65000){
if (pan != 60000){
pan = pan / 100;
}
if (pan == 500){
Motor.B.stop();
}
else if (pan == 60000){
Motor.B.stop();
}
else if(pan > 0){
Motor.B.setSpeed(pan * 2);
Motor.B.backward();
}
else if(pan < 0){
absvalue = Math.abs(pan * 2);
Motor.B.setSpeed(absvalue);
Motor.B.forward();
}
}
else if((pan < 500 && pan > -500) || pan == 500 || pan == 65000){
if (pan == 500){
Motor.A.stop();
}
else if (pan == 65000){
Motor.A.stop();
LCD.drawString("it works", 1, 1);
}
else if (pan == 0){
Motor.A.stop();
}
else if(pan > 0){
Motor.A.setSpeed(pan);
Motor.A.forward();
}
else if(pan < 0){
absvalue = Math.abs(pan);
Motor.A.setSpeed(absvalue);
Motor.A.backward();
}
}
}
}catch(Exception e){}
}
}
-
abalis3
- New User
-
- Posts: 6
- Joined: Sat Jan 14, 2012 4:03 am
by lawrie » Tue Jan 24, 2012 12:36 pm
You are not likely to get many replies asking people to debug a rather complicated program.
You really should try to tidy your program up and think about what different types of commands you are sending to the NXT and not mix it all up in magic numbers in one integer. A command number folowed by data relevant to that command often works well.
You should define constants like STOP_COMMAND rather than use random numbers throughout the program. It is very hard for you, or anyone else, to understand what is going on.
It looks like you will never send 65000 to the NXT as your if statement in pcmainclass will execute:
else if(xRecieved >= 281){
dos.writeInt((xRecieved - 265));
dos.flush();
}
and the branch of the if that you expected to deal with 61000 etc. will never get executed.
-
lawrie
- leJOS Team Member
-
- Posts: 677
- Joined: Mon Feb 05, 2007 1:27 pm
Return to NXJ Projects
Who is online
Users browsing this forum: No registered users and 0 guests