Go to Post Wisdom is knowing the difference between "the way to do things" and "the right way to do things." Just because an action may be physically possible does not mean that it is smart to do. - dlavery [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 02-19-2014, 02:33 PM
adlasa's Avatar
adlasa adlasa is offline
President and Lead Programmer
AKA: Sasha
FRC #1458 (Red Ties)
Team Role: Leadership
 
Join Date: Dec 2012
Rookie Year: 2011
Location: Bay Area
Posts: 35
adlasa has a spectacular aura aboutadlasa has a spectacular aura aboutadlasa has a spectacular aura about
Morse Code

Hello guys. I wrote a Morse Code class which takes a solenoid which powers our camera light, and a text to convert to Morse. It runs on a parallel thread and allows the camera to flash some fun and interesting messages.

Hope you like it,
Sasha

Code:
public class MorseCode implements Runnable{

	//To Call: (new Thread(new MorseCode("Hello World", caneraLight)).start();

	public static String sentance;
	public static String code[];
	public static Solenoid cameraLight;
	public static boolean isDone;

	public MorseCode(String inputSentance, Solenoid light){
		code = new String[inputSentance.length()];
		sentance = inputSentance;
		cameraLight = light;
		convertToMorse();
		isDone = false;
	}
	public void run(){
		cameraLight.set(false);
		try {
			Thread.sleep(4200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i = 0; i < code.length; i++){
			for(int j = 0; j < code[i].length(); j++){
				if(code[i].charAt(j) == '*'){
					cameraLight.set(true);
					try {
						Thread.sleep(600);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}else if(code[i].charAt(j) == '-'){
					cameraLight.set(true);
					try {
						Thread.sleep(1800);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}else if(code[i].charAt(j) == ' '){
					cameraLight.set(false);
					try {
						Thread.sleep(600);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			cameraLight.set(false);
			try {
				Thread.sleep(1800);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		isDone = true;
	}
	private void convertToMorse(){
		for(int i = 0; i < sentance.length(); i++){
			if(sentance.charAt(i)=='A' || sentance.charAt(i)=='a')
				code[i] = "* -";
			else if(sentance.charAt(i)=='B' || sentance.charAt(i)=='b')
				code[i] = "- * * *";
			else if(sentance.charAt(i)=='C' || sentance.charAt(i)=='c')
				code[i] = "- * - *";
			else if(sentance.charAt(i)=='D' || sentance.charAt(i)=='d')
				code[i] = "- * *";
			else if(sentance.charAt(i)=='E' || sentance.charAt(i)=='e')
				code[i] = "*";
			else if(sentance.charAt(i)=='F' || sentance.charAt(i)=='f')
				code[i] = "* * - *";
			else if(sentance.charAt(i)=='G' || sentance.charAt(i)=='g')
				code[i] = "- - *";
			else if(sentance.charAt(i)=='H' || sentance.charAt(i)=='h')
				code[i] = "* * * *";
			else if(sentance.charAt(i)=='I' || sentance.charAt(i)=='i')
				code[i] = "* *";
			else if(sentance.charAt(i)=='J' || sentance.charAt(i)=='j')
				code[i] = "* - - -";
			else if(sentance.charAt(i)=='K' || sentance.charAt(i)=='k')
				code[i] = "- * -";
			else if(sentance.charAt(i)=='L' || sentance.charAt(i)=='l')
				code[i] = "* - * *";
			else if(sentance.charAt(i)=='M' || sentance.charAt(i)=='m')
				code[i] = "- -";
			else if(sentance.charAt(i)=='N' || sentance.charAt(i)=='n')
				code[i] = "- *";
			else if(sentance.charAt(i)=='O' || sentance.charAt(i)=='o')
				code[i] = "- - -";
			else if(sentance.charAt(i)=='P' || sentance.charAt(i)=='p')
				code[i] = "* - - *";
			else if(sentance.charAt(i)=='Q' || sentance.charAt(i)=='q')
				code[i] = "- - * -";
			else if(sentance.charAt(i)=='R' || sentance.charAt(i)=='r')
				code[i] = "* - *";
			else if(sentance.charAt(i)=='S' || sentance.charAt(i)=='s')
				code[i] = "* * *";
			else if(sentance.charAt(i)=='T' || sentance.charAt(i)=='t')
				code[i] = "-";
			else if(sentance.charAt(i)=='U' || sentance.charAt(i)=='u')
				code[i] = "* * -";
			else if(sentance.charAt(i)=='V' || sentance.charAt(i)=='v')
				code[i] = "* * * -";
			else if(sentance.charAt(i)=='W' || sentance.charAt(i)=='w')
				code[i] = "* - -";
			else if(sentance.charAt(i)=='X' || sentance.charAt(i)=='x')
				code[i] = "- * * -";
			else if(sentance.charAt(i)=='Y' || sentance.charAt(i)=='y')
				code[i] = "- * - -";
			else if(sentance.charAt(i)=='Z' || sentance.charAt(i)=='z')
				code[i] = "- - * *";
			else if(sentance.charAt(i)=='0')
				code[i] = "- - - - -";
			else if(sentance.charAt(i)=='1')
				code[i] = "* - - - -";
			else if(sentance.charAt(i)=='2')
				code[i] = "* * - - -";
			else if(sentance.charAt(i)=='3')
				code[i] = "* * * - -";
			else if(sentance.charAt(i)=='4')
				code[i] = "* * * * -";
			else if(sentance.charAt(i)=='5')
				code[i] = "* * * * *";
			else if(sentance.charAt(i)=='6')
				code[i] = "- * * * *";
			else if(sentance.charAt(i)=='7')
				code[i] = "- - * * *";
			else if(sentance.charAt(i)=='8')
				code[i] = "- - - * *";
			else if(sentance.charAt(i)=='9')
				code[i] = "- - - - *";
			else
				code[i] = " ";
		}
	}
}
__________________
Computers run on smoke. When the smoke escapes the computers stop working.
Reply With Quote
  #2   Spotlight this post!  
Unread 02-19-2014, 02:42 PM
nxtmonkeys's Avatar
nxtmonkeys nxtmonkeys is offline
SCP-682
AKA: Parker Wells
FRC #5349 (RoboEagles)
Team Role: Mentor
 
Join Date: Feb 2014
Rookie Year: 2014
Location: New York
Posts: 213
nxtmonkeys will become famous soon enough
Re: Morse Code

Veeeery eeeeenteresting.


No, really, That's cool. Was it hard to do?

Last edited by nxtmonkeys : 02-19-2014 at 02:43 PM. Reason: Cuz it sounded VERY sarcastic
Reply With Quote
  #3   Spotlight this post!  
Unread 02-19-2014, 02:53 PM
adlasa's Avatar
adlasa adlasa is offline
President and Lead Programmer
AKA: Sasha
FRC #1458 (Red Ties)
Team Role: Leadership
 
Join Date: Dec 2012
Rookie Year: 2011
Location: Bay Area
Posts: 35
adlasa has a spectacular aura aboutadlasa has a spectacular aura aboutadlasa has a spectacular aura about
Re: Morse Code

No not really. This was my first time using Java threading but it turns out its fairly easy to use.

The time consuming part was typing the Morse code database.
__________________
Computers run on smoke. When the smoke escapes the computers stop working.
Reply With Quote
  #4   Spotlight this post!  
Unread 02-19-2014, 05:33 PM
DonRotolo's Avatar
DonRotolo DonRotolo is offline
Back to humble
FRC #0832
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Atlanta GA
Posts: 6,979
DonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond reputeDonRotolo has a reputation beyond repute
Re: Morse Code

Talk about the ultimate status display. So long as your drivers know Code, they can get quite a lot of data off the robot.

(Yes, I know there are other ways, too)
__________________

I am N2IRZ - What's your callsign?
Reply With Quote
  #5   Spotlight this post!  
Unread 02-20-2014, 12:13 AM
ekapalka's Avatar
ekapalka ekapalka is offline
Registered User
FRC #3216
 
Join Date: Dec 2012
Location: Bermuda
Posts: 277
ekapalka has a spectacular aura aboutekapalka has a spectacular aura about
Re: Morse Code

Is using threads in Java as "risky" as it is in C++? By that I'm referencing this incident. I sort-of caught some heat from one of our mentors for attempting to show our new programmers how to use threads because there's a lot of potential unintended coordination issues (even if the threads are basically acting as their own separate programme, like this one appears to be). Thanks, and this project seems really cool :)
Reply With Quote
  #6   Spotlight this post!  
Unread 02-20-2014, 10:15 PM
heuristics heuristics is offline
Registered User
FRC #3634
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2014
Location: Trumbull, CT
Posts: 21
heuristics is on a distinguished road
Re: Morse Code

Threading is only "risky" if you don't properly synchronize the data you're sharing across the threads, and only if synchronization matters for the specific task. I wouldn't say either language is necessarily more difficult to synchronize. In both, you need to think through your critical sections, make sure you're locking mutexes, semaphores, etc. in the appropriate places.
Reply With Quote
  #7   Spotlight this post!  
Unread 02-21-2014, 08:44 AM
Bill_B Bill_B is offline
You cannot not make a difference
FRC #2170
 
Join Date: Jan 2010
Rookie Year: 2004
Location: Connecticut
Posts: 1,099
Bill_B has a reputation beyond reputeBill_B has a reputation beyond reputeBill_B has a reputation beyond reputeBill_B has a reputation beyond reputeBill_B has a reputation beyond reputeBill_B has a reputation beyond reputeBill_B has a reputation beyond reputeBill_B has a reputation beyond reputeBill_B has a reputation beyond reputeBill_B has a reputation beyond reputeBill_B has a reputation beyond repute
Re: Morse Code

Don't show your code to your English teacher. You could be "sentanced" to remedial spelling assignments.
__________________
Nature's Fury FLL team 830 - F L eLements
FRC team 2170 - Titanium Tomahawks
Reply With Quote
  #8   Spotlight this post!  
Unread 02-21-2014, 11:03 AM
nickmcski nickmcski is offline
Registered User
AKA: Nicholas McCurry
FRC #1482 (Grandin Ghosts)
Team Role: Alumni
 
Join Date: Nov 2012
Rookie Year: 2012
Location: Canada
Posts: 98
nickmcski has a spectacular aura aboutnickmcski has a spectacular aura aboutnickmcski has a spectacular aura about
Is there a reason you used else if instead of a switch statement?
Reply With Quote
  #9   Spotlight this post!  
Unread 02-21-2014, 03:25 PM
SousVide SousVide is offline
Registered User
no team
 
Join Date: Jan 2011
Location: CA
Posts: 91
SousVide is a splendid one to beholdSousVide is a splendid one to beholdSousVide is a splendid one to beholdSousVide is a splendid one to beholdSousVide is a splendid one to beholdSousVide is a splendid one to beholdSousVide is a splendid one to behold
Re: Morse Code

Great Job on the thread code.
Reply With Quote
  #10   Spotlight this post!  
Unread 02-22-2014, 10:04 AM
taichichuan's Avatar
taichichuan taichichuan is offline
Software Mentor
AKA: Mike Anderson
FRC #0116 (Epsilon Delta)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2010
Location: Herndon, VA
Posts: 328
taichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud oftaichichuan has much to be proud of
Send a message via AIM to taichichuan
Re: Morse Code

Don't be afraid of threads. They are a very basic mechanism in any real-time or embedded code development. I know Dave Wilner personally and was involved in the periphery of the Pathfinder problem.

The confusion came as a result of something called the priority inversion problem that is common in real-time systems. In VxWorks, there are 3 different kinds of semaphores that are used for different purposes. The one that's used in the "CRITICAL_REGION/END_REGION" code is the mutual exclusion semaphore that caused the problems for Pathfinder. The problem with Pathfinder was that that semaphore was not created with the SEM_INVERSION_SAFE flag set. However, the constructor used in WPILib automatically set that flag, so you should be good.

Just make sure you protect any global variable accesses in the CRITICAL_REGION/END_REGION wrappers and everything should work fine.

Good Luck,

Mike
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 03:43 AM.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi