public ArmAutoControl(MyIni ini, float angle, WheelsController wc, List <IMyFunctionalBlock> tools) { this.tools = tools; this.wc = wc; if (ini.ContainsSection(SECTION)) { this.Target = new ArmPos(ini.Get(SECTION, "pos").ToString()); } else { this.SetTarget(new ArmPos(angle)); } }
/// <summary> /// gets ArmPosition from file /// </summary> /// <returns>bool that indicates, if operation was successful</returns> private void GetArmPositionFromFile() { try { //get data from file, using StreamReader using (var sr = new StreamReader("arm_position.dat")) { _armPosition = (ArmPos)Enum.Parse(typeof(ArmPos), sr.ReadToEnd()); } } catch { ArmPosition = ArmPos.Between; } }
private void MoveArm(ArmPos pos) { WaitForArmReady(); waitArm.Reset(); int currTacho = motorArm.GetTachoCount(); int move = armPositions[pos] - currTacho; sbyte speed = move < 0 ? (sbyte)-50 : (sbyte)50; int rampDownVal = Math.Abs(move) > 15 ? 15 : 0; WaitHandle waitHandle = motorArm.SpeedProfile(speed, 0, (uint)Math.Abs(move) - (uint)rampDownVal, (uint)rampDownVal, true); Task.Factory.StartNew(() => { waitHandle.WaitOne(); waitArm.Set(); }); }
private ArmPos WasArmMoved(Skeleton skeleton) { //0' is directly up var angle = CalculateJointAngle(skeleton.Joints[JointType.ElbowLeft], skeleton.Joints[JointType.WristLeft]); if (angle > 305) { prevArmPos = ArmPos.Raised; return(ArmPos.Normal); } //slightly angled down if (angle < 235) { prevArmPos = ArmPos.Lowered; return(ArmPos.Normal); } return(prevArmPos); }
public void SetTarget(ArmPos pos) { this.SwitchTools(false); this.Target = pos; }
private void MoveArm(ArmPos pos) { WaitForArmReady(); waitArm.Reset(); int currTacho = motorArm.GetTachoCount(); int move = armPositions[pos] - currTacho; sbyte speed = move < 0 ? (sbyte) -50 : (sbyte) 50; int rampDownVal = Math.Abs(move) > 15 ? 15 : 0; WaitHandle waitHandle = motorArm.SpeedProfile(speed, 0, (uint) Math.Abs(move) - (uint) rampDownVal, (uint) rampDownVal, true); Task.Factory.StartNew(() => { waitHandle.WaitOne(); waitArm.Set(); }); }
/// <summary> /// The main update loop, happens every frame /// </summary> /// <param name="skeleton">the skeleton data of the player</param> private void Update(Skeleton skeleton) { CalculateDist(skeleton); UpdateGraphs(); ChangeInstrument(skeleton); armLength = CalculateArmLength(skeleton); foreach (Peak p in leftArmPeaks) { p.TimeStep(); } //check if right leg stomped if (LegStomp(false, skeleton)) { if (isCalculatingBPM) { bpmCalculatingLabel.Text = "BPM"; var greyBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#6e6e6e")); bpmCalculatingLabel.Foreground = greyBrush; bpmCounterLabel.Foreground = greyBrush; } else { bpmCalculatingLabel.Text = "BPM (Calculating...)"; bpmCalculatingLabel.Foreground = new SolidColorBrush(Colors.Red); bpmCounterLabel.Foreground = new SolidColorBrush(Colors.Red); } isCalculatingBPM = !isCalculatingBPM; //Console.WriteLine("Right leg stomp"); } //check if left leg stomped else if (LegStomp(true, skeleton)) { //Console.WriteLine("Left leg stomp"); //if currently recording and new record message comes in then wipe and start again if (isRecording) { recordedNotes.RemoveAt(recordedNotes.Count - 1); } if (shouldRecord) { shouldRecord = false; recordingLabel.Text = "Not Recording"; var greyBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#6e6e6e")); recordingLabel.Foreground = greyBrush; recordingCounter.Foreground = greyBrush; } else { recordedNotes.Add(new Note[numberOfBeats]); shouldRecord = true; isRecording = false; recordingLabel.Text = "Will Record"; recordingLabel.Foreground = new SolidColorBrush(Colors.Orange); recordingCounter.Foreground = new SolidColorBrush(Colors.Orange); } } if (isCalculatingBPM) { UpdatePeaks(); CalculateBPM(); } else { int noteValue = currentNoteValue; if (typeSlider.Value == 0) { var wasArmMoved = WasArmMoved(skeleton); if (wasArmMoved != ArmPos.Normal) { //Console.WriteLine("Arm moved!"); prevArmPos = ArmPos.Normal; if (wasArmMoved == ArmPos.Raised) { currentNoteValue++; } else { currentNoteValue--; } currentNoteValue = Math.Max(24, Math.Min(currentNoteValue, 127)); noteValue = currentNoteValue; if (useScale) { var newNoteVal = Math.Floor(Convert.ToDouble(currentNoteValue) / currentScale.Length) * currentScale.Length; //Console.WriteLine("New Note Val: " + newNoteVal); var octave = newNoteVal / currentScale.Length; //Console.WriteLine("Octave: " + octave); var leftOver = currentNoteValue - newNoteVal; //Console.WriteLine("Leftover: " + leftOver); var tempNote = (int)(12 * octave + currentScale[(int)leftOver]); //Console.WriteLine("Old note val: " + noteValue); noteValue = Math.Max(24, tempNote); } //Console.WriteLine(currentNoteValue); //Console.WriteLine("Note value: " + noteValue); } } var shouldTick = bpm.ShouldTick(); if (shouldTick > 0) { //remove playing notes while (playingNotes.Count > 0 && playingNotes.Min().GetTimeEnd() < currentTime) { Note endingNote = playingNotes.ExtractDominating().getNote(); endingNote = endingNote.EndNote(); EndNote(handle, endingNote); } int duration = 3; var dist = skeleton.Joints[JointType.ShoulderLeft].Position.X - skeleton.Joints[JointType.WristLeft].Position.X; //play a note if (typeSlider.Value == 1) { double armHeight = CalculateJointHeight(skeleton.Joints[JointType.WristLeft], skeleton.Joints[JointType.ShoulderLeft], armLength); duration = (int)((dist / Convert.ToDouble(armLength)) * 12); if (useScale) { var newNoteVal = Math.Floor(armHeight / currentScale.Length) * currentScale.Length; //Console.WriteLine("New Note Val: " + newNoteVal); var octave = newNoteVal / currentScale.Length; //Console.WriteLine("Octave: " + octave); var leftOver = armHeight - newNoteVal; //Console.WriteLine("Leftover: " + leftOver); noteValue = (int)(12 * octave + currentScale[(int)leftOver]); noteLabel.Text = noteList[currentScale[(int)leftOver]]; //Console.WriteLine("Old note val: " + noteValue); noteValue = Math.Max(0, noteValue); } else { noteValue = (int)armHeight; } noteValue += 24; //Console.WriteLine("New Note: " + noteValue); } if (duration > 0) { Note thisNote = new Note(noteValue, currentInstrument, duration); //Console.WriteLine("Playing note! " + currentTime + " Duration: "+duration); PlayNote(handle, thisNote); if (isRecording) { recordedNotes[recordedNotes.Count - 1][beatCounter] = thisNote; //Console.WriteLine("Recording note at " + beatCounter + " " + recordedNotes[recordedNotes.Count - 1][beatCounter]); } } var maxRecordings = recordedNotes.Count; if (maxRecordings > 0) { if (isRecording) { maxRecordings--; } for (int i = 0; i < maxRecordings; i++) { if (recordedNotes[i][beatCounter] != null) { PlayNote(handle, recordedNotes[i][beatCounter]); //Console.WriteLine("Playing recorded note at: " + beatCounter + " out of " + recordedNotes[i].Count()); } } } currentTime++; if (shouldTick == 1) { //play drum beat, needs to take what type if (currentDrum.GetType() != typeof(Rest)) { currentDrum.UpdateProbabilites(energy); if (currentDrum.GetType() == typeof(HiHat)) { PlayNote(handle, new Note(currentDrum.GetNote(), 99, 90)); } else { PlayNote(handle, new Note(currentDrum.GetNote(), 99, 117)); } } currentDrum = currentDrum.GetNextNode(); } // update recording. // shouldTick == 2 else { if (currentTick > 1) { currentTick--; } else { currentTick = loopLength; if (shouldRecord) { shouldRecord = false; isRecording = true; recordingLabel.Text = "Recording"; recordingLabel.Foreground = new SolidColorBrush(Colors.Red); recordingCounter.Foreground = new SolidColorBrush(Colors.Red); } else if (isRecording) { isRecording = false; recordingLabel.Text = "Not Recording"; var greyBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#6e6e6e")); recordingLabel.Foreground = greyBrush; recordingCounter.Foreground = greyBrush; } } recordingCounter.Text = currentTick.ToString(); } beatCounter++; if (beatCounter >= numberOfBeats) { beatCounter = 0; } if (beatCounter % 2 == 0) { currentDrum = new Kick(); if (playKick) { PlayNote(handle, new Note(currentDrum.GetNote(), 99, 127)); } else { PlayNote(handle, new Note(38, 99, 127)); } playKick = !playKick; energy = GetEnergy(); currentDrum.UpdateProbabilites(energy); currentDrum = currentDrum.GetNextNode(); } } } }