Hey everyone,
So i have this projects where i should be controlling a BLDC motor wirelessly. I should make it at least start and stop just by entering certain character on the keyboard. BTW, im using the latest Teensyduino and a CC1101. As a newbie, i really dont have any idea except that i send something to the receiver as i have two CC1101, and only after the receiver got the data, the program of my BLDC Motor start to run. But after weeks trying to figure out, i am still lost as the data that received (im using the command of Serial.vailable and Serial.read )is always a -1. Just for starting the motor, i might just use the value of -1. But i cant have any other command as it only have "-1" value regardless of what ever i send on the transmitter.
here is my transmitter code
and here is the rx code.
Help this newbie
So i have this projects where i should be controlling a BLDC motor wirelessly. I should make it at least start and stop just by entering certain character on the keyboard. BTW, im using the latest Teensyduino and a CC1101. As a newbie, i really dont have any idea except that i send something to the receiver as i have two CC1101, and only after the receiver got the data, the program of my BLDC Motor start to run. But after weeks trying to figure out, i am still lost as the data that received (im using the command of Serial.vailable and Serial.read )is always a -1. Just for starting the motor, i might just use the value of -1. But i cant have any other command as it only have "-1" value regardless of what ever i send on the transmitter.
here is my transmitter code
PHP:
/* Routinen für Teensy */
/* mit Pinning: */
/* Teensy Pin CC1101 Funktion */
/* 6 CS */
/* 11 MOSI */
/* 12 MISO */
/* 14 SCK */
/* 17 GD0 */
#include <SPI.h>
#include "ELECHOUSE_CC1101.h"
#define tx
#ifdef tx
#define size 2
byte TX_buffer[size]={0};
byte i;
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (Serial.available()==0);
int val = Serial.read();
if (val=='s'){
ELECHOUSE_cc1101.Init();
for(i=1;i<size;i++)
{
TX_buffer[i]=i;
}
ELECHOUSE_cc1101.SendData(TX_buffer,size);
delay(1);
Serial.println("starting..");
}
while (Serial.available()==0);
int val = Serial.read();
if (val=='s'){
ELECHOUSE_cc1101.Init();
for(i=1;i<size;i++)
{
TX_buffer[i]=i;
}
ELECHOUSE_cc1101.SendData(TX_buffer,size);
delay(1);
Serial.println("starting..");
}
}
#endif
and here is the rx code.
PHP:
#include "bldc_control.h"
#include "I2Cdev.h"
//#include "MPU6050_6Axis_MotionApps20.h"
#include "ELECHOUSE_CC1101.h"
#include <Wire.h>
#include <SPI.h>
#define TASTER 23
#define LED1 13
#define LED2 10
//MPU6050 mpu;
BLDC_Control motor(true);
bool dmpReady = false; // set true if DMP init was successful
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount; // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer
const int N = 200; //Anzahl der beachteten Werte für Init-Rdy Prüdung
double x[N];
int cnt = 0;
boolean steady = false;
double offset;
double yaw;
int speed = 0;
int8_t incomingByte;
void setup()
{
Serial.begin(9600);
ELECHOUSE_cc1101.Init();
ELECHOUSE_cc1101.SetReceive();
Wire.begin();
TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
//pinMode(15,INPUT);
pinMode(TASTER,INPUT);
pinMode(LED1,OUTPUT);
pinMode(LED2,OUTPUT);
//motor.Init();
dmpReady = true;
}
byte RX_buffer[2]={0};
byte size,i,flag;
void loop()
{
if(ELECHOUSE_cc1101.CheckReceiveFlag())
{
size=ELECHOUSE_cc1101.ReceiveData(RX_buffer);
for(i=1;i<size;i++)
{
Serial.print(RX_buffer[i],DEC);
Serial.print(char(' '));
}
Serial.println("");
ELECHOUSE_cc1101.SetReceive();
while (Serial.available()>=2); // check to see if there is data available
incomingByte = Serial.read(); // read the incoming data
delay (1);
Serial.print ("receiving: ");
Serial.println (incomingByte);
}
if (incomingByte==-1)
{
motor.Init();
motor.setDirection(true); // Motor verhalten nicht symmetrisch bei true i.o. auch mit Startverhalten, aber nicht bei false
Serial.println("test1");
speed = 800;
digitalWrite(LED1,LOW);
digitalWrite(LED2,HIGH);
Serial.println("test2");
motor.setSpeed(speed);
motor.StartUp();
//motor.FreeRun(); Freerun schaltet den Motor nicht aus !!! Nur speed = 0
delay(4000);
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
speed = 0;
motor.setSpeed(speed);
delay(4000);
motor.setDirection(false);
speed = 800;
motor.setSpeed(speed);
motor.StartUp();
delay(4000);
speed = 0;
motor.setSpeed(speed);
Serial.println("test3");
delay(1000);
}
}
boolean isSteady()
{
double average_firstHalf = 0;
double average_secondHalf = 0;
int cnt;
for(cnt=0;cnt<(N/2);cnt++) average_firstHalf+=x[cnt];
average_firstHalf/=(N/2);
for(;cnt<N;cnt++) average_secondHalf+=x[cnt];
average_secondHalf/=(N/2);
if(abs((average_firstHalf/average_secondHalf)-1)<0.005) return true;
return false;
}
Help this newbie