From: Thomas Pietrzak Date: Wed, 2 Mar 2022 08:13:02 +0000 (+0100) Subject: SetAngle X-Git-Url: https://git.thomaspietrzak.com/?a=commitdiff_plain;h=f2b607ab63e6780912e10135b0c912ae822849f8;p=tactonlibrary.git SetAngle --- diff --git a/Arduino/TactonPlayer/TactonManager.cpp b/Arduino/TactonPlayer/TactonManager.cpp index ddc9717..92e0706 100644 --- a/Arduino/TactonPlayer/TactonManager.cpp +++ b/Arduino/TactonPlayer/TactonManager.cpp @@ -130,3 +130,32 @@ void TactonManager::stop() { _player->stop(); } + +void TactonManager::setAngle(unsigned int angle) +{ + _player->setAngle(angle); +} + +void TactonManager::setAngleSequence(unsigned int nbangles, unsigned int frameduration, unsigned int nbframes, byte *angles) +{/* + Serial.print("Nb angles "); + Serial.println(nbangles); + Serial.print("Frame duration "); + Serial.println(frameduration); + Serial.print("Nb frames "); + Serial.println(nbframes);*/ + + unsigned int a[nbangles]; + for (int i = 0 ; i < nbangles ; i++) + a[i] = (angles[2 * i] << 8) |angles[2 * i + 1]; + + for (int i = 0 ; i < nbframes ; i++) + { + setAngle(a[i % nbangles]); +/* Serial.print("Angle "); + Serial.println(a[i % nbangles]);*/ + // /!\ don't forget to multiply if the timer was changed in TactonPlayer + delay(64 * frameduration); + } + _player->clear(); +} diff --git a/Arduino/TactonPlayer/TactonManager.h b/Arduino/TactonPlayer/TactonManager.h index e2e83b8..c687c07 100644 --- a/Arduino/TactonPlayer/TactonManager.h +++ b/Arduino/TactonPlayer/TactonManager.h @@ -26,6 +26,9 @@ class TactonManager void setAmplitudes(unsigned int nbf, byte *desc); void setFrequency(unsigned int freq); void stop(); + //angle in degrees made with 4 vibrators + void setAngle(unsigned int angle); + void setAngleSequence(unsigned int nbangles, unsigned int frameduration, unsigned int nbframes, byte *angles); void clear(); diff --git a/Arduino/TactonPlayer/TactonPlayer.cpp b/Arduino/TactonPlayer/TactonPlayer.cpp index 8471061..aba6145 100644 --- a/Arduino/TactonPlayer/TactonPlayer.cpp +++ b/Arduino/TactonPlayer/TactonPlayer.cpp @@ -12,4 +12,11 @@ TactonPlayer::TactonPlayer(byte nbtactors, byte *pins) } } +void TactonPlayer::clear() +{ + for (int i = 0 ; i < _nbtactors ; i++) + digitalWrite(_pins[i], LOW); +} + + void __cxa_pure_virtual(void) {}; \ No newline at end of file diff --git a/Arduino/TactonPlayer/TactonPlayer.h b/Arduino/TactonPlayer/TactonPlayer.h index 3294fb6..e20f02a 100644 --- a/Arduino/TactonPlayer/TactonPlayer.h +++ b/Arduino/TactonPlayer/TactonPlayer.h @@ -6,6 +6,11 @@ //if using more than 8, change type of pattern #define MAXTACTORS 8 +#define TACTOR_RIGHT 0 +#define TACTOR_UP 1 +#define TACTOR_LEFT 2 +#define TACTOR_DOWN 3 + extern "C" void __cxa_pure_virtual(void); class TactonPlayer @@ -19,10 +24,14 @@ class TactonPlayer //set a different amplitude for each vibrator virtual void setAmplitudes(byte nbtactors, byte amplitudes[]) = 0; // virtual void setPattern(byte pattern) { _pattern = pattern; } + virtual void setAngle(unsigned int angle) = 0; //Stop any vibration virtual void stop() = 0; + + //Clear the pattern, the frequency remains + virtual void clear(); //8bits pattern => max 8 tactors, change type if using more virtual void beep(byte pattern, unsigned long duration, unsigned int frequency, byte amplitude) = 0; diff --git a/Arduino/TactonPlayer/TactonPlayerPreciseNew.cpp b/Arduino/TactonPlayer/TactonPlayerPreciseNew.cpp index 9918eab..6b6f32e 100644 --- a/Arduino/TactonPlayer/TactonPlayerPreciseNew.cpp +++ b/Arduino/TactonPlayer/TactonPlayerPreciseNew.cpp @@ -21,8 +21,12 @@ void TactonPlayerPreciseNew::init() const //set prescalar for timer0 and timer2 to 1 // /!\ this affects delay! + TCCR0B |= (1 << CS00); TCCR0B &= ~(1 << CS01); + TCCR0B &= ~(1 << CS02); + TCCR2B |= (1 << CS20); TCCR2B &= ~(1 << CS21); + TCCR2B &= ~(1 << CS22); } void TactonPlayerPreciseNew::setFrequency(unsigned int frequency) @@ -64,6 +68,34 @@ void TactonPlayerPreciseNew::setAmplitudes(byte nbtactors, byte *amplitudes) analogWrite(_pins[i], amplitudes[i]); } +void TactonPlayerPreciseNew::setAngle(unsigned int angle) +{ + //vertical + if (angle < 180) + { + analogWrite(_pins[TACTOR_UP], 255 * sin(angle * M_PI / 180.0)); + analogWrite(_pins[TACTOR_DOWN], 0); + } + else + { + analogWrite(_pins[TACTOR_DOWN], - 255 * sin(angle * M_PI / 180.0)); + analogWrite(_pins[TACTOR_UP], 0); + } + + //horizontal + if (angle < 90 || angle > 270) + { + analogWrite(_pins[TACTOR_RIGHT], 255 * cos(angle * M_PI / 180.0)); + analogWrite(_pins[TACTOR_LEFT], 0); + } + else + { + analogWrite(_pins[TACTOR_RIGHT], 0); + analogWrite(_pins[TACTOR_LEFT], - 255 * cos(angle * M_PI / 180.0)); + } +} + + //Stop any vibration void TactonPlayerPreciseNew::stop() { @@ -72,10 +104,9 @@ void TactonPlayerPreciseNew::stop() OCR1A = 0; OCR1B = 0; //clear the pattern - for (int i = 0 ; i < _nbtactors ; i++) - digitalWrite(_pins[i], LOW); + clear(); } - + //Play a Tacton for a specified duration, frequency and amplitude void TactonPlayerPreciseNew::beep(byte pattern, unsigned long duration, unsigned int frequency, byte amplitude) { diff --git a/Arduino/TactonPlayer/TactonPlayerPreciseNew.h b/Arduino/TactonPlayer/TactonPlayerPreciseNew.h index 796b3c4..6b3acc8 100644 --- a/Arduino/TactonPlayer/TactonPlayerPreciseNew.h +++ b/Arduino/TactonPlayer/TactonPlayerPreciseNew.h @@ -13,6 +13,8 @@ class TactonPlayerPreciseNew: public TactonPlayer void setFrequency(unsigned int frequency); //sets a different amplitude for each vibrator void setAmplitudes(byte nbtactors, byte *amplitudes); + + void setAngle(unsigned int angle); //Stop any vibration void stop();