snapshot
@@ -1,118 +0,0 @@ | ||
1 | -package sample; | |
2 | - | |
3 | -import java.awt.Color; | |
4 | - | |
5 | -import java.awt.Graphics; | |
6 | -import java.awt.Graphics2D; | |
7 | -import java.awt.Shape; | |
8 | -import java.awt.geom.Rectangle2D; | |
9 | -import java.util.Set; | |
10 | -import javax.swing.JFrame; | |
11 | - | |
12 | -import jp.digitalmuseum.mr.Matereal; | |
13 | -import jp.digitalmuseum.mr.entity.Entity; | |
14 | -import jp.digitalmuseum.mr.entity.EntityImpl; | |
15 | -import jp.digitalmuseum.mr.gui.DisposeOnCloseFrame; | |
16 | -import jp.digitalmuseum.mr.gui.ImageProviderPanel; | |
17 | -import jp.digitalmuseum.mr.gui.utils.EntityPainter; | |
18 | -import jp.digitalmuseum.mr.service.MarkerDetector; | |
19 | -import jp.digitalmuseum.mr.service.Camera; | |
20 | -import jp.digitalmuseum.napkit.NapDetectionResult; | |
21 | -import jp.digitalmuseum.napkit.NapMarker; | |
22 | -import jp.digitalmuseum.napkit.gui.TypicalMDCPane; | |
23 | -import jp.digitalmuseum.utils.ScreenPosition; | |
24 | -import jp.digitalmuseum.utils.ScreenRectangle; | |
25 | - | |
26 | -/** | |
27 | - * Run a marker detection and show results. | |
28 | - * | |
29 | - * @author Jun KATO | |
30 | - */ | |
31 | -public class Step2_PaintEntity { | |
32 | - | |
33 | - public static void main(String[] args) { | |
34 | - new Step2_PaintEntity(); | |
35 | - } | |
36 | - | |
37 | - public Step2_PaintEntity() { | |
38 | - | |
39 | - // Run a camera. | |
40 | - final Camera camera = new Camera(); | |
41 | - camera.start(); | |
42 | - | |
43 | - // Run a marker detector. | |
44 | - final MarkerDetector detector = new MarkerDetector(); | |
45 | - detector.loadCameraParameter("calib_qcam.dat"); | |
46 | - detector.start(); | |
47 | - | |
48 | - // Show a configuration window. | |
49 | - final JFrame configFrame = new DisposeOnCloseFrame(new TypicalMDCPane(detector)); | |
50 | - | |
51 | - // Detect a marker. | |
52 | - final NapMarker marker = new NapMarker("markers\\4x4_78.patt",45); | |
53 | - final Entity dummy = new EntityImpl("test") { | |
54 | - public void dispose() {} | |
55 | - public Shape getShape() { return new Rectangle2D.Double(-10, -10, 20, 20); } | |
56 | - }; | |
57 | - detector.put(marker, dummy); | |
58 | - | |
59 | - // Initialize a painter | |
60 | - final EntityPainter painter = new EntityPainter(); | |
61 | - | |
62 | - // Show detection results in real-time. | |
63 | - final DisposeOnCloseFrame frame = new DisposeOnCloseFrame( | |
64 | - new ImageProviderPanel(camera) { | |
65 | - private static final long serialVersionUID = 1L; | |
66 | - @Override public void paintComponent(Graphics g) { | |
67 | - super.paintComponent(g); | |
68 | - | |
69 | - // Get detected results. | |
70 | - final Set<NapDetectionResult> results = detector.getResults(); | |
71 | - | |
72 | - // Draw detected number of squares. | |
73 | - drawString(g, "detected: "+(results == null ? 0 : results.size()), | |
74 | - 10, getHeight()-10); | |
75 | - | |
76 | - // Draw each detected result. | |
77 | - for (final NapDetectionResult result : results) { | |
78 | - | |
79 | - // Draw corners | |
80 | - final ScreenRectangle corners = result.getSquare(); | |
81 | - corners.draw((Graphics2D) g, true); | |
82 | - | |
83 | - // Draw information for the square. | |
84 | - final ScreenPosition point = result.getPosition(); | |
85 | - g.drawLine(point.getX(), point.getY(), point.getX()+55, point.getY()+43); | |
86 | - g.drawRect(point.getX()+55, point.getY()+23, 200, 40); | |
87 | - drawString(g, "Confidence: "+result.getConfidence(), point.getX()+64, point.getY()+40); | |
88 | - drawString(g, "Position: "+point, point.getX()+64, point.getY()+56); | |
89 | - } | |
90 | - | |
91 | - // Paint a entity | |
92 | - painter.paint(g, dummy); | |
93 | - } | |
94 | - | |
95 | - /** | |
96 | - * Draw a string with 1px simple black border. | |
97 | - */ | |
98 | - private void drawString(Graphics g, String s, int x, int y) { | |
99 | - g.setColor(Color.black); | |
100 | - g.drawString(s, x+1, y); | |
101 | - g.drawString(s, x-1, y); | |
102 | - g.drawString(s, x, y-1); | |
103 | - g.drawString(s, x, y+1); | |
104 | - g.setColor(Color.orange); | |
105 | - g.drawString(s, x, y); | |
106 | - } | |
107 | - }) { | |
108 | - private static final long serialVersionUID = 1L; | |
109 | - @Override public void dispose() { | |
110 | - configFrame.dispose(); | |
111 | - super.dispose(); | |
112 | - Matereal.getInstance().dispose(); | |
113 | - } | |
114 | - }; | |
115 | - frame.setFrameSize(camera.getWidth(), camera.getHeight()); | |
116 | - frame.setResizable(false); | |
117 | - } | |
118 | -} |
@@ -1,123 +0,0 @@ | ||
1 | -package sample; | |
2 | - | |
3 | -import java.awt.BasicStroke; | |
4 | -import java.awt.Color; | |
5 | -import java.awt.Graphics; | |
6 | -import java.awt.Graphics2D; | |
7 | -import java.awt.event.MouseAdapter; | |
8 | -import java.awt.event.MouseEvent; | |
9 | - | |
10 | -import javax.swing.JFrame; | |
11 | - | |
12 | -import jp.digitalmuseum.mr.Matereal; | |
13 | -import jp.digitalmuseum.mr.entity.MindstormsNXT; | |
14 | -import jp.digitalmuseum.mr.entity.Robot; | |
15 | -import jp.digitalmuseum.mr.gui.DisposeOnCloseFrame; | |
16 | -import jp.digitalmuseum.mr.gui.ImageProviderPanel; | |
17 | -import jp.digitalmuseum.mr.service.MarkerDetector; | |
18 | -import jp.digitalmuseum.mr.service.Camera; | |
19 | -import jp.digitalmuseum.mr.task.RotateTo; | |
20 | -import jp.digitalmuseum.mr.task.Task; | |
21 | -import jp.digitalmuseum.napkit.NapMarker; | |
22 | -import jp.digitalmuseum.napkit.gui.TypicalMDCPane; | |
23 | -import jp.digitalmuseum.utils.Position; | |
24 | -import jp.digitalmuseum.utils.ScreenPosition; | |
25 | -//import jp.digitalmuseum.mr.entity.Noopy; | |
26 | - | |
27 | - | |
28 | -/** | |
29 | - * Show a frame with which an end-user can specify in which direction a robot should face. | |
30 | - * | |
31 | - * @author Jun KATO | |
32 | - */ | |
33 | -public class Step4_ClickAndRotate { | |
34 | - private Task workingTask; | |
35 | - final private ScreenPosition clickedPosition = new ScreenPosition(); | |
36 | - | |
37 | - public static void main(String[] args) { | |
38 | - new Step4_ClickAndRotate(); | |
39 | - } | |
40 | - | |
41 | - public Step4_ClickAndRotate() { | |
42 | - | |
43 | - final Robot robot = new MindstormsNXT("Mindstorms NXT", "btspp://00165306523e"); | |
44 | - | |
45 | - // Run a camera. | |
46 | - final Camera camera = new Camera(); | |
47 | - camera.start(); | |
48 | - | |
49 | - // Run a marker detector. | |
50 | - final MarkerDetector detector = new MarkerDetector(); | |
51 | - detector.put(new NapMarker("markers/4x4_78.patt", 4.5), robot); | |
52 | - detector.start(); | |
53 | - | |
54 | - // Show a configuration window. | |
55 | - final JFrame configFrame = new DisposeOnCloseFrame( | |
56 | - new TypicalMDCPane(detector)); | |
57 | - | |
58 | - // Initialize a main panel. | |
59 | - final ImageProviderPanel panel = new ImageProviderPanel(camera) { | |
60 | - private static final long serialVersionUID = 1L; | |
61 | - @Override public void paintComponent(Graphics g) { | |
62 | - super.paintComponent(g); | |
63 | - final Graphics2D g2 = (Graphics2D) g; | |
64 | - g2.translate(getOffsetX(), getOffsetY()); | |
65 | - | |
66 | - // Paint detection results. | |
67 | - g2.setStroke(new BasicStroke(4)); | |
68 | - g2.setColor(Color.orange); | |
69 | - detector.paint(g2); | |
70 | - | |
71 | - // Paint the destination. | |
72 | - if (clickedPosition != null) { | |
73 | - g2.setColor(Color.green); | |
74 | - g2.fillOval( | |
75 | - clickedPosition.getX()-5, | |
76 | - clickedPosition.getY()-5, | |
77 | - 10, 10); | |
78 | - } | |
79 | - } | |
80 | - @Override public void dispose() { | |
81 | - configFrame.dispose(); | |
82 | - super.dispose(); | |
83 | - Matereal.getInstance().dispose(); | |
84 | - } | |
85 | - }; | |
86 | - | |
87 | - // Add a mouse listener. | |
88 | - panel.addMouseListener(new MouseAdapter() { | |
89 | - final private Position destination = | |
90 | - new Position(); | |
91 | - private Position position = | |
92 | - new Position(); | |
93 | - @Override public void mouseClicked(MouseEvent e) { | |
94 | - if (e.getButton() == MouseEvent.BUTTON1) { | |
95 | - | |
96 | - // Get a clicked position. | |
97 | - clickedPosition.set(e.getX(), e.getY()); | |
98 | - panel.getScreenToImageOut(clickedPosition, clickedPosition); | |
99 | - camera.screenToRealOut(clickedPosition, destination); | |
100 | - detector.getPositionOut(robot, position); | |
101 | - | |
102 | - final double destinationDirection = | |
103 | - position.getRelativeDirection(destination); | |
104 | - | |
105 | - // If another task is already working, stop it. | |
106 | - if (workingTask != null && workingTask.isStarted()) { | |
107 | - workingTask.stop(); | |
108 | - } | |
109 | - | |
110 | - // Assign a task rotating to a certain direction. | |
111 | - workingTask = new RotateTo(destinationDirection); | |
112 | - if (workingTask.assign(robot)) { | |
113 | - workingTask.start(); | |
114 | - } | |
115 | - } | |
116 | - } | |
117 | - }); | |
118 | - | |
119 | - // Show a world. | |
120 | - final DisposeOnCloseFrame frame = new DisposeOnCloseFrame(panel); | |
121 | - frame.setFrameSize(560, 420); | |
122 | - } | |
123 | -} |
@@ -1,125 +0,0 @@ | ||
1 | -package sample; | |
2 | - | |
3 | -import java.awt.BasicStroke; | |
4 | - | |
5 | -import java.awt.Color; | |
6 | -import java.awt.Graphics; | |
7 | -import java.awt.Graphics2D; | |
8 | -import java.awt.event.MouseAdapter; | |
9 | -import java.awt.event.MouseEvent; | |
10 | - | |
11 | -import javax.swing.JFrame; | |
12 | - | |
13 | -import jp.digitalmuseum.mr.Matereal; | |
14 | -import jp.digitalmuseum.mr.entity.NetTansor; | |
15 | -import jp.digitalmuseum.mr.gui.DisposeOnCloseFrame; | |
16 | -import jp.digitalmuseum.mr.gui.ImageProviderPanel; | |
17 | -import jp.digitalmuseum.mr.service.MarkerDetector; | |
18 | -import jp.digitalmuseum.mr.service.Camera; | |
19 | -import jp.digitalmuseum.mr.task.Follow; | |
20 | -import jp.digitalmuseum.mr.task.Move; | |
21 | -import jp.digitalmuseum.mr.task.Task; | |
22 | -import jp.digitalmuseum.napkit.NapMarker; | |
23 | -import jp.digitalmuseum.napkit.gui.TypicalMDCPane; | |
24 | -import jp.digitalmuseum.utils.Position; | |
25 | -import jp.digitalmuseum.utils.ScreenPosition; | |
26 | - | |
27 | - | |
28 | -/** | |
29 | - * Show a frame with a view of the world coordinate. | |
30 | - * | |
31 | - * @author Jun KATO | |
32 | - */ | |
33 | -public class Step4_ClickAndFollow { | |
34 | - private Task workingTask; | |
35 | - private ScreenPosition clickedPosition; | |
36 | - | |
37 | - public static void main(String[] args) { | |
38 | - new Step4_ClickAndFollow(); | |
39 | - } | |
40 | - | |
41 | - public Step4_ClickAndFollow() { | |
42 | - | |
43 | - // Initialize a robot. | |
44 | - final NetTansor robot = new NetTansor("NetTansor Web", "http://192.168.1.103:8081"); | |
45 | - final NetTansor robot2 = new NetTansor("NetTansor", "http://192.168.1.104:8081"); | |
46 | - | |
47 | - // Run a camera. | |
48 | - final Camera camera = new Camera("filter:1"); | |
49 | - camera.start(); | |
50 | - | |
51 | - // Run a marker detector. | |
52 | - final MarkerDetector detector = new MarkerDetector(); | |
53 | - detector.setImageProvider(camera); | |
54 | - detector.put(new NapMarker("markers/4x4_112.patt", 4.5), robot); | |
55 | - detector.put(new NapMarker("markers/4x4_78.patt", 4.5), robot2); | |
56 | - detector.start(); | |
57 | - | |
58 | - // Show a configuration window. | |
59 | - final JFrame configFrame = new DisposeOnCloseFrame( | |
60 | - new TypicalMDCPane(detector)); | |
61 | - | |
62 | - // Initialize a main panel. | |
63 | - final ImageProviderPanel panel = new ImageProviderPanel(camera) { | |
64 | - private static final long serialVersionUID = 1L; | |
65 | - @Override public void paintComponent(Graphics g) { | |
66 | - super.paintComponent(g); | |
67 | - final Graphics2D g2 = (Graphics2D) g; | |
68 | - | |
69 | - // Paint detection results. | |
70 | - g2.setStroke(new BasicStroke(4)); | |
71 | - g2.setColor(Color.orange); | |
72 | - detector.paint(g2); | |
73 | - | |
74 | - // Paint the destination. | |
75 | - if (clickedPosition != null) { | |
76 | - g2.setColor(Color.green); | |
77 | - g2.fillOval( | |
78 | - clickedPosition.getX()-5, | |
79 | - clickedPosition.getY()-5, | |
80 | - 10, 10); | |
81 | - } | |
82 | - } | |
83 | - @Override public void dispose() { | |
84 | - configFrame.dispose(); | |
85 | - super.dispose(); | |
86 | - Matereal.getInstance().dispose(); | |
87 | - } | |
88 | - }; | |
89 | - | |
90 | - // Add a mouse listener. | |
91 | - panel.addMouseListener(new MouseAdapter() { | |
92 | - final private ScreenPosition clickedPosition = | |
93 | - new ScreenPosition(); | |
94 | - final private Position destination = | |
95 | - new Position(); | |
96 | - @Override public void mouseClicked(MouseEvent e) { | |
97 | - if (e.getButton() == MouseEvent.BUTTON1) { | |
98 | - | |
99 | - // Get a clicked position. | |
100 | - clickedPosition.set(e.getX(), e.getY()); | |
101 | - panel.getScreenToImageOut(clickedPosition, clickedPosition); | |
102 | - camera.screenToRealOut(clickedPosition, destination); | |
103 | - | |
104 | - // Assign a task moving to the position. | |
105 | - if (workingTask != null && workingTask.isStarted()) { | |
106 | - workingTask.stop(); | |
107 | - } | |
108 | - workingTask = new Move(destination); | |
109 | - if (workingTask.assign(robot)) { | |
110 | - workingTask.start(); | |
111 | - } | |
112 | - } | |
113 | - } | |
114 | - }); | |
115 | - | |
116 | - final Follow follow = new Follow(robot); | |
117 | - if (follow.assign(robot2)) { | |
118 | - follow.start(); | |
119 | - } | |
120 | - | |
121 | - // Show a world. | |
122 | - final JFrame frame = new DisposeOnCloseFrame(panel); | |
123 | - frame.setSize(710, 660); | |
124 | - } | |
125 | -} |
@@ -1,114 +0,0 @@ | ||
1 | -package sample; | |
2 | - | |
3 | -import java.awt.BasicStroke; | |
4 | - | |
5 | -import java.awt.Color; | |
6 | -import java.awt.Graphics; | |
7 | -import java.awt.Graphics2D; | |
8 | -import java.awt.Stroke; | |
9 | -import java.util.Set; | |
10 | -import javax.swing.JFrame; | |
11 | - | |
12 | -import jp.digitalmuseum.mr.Matereal; | |
13 | -import jp.digitalmuseum.mr.entity.Entity; | |
14 | -import jp.digitalmuseum.mr.entity.EntityImpl; | |
15 | -import jp.digitalmuseum.mr.gui.DisposeOnCloseFrame; | |
16 | -import jp.digitalmuseum.mr.gui.ImageProviderPanel; | |
17 | -import jp.digitalmuseum.mr.service.MarkerDetector; | |
18 | -import jp.digitalmuseum.mr.service.Camera; | |
19 | -import jp.digitalmuseum.napkit.NapDetectionResult; | |
20 | -import jp.digitalmuseum.napkit.NapMarker; | |
21 | -import jp.digitalmuseum.napkit.gui.TypicalMDCPane; | |
22 | -import jp.digitalmuseum.utils.ScreenPosition; | |
23 | - | |
24 | -/** | |
25 | - * Run a marker detection and show results. | |
26 | - * | |
27 | - * @author Jun KATO | |
28 | - */ | |
29 | -public class Step2_RunDetectMarker { | |
30 | - | |
31 | - public static void main(String[] args) { | |
32 | - new Step2_RunDetectMarker(); | |
33 | - } | |
34 | - | |
35 | - public Step2_RunDetectMarker() { | |
36 | - | |
37 | - // Run a camera. | |
38 | - final Camera camera = new Camera(); | |
39 | - camera.start(); | |
40 | - | |
41 | - // Run a marker detector. | |
42 | - final MarkerDetector detector = new MarkerDetector(); | |
43 | - detector.loadCameraParameter("calib_qcam.dat"); | |
44 | - detector.start(); | |
45 | - | |
46 | - // Show a configuration window. | |
47 | - final JFrame configFrame = new DisposeOnCloseFrame(new TypicalMDCPane(detector)); | |
48 | - | |
49 | - // Detect a marker. | |
50 | - final NapMarker marker = new NapMarker("markers\\4x4_743.patt",120); | |
51 | - final Entity dummy = new EntityImpl("test"); | |
52 | - detector.put(marker, dummy); | |
53 | - | |
54 | - // Show detection results in real-time. | |
55 | - final DisposeOnCloseFrame frame = new DisposeOnCloseFrame( | |
56 | - new ImageProviderPanel(camera) { | |
57 | - private static final long serialVersionUID = 1L; | |
58 | - private transient Stroke stroke; | |
59 | - @Override public void paintComponent(Graphics g) { | |
60 | - super.paintComponent(g); | |
61 | - final Graphics2D g2 = (Graphics2D) g; | |
62 | - if (stroke == null) { | |
63 | - stroke = new BasicStroke(5); | |
64 | - } | |
65 | - g2.setStroke(stroke); | |
66 | - g2.translate(getOffsetX(), getOffsetY()); | |
67 | - | |
68 | - // Get detected results. | |
69 | - final Set<NapDetectionResult> results = detector.getResults(); | |
70 | - | |
71 | - // Draw each detected result. | |
72 | - for (final NapDetectionResult result : results) { | |
73 | - | |
74 | - // Draw corners | |
75 | - g.setColor(Color.orange); | |
76 | - detector.paint(g2, result); | |
77 | - | |
78 | - // Draw information for the square. | |
79 | - g.setColor(Color.cyan); | |
80 | - final ScreenPosition point = result.getPosition(); | |
81 | - g2.drawLine(point.getX(), point.getY(), point.getX()+55, point.getY()+43); | |
82 | - g2.drawRect(point.getX()+55, point.getY()+23, 200, 40); | |
83 | - drawString(g2, "Confidence: "+result.getConfidence(), point.getX()+64, point.getY()+40); | |
84 | - drawString(g2, "Position: "+point, point.getX()+64, point.getY()+56); | |
85 | - } | |
86 | - | |
87 | - // Draw detected number of squares. | |
88 | - drawString(g2, "detected: "+(results == null ? 0 : results.size()), | |
89 | - 10, getHeight()-10); | |
90 | - } | |
91 | - | |
92 | - /** | |
93 | - * Draw a string with 1px simple black border. | |
94 | - */ | |
95 | - private void drawString(Graphics g, String s, int x, int y) { | |
96 | - g.setColor(Color.black); | |
97 | - g.drawString(s, x+1, y); | |
98 | - g.drawString(s, x-1, y); | |
99 | - g.drawString(s, x, y-1); | |
100 | - g.drawString(s, x, y+1); | |
101 | - g.setColor(Color.cyan); | |
102 | - g.drawString(s, x, y); | |
103 | - } | |
104 | - }) { | |
105 | - private static final long serialVersionUID = 1L; | |
106 | - @Override public void dispose() { | |
107 | - configFrame.dispose(); | |
108 | - super.dispose(); | |
109 | - Matereal.getInstance().dispose(); | |
110 | - } | |
111 | - }; | |
112 | - frame.setFrameSize(camera.getWidth(), camera.getHeight()); | |
113 | - } | |
114 | -} |
@@ -1,122 +0,0 @@ | ||
1 | -package sample; | |
2 | - | |
3 | -import java.awt.BasicStroke; | |
4 | - | |
5 | -import java.awt.Color; | |
6 | -import java.awt.Graphics; | |
7 | -import java.awt.Graphics2D; | |
8 | -import java.awt.event.MouseAdapter; | |
9 | -import java.awt.event.MouseEvent; | |
10 | - | |
11 | -import javax.swing.JFrame; | |
12 | - | |
13 | -import jp.digitalmuseum.mr.Matereal; | |
14 | -import jp.digitalmuseum.mr.entity.MindstormsNXT; | |
15 | -import jp.digitalmuseum.mr.entity.Robot; | |
16 | -import jp.digitalmuseum.mr.gui.DisposeOnCloseFrame; | |
17 | -import jp.digitalmuseum.mr.gui.ImageProviderPanel; | |
18 | -import jp.digitalmuseum.mr.resource.WheelsController; | |
19 | -import jp.digitalmuseum.mr.service.MarkerDetector; | |
20 | -import jp.digitalmuseum.mr.service.Camera; | |
21 | -import jp.digitalmuseum.mr.task.Move; | |
22 | -import jp.digitalmuseum.mr.task.Task; | |
23 | -import jp.digitalmuseum.napkit.NapMarker; | |
24 | -import jp.digitalmuseum.napkit.gui.TypicalMDCPane; | |
25 | -import jp.digitalmuseum.utils.Position; | |
26 | -import jp.digitalmuseum.utils.ScreenPosition; | |
27 | - | |
28 | -/** | |
29 | - * Show a frame with a view of the world coordinate. | |
30 | - * | |
31 | - * @author Jun KATO | |
32 | - */ | |
33 | -public class Step4_ClickAndRun { | |
34 | - private Task moveTo; | |
35 | - final private ScreenPosition goalPosition = new ScreenPosition(); | |
36 | - | |
37 | - public static void main(String[] args) { | |
38 | - new Step4_ClickAndRun(); | |
39 | - } | |
40 | - | |
41 | - public Step4_ClickAndRun() { | |
42 | - | |
43 | - // Destination is not specified at first. | |
44 | - goalPosition.setNotFound(true); | |
45 | - | |
46 | - // Initialize a robot. | |
47 | - final Robot robot = new MindstormsNXT("Mindstorms NXT", "btspp://00165305b308"); | |
48 | - | |
49 | - // Run a camera. | |
50 | - final Camera camera = new Camera(); | |
51 | - camera.start(); | |
52 | - | |
53 | - // Run a marker detector. | |
54 | - final MarkerDetector detector = new MarkerDetector(); | |
55 | - detector.setImageProvider(camera); | |
56 | - detector.put(new NapMarker("markers/4x4_743.patt", 12), robot); | |
57 | - detector.start(); | |
58 | - | |
59 | - // Show a configuration window. | |
60 | - final JFrame configFrame = new DisposeOnCloseFrame( | |
61 | - new TypicalMDCPane(detector)); | |
62 | - | |
63 | - // Initialize a main panel. | |
64 | - final ImageProviderPanel panel = new ImageProviderPanel(camera) { | |
65 | - private static final long serialVersionUID = 1L; | |
66 | - @Override public void paintComponent(Graphics g) { | |
67 | - super.paintComponent(g); | |
68 | - final Graphics2D g2 = (Graphics2D) g; | |
69 | - g2.translate(getOffsetX(), getOffsetY()); | |
70 | - | |
71 | - // Paint detection results. | |
72 | - g2.setStroke(new BasicStroke(4)); | |
73 | - g2.setColor(Color.orange); | |
74 | - detector.paint(g2); | |
75 | - | |
76 | - // Paint the destination. | |
77 | - if (!goalPosition.isNotFound()) { | |
78 | - g2.setColor(Color.green); | |
79 | - g2.fillOval( | |
80 | - goalPosition.getX()-5, | |
81 | - goalPosition.getY()-5, | |
82 | - 10, 10); | |
83 | - } | |
84 | - } | |
85 | - @Override public void dispose() { | |
86 | - configFrame.dispose(); | |
87 | - super.dispose(); | |
88 | - Matereal.getInstance().dispose(); | |
89 | - } | |
90 | - }; | |
91 | - | |
92 | - // Add a mouse listener. | |
93 | - panel.addMouseListener(new MouseAdapter() { | |
94 | - final private Position destination = | |
95 | - new Position(); | |
96 | - | |
97 | - @Override public void mouseClicked(MouseEvent e) { | |
98 | - if (e.getButton() == MouseEvent.BUTTON1) { | |
99 | - | |
100 | - // Get a clicked position. | |
101 | - goalPosition.set(e.getX(), e.getY()); | |
102 | - panel.getScreenToImageOut(goalPosition, goalPosition); | |
103 | - camera.screenToRealOut(goalPosition, destination); | |
104 | - | |
105 | - // Assign a task moving to the position. | |
106 | - final Task task = robot.getAssignedTask(WheelsController.class); | |
107 | - if (task != null) { | |
108 | - task.stop(); | |
109 | - } | |
110 | - moveTo = new Move(camera.screenToReal(goalPosition)); | |
111 | - if (moveTo.assign(robot)) { | |
112 | - moveTo.start(); | |
113 | - } | |
114 | - } | |
115 | - } | |
116 | - }); | |
117 | - | |
118 | - // Show a world. | |
119 | - final JFrame frame = new DisposeOnCloseFrame(panel); | |
120 | - frame.setSize(710, 660); | |
121 | - } | |
122 | -} |
@@ -0,0 +1,127 @@ | ||
1 | +package sample; | |
2 | + | |
3 | +import java.awt.BasicStroke; | |
4 | + | |
5 | +import java.awt.Color; | |
6 | +import java.awt.Graphics; | |
7 | +import java.awt.Graphics2D; | |
8 | +import java.awt.Stroke; | |
9 | +import java.util.Set; | |
10 | +import javax.swing.JFrame; | |
11 | +import javax.swing.JOptionPane; | |
12 | + | |
13 | +import jp.digitalmuseum.capture.VideoCaptureFactoryImpl; | |
14 | +import jp.digitalmuseum.mr.Matereal; | |
15 | +import jp.digitalmuseum.mr.entity.Entity; | |
16 | +import jp.digitalmuseum.mr.entity.EntityImpl; | |
17 | +import jp.digitalmuseum.mr.gui.DisposeOnCloseFrame; | |
18 | +import jp.digitalmuseum.mr.gui.ImageProviderPanel; | |
19 | +import jp.digitalmuseum.mr.service.MarkerDetector; | |
20 | +import jp.digitalmuseum.mr.service.Camera; | |
21 | +import jp.digitalmuseum.napkit.NapDetectionResult; | |
22 | +import jp.digitalmuseum.napkit.NapMarker; | |
23 | +import jp.digitalmuseum.napkit.gui.TypicalMDCPane; | |
24 | +import jp.digitalmuseum.utils.ScreenPosition; | |
25 | + | |
26 | +/** | |
27 | + * Run a marker detection and show results. | |
28 | + * | |
29 | + * @author Jun KATO | |
30 | + */ | |
31 | +public class Step2_RunDetectMarker { | |
32 | + | |
33 | + public static void main(String[] args) { | |
34 | + new Step2_RunDetectMarker(); | |
35 | + } | |
36 | + | |
37 | + public Step2_RunDetectMarker() { | |
38 | + | |
39 | + // Run a camera. | |
40 | + // Let users select a device to capture images. | |
41 | + final String identifier = (String) JOptionPane.showInputDialog(null, | |
42 | + "Select a device to capture images.", "Device list", | |
43 | + JOptionPane.QUESTION_MESSAGE, null, new VideoCaptureFactoryImpl() | |
44 | + .queryIdentifiers(), null); | |
45 | + Camera camera; | |
46 | + if ((identifier != null) && (identifier.length() > 0)) { | |
47 | + camera = new Camera(identifier); | |
48 | + } else { | |
49 | + camera = new Camera(); | |
50 | + } | |
51 | + camera.setSize(800, 600); | |
52 | + camera.start(); | |
53 | + | |
54 | + // Run a marker detector. | |
55 | + final MarkerDetector detector = new MarkerDetector(); | |
56 | + detector.loadCameraParameter("calib_qcam.dat"); | |
57 | + detector.start(); | |
58 | + | |
59 | + // Show a configuration window. | |
60 | + final JFrame configFrame = new DisposeOnCloseFrame(new TypicalMDCPane(detector)); | |
61 | + | |
62 | + // Detect a marker. | |
63 | + final NapMarker marker = new NapMarker("markers\\4x4_35.patt",120); | |
64 | + final Entity dummy = new EntityImpl("test"); | |
65 | + detector.put(marker, dummy); | |
66 | + | |
67 | + // Show detection results in real-time. | |
68 | + final DisposeOnCloseFrame frame = new DisposeOnCloseFrame( | |
69 | + new ImageProviderPanel(camera) { | |
70 | + private static final long serialVersionUID = 1L; | |
71 | + private transient Stroke stroke; | |
72 | + @Override public void paintComponent(Graphics g) { | |
73 | + super.paintComponent(g); | |
74 | + final Graphics2D g2 = (Graphics2D) g; | |
75 | + if (stroke == null) { | |
76 | + stroke = new BasicStroke(5); | |
77 | + } | |
78 | + g2.setStroke(stroke); | |
79 | + g2.translate(getOffsetX(), getOffsetY()); | |
80 | + | |
81 | + // Get detected results. | |
82 | + final Set<NapDetectionResult> results = detector.getResults(); | |
83 | + | |
84 | + // Draw each detected result. | |
85 | + for (final NapDetectionResult result : results) { | |
86 | + | |
87 | + // Draw corners | |
88 | + g.setColor(Color.orange); | |
89 | + detector.paint(g2, result); | |
90 | + | |
91 | + // Draw information for the square. | |
92 | + g.setColor(Color.cyan); | |
93 | + final ScreenPosition point = result.getPosition(); | |
94 | + g2.drawLine(point.getX(), point.getY(), point.getX()+55, point.getY()+43); | |
95 | + g2.drawRect(point.getX()+55, point.getY()+23, 200, 40); | |
96 | + drawString(g2, "Confidence: "+result.getConfidence(), point.getX()+64, point.getY()+40); | |
97 | + drawString(g2, "Position: "+point, point.getX()+64, point.getY()+56); | |
98 | + } | |
99 | + | |
100 | + // Draw detected number of squares. | |
101 | + drawString(g2, "detected: "+(results == null ? 0 : results.size()), | |
102 | + 10, getHeight()-10); | |
103 | + } | |
104 | + | |
105 | + /** | |
106 | + * Draw a string with 1px simple black border. | |
107 | + */ | |
108 | + private void drawString(Graphics g, String s, int x, int y) { | |
109 | + g.setColor(Color.black); | |
110 | + g.drawString(s, x+1, y); | |
111 | + g.drawString(s, x-1, y); | |
112 | + g.drawString(s, x, y-1); | |
113 | + g.drawString(s, x, y+1); | |
114 | + g.setColor(Color.cyan); | |
115 | + g.drawString(s, x, y); | |
116 | + } | |
117 | + }) { | |
118 | + private static final long serialVersionUID = 1L; | |
119 | + @Override public void dispose() { | |
120 | + configFrame.dispose(); | |
121 | + super.dispose(); | |
122 | + Matereal.getInstance().dispose(); | |
123 | + } | |
124 | + }; | |
125 | + frame.setFrameSize(camera.getWidth(), camera.getHeight()); | |
126 | + } | |
127 | +} |
@@ -0,0 +1,118 @@ | ||
1 | +package sample; | |
2 | + | |
3 | +import java.awt.Color; | |
4 | + | |
5 | +import java.awt.Graphics; | |
6 | +import java.awt.Graphics2D; | |
7 | +import java.awt.Shape; | |
8 | +import java.awt.geom.Rectangle2D; | |
9 | +import java.util.Set; | |
10 | +import javax.swing.JFrame; | |
11 | + | |
12 | +import jp.digitalmuseum.mr.Matereal; | |
13 | +import jp.digitalmuseum.mr.entity.Entity; | |
14 | +import jp.digitalmuseum.mr.entity.EntityImpl; | |
15 | +import jp.digitalmuseum.mr.gui.DisposeOnCloseFrame; | |
16 | +import jp.digitalmuseum.mr.gui.ImageProviderPanel; | |
17 | +import jp.digitalmuseum.mr.gui.utils.EntityPainter; | |
18 | +import jp.digitalmuseum.mr.service.MarkerDetector; | |
19 | +import jp.digitalmuseum.mr.service.Camera; | |
20 | +import jp.digitalmuseum.napkit.NapDetectionResult; | |
21 | +import jp.digitalmuseum.napkit.NapMarker; | |
22 | +import jp.digitalmuseum.napkit.gui.TypicalMDCPane; | |
23 | +import jp.digitalmuseum.utils.ScreenPosition; | |
24 | +import jp.digitalmuseum.utils.ScreenRectangle; | |
25 | + | |
26 | +/** | |
27 | + * Run a marker detection and show results. | |
28 | + * | |
29 | + * @author Jun KATO | |
30 | + */ | |
31 | +public class Step2_PaintEntity { | |
32 | + | |
33 | + public static void main(String[] args) { | |
34 | + new Step2_PaintEntity(); | |
35 | + } | |
36 | + | |
37 | + public Step2_PaintEntity() { | |
38 | + | |
39 | + // Run a camera. | |
40 | + final Camera camera = new Camera(); | |
41 | + camera.start(); | |
42 | + | |
43 | + // Run a marker detector. | |
44 | + final MarkerDetector detector = new MarkerDetector(); | |
45 | + detector.loadCameraParameter("calib_qcam.dat"); | |
46 | + detector.start(); | |
47 | + | |
48 | + // Show a configuration window. | |
49 | + final JFrame configFrame = new DisposeOnCloseFrame(new TypicalMDCPane(detector)); | |
50 | + | |
51 | + // Detect a marker. | |
52 | + final NapMarker marker = new NapMarker("markers\\4x4_78.patt",45); | |
53 | + final Entity dummy = new EntityImpl("test") { | |
54 | + public void dispose() {} | |
55 | + public Shape getShape() { return new Rectangle2D.Double(-10, -10, 20, 20); } | |
56 | + }; | |
57 | + detector.put(marker, dummy); | |
58 | + | |
59 | + // Initialize a painter | |
60 | + final EntityPainter painter = new EntityPainter(); | |
61 | + | |
62 | + // Show detection results in real-time. | |
63 | + final DisposeOnCloseFrame frame = new DisposeOnCloseFrame( | |
64 | + new ImageProviderPanel(camera) { | |
65 | + private static final long serialVersionUID = 1L; | |
66 | + @Override public void paintComponent(Graphics g) { | |
67 | + super.paintComponent(g); | |
68 | + | |
69 | + // Get detected results. | |
70 | + final Set<NapDetectionResult> results = detector.getResults(); | |
71 | + | |
72 | + // Draw detected number of squares. | |
73 | + drawString(g, "detected: "+(results == null ? 0 : results.size()), | |
74 | + 10, getHeight()-10); | |
75 | + | |
76 | + // Draw each detected result. | |
77 | + for (final NapDetectionResult result : results) { | |
78 | + | |
79 | + // Draw corners | |
80 | + final ScreenRectangle corners = result.getSquare(); | |
81 | + corners.draw((Graphics2D) g, true); | |
82 | + | |
83 | + // Draw information for the square. | |
84 | + final ScreenPosition point = result.getPosition(); | |
85 | + g.drawLine(point.getX(), point.getY(), point.getX()+55, point.getY()+43); | |
86 | + g.drawRect(point.getX()+55, point.getY()+23, 200, 40); | |
87 | + drawString(g, "Confidence: "+result.getConfidence(), point.getX()+64, point.getY()+40); | |
88 | + drawString(g, "Position: "+point, point.getX()+64, point.getY()+56); | |
89 | + } | |
90 | + | |
91 | + // Paint a entity | |
92 | + painter.paint(g, dummy); | |
93 | + } | |
94 | + | |
95 | + /** | |
96 | + * Draw a string with 1px simple black border. | |
97 | + */ | |
98 | + private void drawString(Graphics g, String s, int x, int y) { | |
99 | + g.setColor(Color.black); | |
100 | + g.drawString(s, x+1, y); | |
101 | + g.drawString(s, x-1, y); | |
102 | + g.drawString(s, x, y-1); | |
103 | + g.drawString(s, x, y+1); | |
104 | + g.setColor(Color.orange); | |
105 | + g.drawString(s, x, y); | |
106 | + } | |
107 | + }) { | |
108 | + private static final long serialVersionUID = 1L; | |
109 | + @Override public void dispose() { | |
110 | + configFrame.dispose(); | |
111 | + super.dispose(); | |
112 | + Matereal.getInstance().dispose(); | |
113 | + } | |
114 | + }; | |
115 | + frame.setFrameSize(camera.getWidth(), camera.getHeight()); | |
116 | + frame.setResizable(false); | |
117 | + } | |
118 | +} |
@@ -0,0 +1,123 @@ | ||
1 | +package sample; | |
2 | + | |
3 | +import java.awt.BasicStroke; | |
4 | +import java.awt.Color; | |
5 | +import java.awt.Graphics; | |
6 | +import java.awt.Graphics2D; | |
7 | +import java.awt.event.MouseAdapter; | |
8 | +import java.awt.event.MouseEvent; | |
9 | + | |
10 | +import javax.swing.JFrame; | |
11 | + | |
12 | +import jp.digitalmuseum.mr.Matereal; | |
13 | +import jp.digitalmuseum.mr.entity.MindstormsNXT; | |
14 | +import jp.digitalmuseum.mr.entity.Robot; | |
15 | +import jp.digitalmuseum.mr.gui.DisposeOnCloseFrame; | |
16 | +import jp.digitalmuseum.mr.gui.ImageProviderPanel; | |
17 | +import jp.digitalmuseum.mr.service.MarkerDetector; | |
18 | +import jp.digitalmuseum.mr.service.Camera; | |
19 | +import jp.digitalmuseum.mr.task.RotateTo; | |
20 | +import jp.digitalmuseum.mr.task.Task; | |
21 | +import jp.digitalmuseum.napkit.NapMarker; | |
22 | +import jp.digitalmuseum.napkit.gui.TypicalMDCPane; | |
23 | +import jp.digitalmuseum.utils.Position; | |
24 | +import jp.digitalmuseum.utils.ScreenPosition; | |
25 | +//import jp.digitalmuseum.mr.entity.Noopy; | |
26 | + | |
27 | + | |
28 | +/** | |
29 | + * Show a frame with which an end-user can specify in which direction a robot should face. | |
30 | + * | |
31 | + * @author Jun KATO | |
32 | + */ | |
33 | +public class Step4_ClickAndRotate { | |
34 | + private Task workingTask; | |
35 | + final private ScreenPosition clickedPosition = new ScreenPosition(); | |
36 | + | |
37 | + public static void main(String[] args) { | |
38 | + new Step4_ClickAndRotate(); | |
39 | + } | |
40 | + | |
41 | + public Step4_ClickAndRotate() { | |
42 | + | |
43 | + final Robot robot = new MindstormsNXT("Mindstorms NXT", "btspp://00165306523e"); | |
44 | + | |
45 | + // Run a camera. | |
46 | + final Camera camera = new Camera(); | |
47 | + camera.start(); | |
48 | + | |
49 | + // Run a marker detector. | |
50 | + final MarkerDetector detector = new MarkerDetector(); | |
51 | + detector.put(new NapMarker("markers/4x4_78.patt", 4.5), robot); | |
52 | + detector.start(); | |
53 | + | |
54 | + // Show a configuration window. | |
55 | + final JFrame configFrame = new DisposeOnCloseFrame( | |
56 | + new TypicalMDCPane(detector)); | |
57 | + | |
58 | + // Initialize a main panel. | |
59 | + final ImageProviderPanel panel = new ImageProviderPanel(camera) { | |
60 | + private static final long serialVersionUID = 1L; | |
61 | + @Override public void paintComponent(Graphics g) { | |
62 | + super.paintComponent(g); | |
63 | + final Graphics2D g2 = (Graphics2D) g; | |
64 | + g2.translate(getOffsetX(), getOffsetY()); | |
65 | + | |
66 | + // Paint detection results. | |
67 | + g2.setStroke(new BasicStroke(4)); | |
68 | + g2.setColor(Color.orange); | |
69 | + detector.paint(g2); | |
70 | + | |
71 | + // Paint the destination. | |
72 | + if (clickedPosition != null) { | |
73 | + g2.setColor(Color.green); | |
74 | + g2.fillOval( | |
75 | + clickedPosition.getX()-5, | |
76 | + clickedPosition.getY()-5, | |
77 | + 10, 10); | |
78 | + } | |
79 | + } | |
80 | + @Override public void dispose() { | |
81 | + configFrame.dispose(); | |
82 | + super.dispose(); | |
83 | + Matereal.getInstance().dispose(); | |
84 | + } | |
85 | + }; | |
86 | + | |
87 | + // Add a mouse listener. | |
88 | + panel.addMouseListener(new MouseAdapter() { | |
89 | + final private Position destination = | |
90 | + new Position(); | |
91 | + private Position position = | |
92 | + new Position(); | |
93 | + @Override public void mouseClicked(MouseEvent e) { | |
94 | + if (e.getButton() == MouseEvent.BUTTON1) { | |
95 | + | |
96 | + // Get a clicked position. | |
97 | + clickedPosition.set(e.getX(), e.getY()); | |
98 | + panel.getScreenToImageOut(clickedPosition, clickedPosition); | |
99 | + camera.screenToRealOut(clickedPosition, destination); | |
100 | + detector.getPositionOut(robot, position); | |
101 | + | |
102 | + final double destinationDirection = | |
103 | + position.getRelativeDirection(destination); | |
104 | + | |
105 | + // If another task is already working, stop it. | |
106 | + if (workingTask != null && workingTask.isStarted()) { | |
107 | + workingTask.stop(); | |
108 | + } | |
109 | + | |
110 | + // Assign a task rotating to a certain direction. | |
111 | + workingTask = new RotateTo(destinationDirection); | |
112 | + if (workingTask.assign(robot)) { | |
113 | + workingTask.start(); | |
114 | + } | |
115 | + } | |
116 | + } | |
117 | + }); | |
118 | + | |
119 | + // Show a world. | |
120 | + final DisposeOnCloseFrame frame = new DisposeOnCloseFrame(panel); | |
121 | + frame.setFrameSize(560, 420); | |
122 | + } | |
123 | +} |
@@ -0,0 +1,125 @@ | ||
1 | +package sample; | |
2 | + | |
3 | +import java.awt.BasicStroke; | |
4 | + | |
5 | +import java.awt.Color; | |
6 | +import java.awt.Graphics; | |
7 | +import java.awt.Graphics2D; | |
8 | +import java.awt.event.MouseAdapter; | |
9 | +import java.awt.event.MouseEvent; | |
10 | + | |
11 | +import javax.swing.JFrame; | |
12 | + | |
13 | +import jp.digitalmuseum.mr.Matereal; | |
14 | +import jp.digitalmuseum.mr.entity.NetTansor; | |
15 | +import jp.digitalmuseum.mr.gui.DisposeOnCloseFrame; | |
16 | +import jp.digitalmuseum.mr.gui.ImageProviderPanel; | |
17 | +import jp.digitalmuseum.mr.service.MarkerDetector; | |
18 | +import jp.digitalmuseum.mr.service.Camera; | |
19 | +import jp.digitalmuseum.mr.task.Follow; | |
20 | +import jp.digitalmuseum.mr.task.Move; | |
21 | +import jp.digitalmuseum.mr.task.Task; | |
22 | +import jp.digitalmuseum.napkit.NapMarker; | |
23 | +import jp.digitalmuseum.napkit.gui.TypicalMDCPane; | |
24 | +import jp.digitalmuseum.utils.Position; | |
25 | +import jp.digitalmuseum.utils.ScreenPosition; | |
26 | + | |
27 | + | |
28 | +/** | |
29 | + * Show a frame with a view of the world coordinate. | |
30 | + * | |
31 | + * @author Jun KATO | |
32 | + */ | |
33 | +public class Step4_ClickAndFollow { | |
34 | + private Task workingTask; | |
35 | + private ScreenPosition clickedPosition; | |
36 | + | |
37 | + public static void main(String[] args) { | |
38 | + new Step4_ClickAndFollow(); | |
39 | + } | |
40 | + | |
41 | + public Step4_ClickAndFollow() { | |
42 | + | |
43 | + // Initialize a robot. | |
44 | + final NetTansor robot = new NetTansor("NetTansor Web", "http://192.168.1.103:8081"); | |
45 | + final NetTansor robot2 = new NetTansor("NetTansor", "http://192.168.1.104:8081"); | |
46 | + | |
47 | + // Run a camera. | |
48 | + final Camera camera = new Camera("filter:1"); | |
49 | + camera.start(); | |
50 | + | |
51 | + // Run a marker detector. | |
52 | + final MarkerDetector detector = new MarkerDetector(); | |
53 | + detector.setImageProvider(camera); | |
54 | + detector.put(new NapMarker("markers/4x4_112.patt", 4.5), robot); | |
55 | + detector.put(new NapMarker("markers/4x4_78.patt", 4.5), robot2); | |
56 | + detector.start(); | |
57 | + | |
58 | + // Show a configuration window. | |
59 | + final JFrame configFrame = new DisposeOnCloseFrame( | |
60 | + new TypicalMDCPane(detector)); | |
61 | + | |
62 | + // Initialize a main panel. | |
63 | + final ImageProviderPanel panel = new ImageProviderPanel(camera) { | |
64 | + private static final long serialVersionUID = 1L; | |
65 | + @Override public void paintComponent(Graphics g) { | |
66 | + super.paintComponent(g); | |
67 | + final Graphics2D g2 = (Graphics2D) g; | |
68 | + | |
69 | + // Paint detection results. | |
70 | + g2.setStroke(new BasicStroke(4)); | |
71 | + g2.setColor(Color.orange); | |
72 | + detector.paint(g2); | |
73 | + | |
74 | + // Paint the destination. | |
75 | + if (clickedPosition != null) { | |
76 | + g2.setColor(Color.green); | |
77 | + g2.fillOval( | |
78 | + clickedPosition.getX()-5, | |
79 | + clickedPosition.getY()-5, | |
80 | + 10, 10); | |
81 | + } | |
82 | + } | |
83 | + @Override public void dispose() { | |
84 | + configFrame.dispose(); | |
85 | + super.dispose(); | |
86 | + Matereal.getInstance().dispose(); | |
87 | + } | |
88 | + }; | |
89 | + | |
90 | + // Add a mouse listener. | |
91 | + panel.addMouseListener(new MouseAdapter() { | |
92 | + final private ScreenPosition clickedPosition = | |
93 | + new ScreenPosition(); | |
94 | + final private Position destination = | |
95 | + new Position(); | |
96 | + @Override public void mouseClicked(MouseEvent e) { | |
97 | + if (e.getButton() == MouseEvent.BUTTON1) { | |
98 | + | |
99 | + // Get a clicked position. | |
100 | + clickedPosition.set(e.getX(), e.getY()); | |
101 | + panel.getScreenToImageOut(clickedPosition, clickedPosition); | |
102 | + camera.screenToRealOut(clickedPosition, destination); | |
103 | + | |
104 | + // Assign a task moving to the position. | |
105 | + if (workingTask != null && workingTask.isStarted()) { | |
106 | + workingTask.stop(); | |
107 | + } | |
108 | + workingTask = new Move(destination); | |
109 | + if (workingTask.assign(robot)) { | |
110 | + workingTask.start(); | |
111 | + } | |
112 | + } | |
113 | + } | |
114 | + }); | |
115 | + | |
116 | + final Follow follow = new Follow(robot); | |
117 | + if (follow.assign(robot2)) { | |
118 | + follow.start(); | |
119 | + } | |
120 | + | |
121 | + // Show a world. | |
122 | + final JFrame frame = new DisposeOnCloseFrame(panel); | |
123 | + frame.setSize(710, 660); | |
124 | + } | |
125 | +} |
@@ -0,0 +1,122 @@ | ||
1 | +package sample; | |
2 | + | |
3 | +import java.awt.BasicStroke; | |
4 | + | |
5 | +import java.awt.Color; | |
6 | +import java.awt.Graphics; | |
7 | +import java.awt.Graphics2D; | |
8 | +import java.awt.event.MouseAdapter; | |
9 | +import java.awt.event.MouseEvent; | |
10 | + | |
11 | +import javax.swing.JFrame; | |
12 | + | |
13 | +import jp.digitalmuseum.mr.Matereal; | |
14 | +import jp.digitalmuseum.mr.entity.MindstormsNXT; | |
15 | +import jp.digitalmuseum.mr.entity.Robot; | |
16 | +import jp.digitalmuseum.mr.gui.DisposeOnCloseFrame; | |
17 | +import jp.digitalmuseum.mr.gui.ImageProviderPanel; | |
18 | +import jp.digitalmuseum.mr.resource.WheelsController; | |
19 | +import jp.digitalmuseum.mr.service.MarkerDetector; | |
20 | +import jp.digitalmuseum.mr.service.Camera; | |
21 | +import jp.digitalmuseum.mr.task.Move; | |
22 | +import jp.digitalmuseum.mr.task.Task; | |
23 | +import jp.digitalmuseum.napkit.NapMarker; | |
24 | +import jp.digitalmuseum.napkit.gui.TypicalMDCPane; | |
25 | +import jp.digitalmuseum.utils.Position; | |
26 | +import jp.digitalmuseum.utils.ScreenPosition; | |
27 | + | |
28 | +/** | |
29 | + * Show a frame with a view of the world coordinate. | |
30 | + * | |
31 | + * @author Jun KATO | |
32 | + */ | |
33 | +public class Step4_ClickAndRun { | |
34 | + private Task moveTo; | |
35 | + final private ScreenPosition goalPosition = new ScreenPosition(); | |
36 | + | |
37 | + public static void main(String[] args) { | |
38 | + new Step4_ClickAndRun(); | |
39 | + } | |
40 | + | |
41 | + public Step4_ClickAndRun() { | |
42 | + | |
43 | + // Destination is not specified at first. | |
44 | + goalPosition.setNotFound(true); | |
45 | + | |
46 | + // Initialize a robot. | |
47 | + final Robot robot = new MindstormsNXT("Mindstorms NXT", "btspp://00165305b308"); | |
48 | + | |
49 | + // Run a camera. | |
50 | + final Camera camera = new Camera(); | |
51 | + camera.start(); | |
52 | + | |
53 | + // Run a marker detector. | |
54 | + final MarkerDetector detector = new MarkerDetector(); | |
55 | + detector.setImageProvider(camera); | |
56 | + detector.put(new NapMarker("markers/4x4_743.patt", 12), robot); | |
57 | + detector.start(); | |
58 | + | |
59 | + // Show a configuration window. | |
60 | + final JFrame configFrame = new DisposeOnCloseFrame( | |
61 | + new TypicalMDCPane(detector)); | |
62 | + | |
63 | + // Initialize a main panel. | |
64 | + final ImageProviderPanel panel = new ImageProviderPanel(camera) { | |
65 | + private static final long serialVersionUID = 1L; | |
66 | + @Override public void paintComponent(Graphics g) { | |
67 | + super.paintComponent(g); | |
68 | + final Graphics2D g2 = (Graphics2D) g; | |
69 | + g2.translate(getOffsetX(), getOffsetY()); | |
70 | + | |
71 | + // Paint detection results. | |
72 | + g2.setStroke(new BasicStroke(4)); | |
73 | + g2.setColor(Color.orange); | |
74 | + detector.paint(g2); | |
75 | + | |
76 | + // Paint the destination. | |
77 | + if (!goalPosition.isNotFound()) { | |
78 | + g2.setColor(Color.green); | |
79 | + g2.fillOval( | |
80 | + goalPosition.getX()-5, | |
81 | + goalPosition.getY()-5, | |
82 | + 10, 10); | |
83 | + } | |
84 | + } | |
85 | + @Override public void dispose() { | |
86 | + configFrame.dispose(); | |
87 | + super.dispose(); | |
88 | + Matereal.getInstance().dispose(); | |
89 | + } | |
90 | + }; | |
91 | + | |
92 | + // Add a mouse listener. | |
93 | + panel.addMouseListener(new MouseAdapter() { | |
94 | + final private Position destination = | |
95 | + new Position(); | |
96 | + | |
97 | + @Override public void mouseClicked(MouseEvent e) { | |
98 | + if (e.getButton() == MouseEvent.BUTTON1) { | |
99 | + | |
100 | + // Get a clicked position. | |
101 | + goalPosition.set(e.getX(), e.getY()); | |
102 | + panel.getScreenToImageOut(goalPosition, goalPosition); | |
103 | + camera.screenToRealOut(goalPosition, destination); | |
104 | + | |
105 | + // Assign a task moving to the position. | |
106 | + final Task task = robot.getAssignedTask(WheelsController.class); | |
107 | + if (task != null) { | |
108 | + task.stop(); | |
109 | + } | |
110 | + moveTo = new Move(camera.screenToReal(goalPosition)); | |
111 | + if (moveTo.assign(robot)) { | |
112 | + moveTo.start(); | |
113 | + } | |
114 | + } | |
115 | + } | |
116 | + }); | |
117 | + | |
118 | + // Show a world. | |
119 | + final JFrame frame = new DisposeOnCloseFrame(panel); | |
120 | + frame.setSize(710, 660); | |
121 | + } | |
122 | +} |
@@ -16,6 +16,8 @@ | ||
16 | 16 | |
17 | 17 | import javax.swing.JFrame; |
18 | 18 | |
19 | +import sample.application.VisualizedClickAndRun; | |
20 | + | |
19 | 21 | import jp.digitalmuseum.mr.core.Matereal; |
20 | 22 | import jp.digitalmuseum.mr.core.entity.Robot; |
21 | 23 | import jp.digitalmuseum.mr.core.message.Event; |
@@ -101,7 +101,7 @@ | ||
101 | 101 | * Set pixel data. |
102 | 102 | * @param pixels pixel data in a byte array. |
103 | 103 | */ |
104 | - public void setData(final byte[] pixels) { this.pixels = pixels.clone(); } | |
104 | + public void setData(final byte[] pixels) { if (pixels != null) { this.pixels = pixels.clone(); } } | |
105 | 105 | |
106 | 106 | @Override public INyARRgbPixelReader getRgbPixelReader() { return pixelReader; } |
107 | 107 | public INyARBufferReader getBufferReader() { return bufferReader; } |
@@ -210,10 +210,14 @@ | ||
210 | 210 | if (imageProvider == null) { |
211 | 211 | return; |
212 | 212 | } |
213 | + byte[] imageData = imageProvider.getImageData(); | |
214 | + if (imageData == null) { | |
215 | + return; | |
216 | + } | |
213 | 217 | |
214 | 218 | // Detect markers. |
215 | 219 | final Array<NapDetectionResult> resultHolder |
216 | - = detector.detectMarker(imageProvider.getImageData()); | |
220 | + = detector.detectMarker(imageData); | |
217 | 221 | |
218 | 222 | // Manage results. |
219 | 223 | synchronized (this) { |