Context:
In the process of making this Small Hexapod
For moving the servos, I am using this ArduSnake Library.
Problem:
I compiled the ArduSnake Library Example file Oscillator_serial_1
but getting errors in the .cpp and .h library files
Oscillator_serial_1 (Library Example File):
#include <Servo.h>
#include <Oscillator.h>
//-- Serial commands
const unsigned char CMD_STOP = 'S'; //-- Stop the oscilaltor
const unsigned char CMD_PLAY = 'P'; //-- Play the oscillator
//-- Declare two oscillators
Oscillator osc;
void setup()
{
//-- Configure the serial comunication with PC
Serial.begin(9600);
//-- Attach the oscillator to servo
osc.attach(8);
//-- Initially the oscillator is stoped
osc.Stop();
//-- The oscillator is configured with the default parameters
}
//-- Incoming byte from the PC
unsigned char inbyte;
void loop()
{
//-- refresh the oscillator
osc.refresh();
//-- When a byte is received from the PC
if (Serial.available()) {
//-- Read the byte and modify the oscillator state
//-- acording to the command received
inbyte = Serial.read();
switch(inbyte){
case CMD_STOP: //-- Stop command
osc.Stop();
break;
case CMD_PLAY: //-- Play command
osc.Play();
break;
}
}
}
Error Seen on Arduino IDE:
In file included from C:\Users\Dielectric\AppData\Roaming\Arduino15\packages\arduino\hardware\sam\1.6.3\cores\arduino/WCharacter.h:23:0,
from C:\Users\Dielectric\AppData\Roaming\Arduino15\packages\arduino\hardware\sam\1.6.3\cores\arduino/Arduino.h:179,
from C:\Program Files (x86)\Arduino\libraries\ArduSnake\Oscillator.cpp:9:
C:\Program Files (x86)\Arduino\libraries\ArduSnake\Oscillator.h:54:12: error: expected unqualified-id before numeric constant
double _N; //-- Number of samples
^
C:\Program Files (x86)\Arduino\libraries\ArduSnake\Oscillator.cpp: In member function 'void Oscillator::attach(int, bool)':
C:\Program Files (x86)\Arduino\libraries\ArduSnake\Oscillator.cpp:48:6: error: lvalue required as left operand of assignment
_N = _T/_TS;
^
C:\Program Files (x86)\Arduino\libraries\ArduSnake\Oscillator.cpp: In member function 'void Oscillator::SetT(unsigned int)':
C:\Program Files (x86)\Arduino\libraries\ArduSnake\Oscillator.cpp:73:6:error: lvalue required as left operand of assignment
_N = _T/_TS;
^
Error compiling.
.cpp file & .h file:
--> Oscillator.cpp file
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include <pins_arduino.h>
#endif
#include "Oscillator.h"
#include <Servo.h>
//-- This function returns true if another sample
//-- should be taken (i.e. the TS time has passed since
//-- the last sample was taken
bool Oscillator::next_sample()
{
//-- Read current time
_currentMillis = millis();
//-- Check if the timeout has passed
if(_currentMillis - _previousMillis > _TS) {
_previousMillis = _currentMillis;
return true;
}
return false;
}
//-- Attach an oscillator to a servo
//-- Input: pin is the arduino pin were the servo
//-- is connected
void Oscillator::attach(int pin, bool rev)
{
//-- Attach the servo and move it to the home position
_servo.attach(pin);
_servo.write(90);
//-- Initialization of oscilaltor parameters
_TS=30;
_T=2000;
_N =_T/_TS;
_inc = 2*M_PI/_N;
_previousMillis=0;
//-- Default parameters
_A=45;
_phase=0;
_phase0=0;
_O=0;
_stop=false;
//-- Reverse mode
_rev = rev;
}
/*************************************/
/* Set the oscillator period, in ms */
/*************************************/
void Oscillator::SetT(unsigned int T)
{
//-- Assign the new period
_T=T;
//-- Recalculate the parameters
_N = (double)_T/(double)_TS;
_inc = 2*M_PI/_N;
};
/*******************************/
/* Manual set of the position */
/******************************/
void Oscillator::SetPosition(int position)
{
_servo.write(position+_trim);
};
/*******************************************************************/
/* This function should be periodically called */
/* in order to maintain the oscillations. It calculates */
/* if another sample should be taken and position the servo if so */
/*******************************************************************/
void Oscillator::refresh()
{
//-- Only When TS milliseconds have passed, the new sample is obtained
if (next_sample()) {
//-- If the oscillator is not stopped, calculate the servo position
if (!_stop) {
//-- Sample the sine function and set the servo pos
_pos = round(_A * sin(_phase + _phase0) + _O);
if (_rev) _pos=-_pos;
_servo.write(_pos+90+_trim);
}
//-- Increment the phase
//-- It is always increased, even when the oscillator is stop
//-- so that the coordination is always kept
_phase = _phase + _inc;
}
}
-->Oscillator.h file
#ifndef Oscillator_h
#define Oscillator_h
#include <Servo.h>
//-- Macro for converting from degrees to radians
#ifndef DEG2RAD
#define DEG2RAD(g) ((g)*M_PI)/180
#endif
class Oscillator
{
public:
Oscillator(int trim=0) {_trim=trim;};
void attach(int pin, bool rev =false);
void SetA(unsigned int A) {_A=A;};
void SetO(unsigned int O) {_O=O;};
void SetPh(double Ph) {_phase0=Ph;};
void SetT(unsigned int T);
void SetTrim(int trim){_trim=trim;};
int getTrim() {return _trim;};
void SetPosition(int position);
void Stop() {_stop=true;};
void Play() {_stop=false;};
void Reset() {_phase=0;};
void refresh();
private:
bool next_sample();
private:
//-- Servo that is attached to the oscillator
Servo _servo;
//-- Oscillators parameters
unsigned int _A; //-- Amplitude (degrees)
unsigned int _O; //-- Offset (degrees)
unsigned int _T; //-- Period (miliseconds)
double _phase0; //-- Phase (radians)
//-- Internal variables
int _pos; //-- Current servo pos
int _trim; //-- Calibration offset
double _phase; //-- Current phase
double _inc; //-- Increment of phase
double _N; //-- Number of samples
unsigned int _TS; //-- sampling period (ms)
long _previousMillis;
long _currentMillis;
//-- Oscillation mode. If true, the servo is stopped
bool _stop;
//-- Reverse mode
bool _rev;
};
#endif
Thanks in advance for checking out my problem