Port Controled WiFi RC Car.

Discussion about any of the included sketches (i.e. WebServer), or user-generated applications.

Re: Port Controled WiFi RC Car.

Postby rj44319 » Sat Feb 27, 2010 10:13 pm

Sorry I have it!
Code: Select all
/*
* Socket App to control Radio Control Car
*
* Loosly based off the simple socket application example using the WiShield 1.0
*/

#include <WiShield.h>
#include <Servo.h>

#define WIRELESS_MODE_INFRA   1
#define WIRELESS_MODE_ADHOC   2

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,2};   // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1};   // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0};   // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"dd-wrt"};      // max 32 bytes

unsigned char security_type = 0;   // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"12345678"};   // max 64 characters

// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = {   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,   // Key 0
                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   0x00,   // Key 1
                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   0x00,   // Key 2
                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   0x00   // Key 3
                        };

// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

unsigned char ssid_len;
unsigned char security_passphrase_len;
//---------------------------------------------------------------------------
char buffer[20];

int pinArray[2] = {3, 5}; // digital pins for the servos
int minPulse = 500;             // minimum servo position
int maxPulse = 2500;            // maximum servo position
int refreshTime =  20;          // time (ms) between pulses (50Hz)

int i;              // iterator
int servoPin;       // control pin for current servo
int pulseWidth;     // servo pulse width
int servoPosition;  // commanded servo position, 0-180 degrees
int pulseRange;     // maxPulse - minPulse
int centerServo;    // servo starting point
long lastPulse = 0; // recorded time (ms) of the last pulse
int servo;          // which servo to pulse
int servo1[2];      // servo #1 array{pin, pulsewidth}
int servo2[2];      // servo #2 array{pin, pulsewidth}
int pin;            // digital pin for pulse() function
int puls;           // pulsewidth for pulse() function
int posout;
int pos[2];

void setup()
{
  WiFi.init();
  // loop through all 4 servo pins
  // and set them as OUTPUT
  for (i=0;i<2;i++) {
    pinMode(pinArray[i], OUTPUT);
  } 
  // servo starting point (center)
  pulseRange  = maxPulse - minPulse;
  centerServo = maxPulse - ((pulseRange)/2);
  pulseWidth  = centerServo;
  // map pins to servos
  servo1[0] = pinArray[0];  // servo #1 is pin 3
  servo2[0] = pinArray[1];  // servo #2 is pin 4
  // center all servos
  servo1[1] = pulseWidth;
  servo2[1] = pulseWidth;
}

void loop()
{
  WiFi.run(); 
 
  if(buffer[0] == 'A'){
    //Get position from string
    pos[0] = servopos(buffer[1]);
    pos[1] = servopos(buffer[2]);
    //Calculate Pulse Position
    servo1[1] = calpulse(pos[0]);
    servo2[1] = calpulse(pos[1]);
       
  memset(buffer, 0x00, sizeof(buffer));
  }
   
   //pulse each servo
  if (millis() - lastPulse >= refreshTime) {
    pulse(servo1[0], servo1[1]);
    pulse(servo2[0], servo2[1]);
   // save the time of the last pulse
    lastPulse = millis();
  }
}

void pulse(int pin, int puls) {
    digitalWrite(pin, HIGH); // start the pulse
    delayMicroseconds(puls); // pulse width
    digitalWrite(pin, LOW);  // stop the pulse
}

int calpulse(int srvpos) {
  if (srvpos == 255) { servo = 255; }
  // compute pulseWidth from servoPosition
  pulseWidth = minPulse + (srvpos * (pulseRange/180));
  // stop servo pulse at min and max
  if (pulseWidth > maxPulse) { pulseWidth = maxPulse; }
  if (pulseWidth < minPulse) { pulseWidth = minPulse; }
  return pulseWidth;
}

int servopos(int srvpos){
  if(srvpos == 'A'){posout = 0;}
  else if(srvpos == 'B'){posout = 0;}
  else if(srvpos == 'C'){posout = 10;}
  else if(srvpos == 'D'){posout = 20;}
  else if(srvpos == 'E'){posout = 30;}
  else if(srvpos == 'F'){posout = 40;}
  else if(srvpos == 'G'){posout = 50;}
  else if(srvpos == 'H'){posout = 60;}
  else if(srvpos == 'I'){posout = 70;}
  else if(srvpos == 'J'){posout = 80;}
  else if(srvpos == 'K'){posout = 90;}
  else if(srvpos == 'L'){posout = 100;}
  else if(srvpos == 'M'){posout = 110;}
  else if(srvpos == 'N'){posout = 120;}
  else if(srvpos == 'O'){posout = 130;}
  else if(srvpos == 'P'){posout = 140;}
  else if(srvpos == 'Q'){posout = 150;}
  else if(srvpos == 'R'){posout = 160;}
  else if(srvpos == 'S'){posout = 170;}
  else if(srvpos == 'T'){posout = 180;}
  else if(srvpos == 'U'){posout = 180;}
  return posout;
}


then here is the socket app. Here is were the modification is.

Code: Select all

/******************************************************************************

  Filename:      socketapp.c
  Description:   Simple socket programming example for the WiShield 1.0

******************************************************************************

  TCP/IP stack and driver for the WiShield 1.0 wireless devices

  Copyright(c) 2009 Async Labs Inc. All rights reserved.

  This program is free software; you can redistribute it and/or modify it
  under the terms of version 2 of the GNU General Public License as
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc., 59
  Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  Contact Information:
  <[email protected]>

   Author               Date        Comment
  ---------------------------------------------------------------
   AsyncLabs         06/06/2009   Initial version

*****************************************************************************/

/*
* This is a short example of how to write uIP applications using
* protosockets.
*/

/*
* We define the application state (struct socket_app_state) in the
* socketapp.h file, so we need to include it here. We also include
* uip.h (since this cannot be included in socketapp.h) and
* <string.h>, since we use the memcpy() function in the code.
*/
#include "socketapp.h"
#include "uip.h"
#include <string.h>

/*
* Declaration of the protosocket function that handles the connection
* (defined at the end of the code).
*/
static int handle_connection(struct socket_app_state *s);
/*---------------------------------------------------------------------------*/
/*
* The initialization function. We must explicitly call this function
* from the system initialization code, some time after uip_init() is
* called.
*/
void socket_app_init(void)
{
  /* We start to listen for connections on TCP port 1000. */
  uip_listen(HTONS(1000));
}
/*---------------------------------------------------------------------------*/
/*
* In socketapp.h we have defined the UIP_APPCALL macro to
* socket_app_appcall so that this function is uIP's application
* function. This function is called whenever an uIP event occurs
* (e.g. when a new connection is established, new data arrives, sent
* data is acknowledged, data needs to be retransmitted, etc.).
*/
void socket_app_appcall(void)
{
  /*
   * The uip_conn structure has a field called "appstate" that holds
   * the application state of the connection. We make a pointer to
   * this to access it easier.
   */
  struct socket_app_state *s = &(uip_conn->appstate);

  /*
   * If a new connection was just established, we should initialize
   * the protosocket in our applications' state structure.
   */
  if(uip_connected()) {
    PSOCK_INIT(&s->p, s->inputbuffer, sizeof(s->inputbuffer));
  }

  /*
   * Finally, we run the protosocket function that actually handles
   * the communication. We pass it a pointer to the application state
   * of the current connection.
   */
  handle_connection(s);
}
/*---------------------------------------------------------------------------*/
/*
* This is the protosocket function that handles the communication. A
* protosocket function must always return an int, but must never
* explicitly return - all return statements are hidden in the PSOCK
* macros.
*/
extern char buffer[20];
static int handle_connection(struct socket_app_state *s)
{
  PSOCK_BEGIN(&s->p);

  PSOCK_READTO(&s->p, '\n');
  memcpy(buffer,s->inputbuffer,PSOCK_DATALEN(&s->p));
  memset(s->inputbuffer, 0x00, sizeof(s->inputbuffer));

  PSOCK_END(&s->p);
}
/*---------------------------------------------------------------------------*/
rj44319
 
Posts: 25
Joined: Sat Feb 06, 2010 9:27 pm

Re: Port Controled WiFi RC Car.

Postby charlieo » Sun Feb 28, 2010 11:38 am

Thanks rj44319!

Would you mind posting the PC-side code you are using?

Thanks again,
Charlie
charlieo
 
Posts: 15
Joined: Sun Jan 17, 2010 3:28 pm

Re: Port Controled WiFi RC Car.

Postby rj44319 » Sun Feb 28, 2010 9:22 pm

Ok, I will send the file. It will be compiled to run as a exe. Now, I will be up loading a bigger and better peace of code for a NASA compilation that I am going to. Once it is done (Monday of this week hopefully), I will share it to the community. PC program and all. BTW- I set this socket client to send "text" to the Arduino. So, You have to send "AKK" to it. (with out the quotes) The new version will have buttons (on the PC) to control it.
Attachments
SocketsClient.zip
(8.82 KiB) Downloaded 26 times
rj44319
 
Posts: 25
Joined: Sat Feb 06, 2010 9:27 pm

Re: Port Controled WiFi RC Car.

Postby rj44319 » Mon Mar 01, 2010 10:31 pm

Sorry for all who wanted to see the new code. I can not relise it till after competition at NASA. Soon after, I will.
rj44319
 
Posts: 25
Joined: Sat Feb 06, 2010 9:27 pm

Re: Port Controled WiFi RC Car.

Postby selectiveapathy » Wed Jun 09, 2010 5:39 am

Hello
I am quite interested in the Socket Client and its code.
Is it possible to post this if you are not busy.
selectiveapathy
 
Posts: 8
Joined: Tue Jun 08, 2010 8:12 am

Re: Port Controled WiFi RC Car.

Postby rj44319 » Tue Jun 15, 2010 1:44 pm

look here for NASA project:
look at last post:
viewtopic.php?f=15&t=226&p=2526#p2526
rj44319
 
Posts: 25
Joined: Sat Feb 06, 2010 9:27 pm

Re: Port Controled WiFi RC Car.

Postby clarkden » Tue Oct 26, 2010 5:20 pm

rj44319 wrote:Sorry I have it!
Code: Select all
/*
* Socket App to control Radio Control Car
*
* Loosly based off the simple socket application example using the WiShield 1.0
*/

#include <WiShield.h>
#include <Servo.h>

#define WIRELESS_MODE_INFRA   1
#define WIRELESS_MODE_ADHOC   2

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,2};   // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1};   // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0};   // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"dd-wrt"};      // max 32 bytes

unsigned char security_type = 0;   // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"12345678"};   // max 64 characters

// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = {   0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,   // Key 0
                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   0x00,   // Key 1
                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   0x00,   // Key 2
                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   0x00   // Key 3
                        };

// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

unsigned char ssid_len;
unsigned char security_passphrase_len;
//---------------------------------------------------------------------------
char buffer[20];

int pinArray[2] = {3, 5}; // digital pins for the servos
int minPulse = 500;             // minimum servo position
int maxPulse = 2500;            // maximum servo position
int refreshTime =  20;          // time (ms) between pulses (50Hz)

int i;              // iterator
int servoPin;       // control pin for current servo
int pulseWidth;     // servo pulse width
int servoPosition;  // commanded servo position, 0-180 degrees
int pulseRange;     // maxPulse - minPulse
int centerServo;    // servo starting point
long lastPulse = 0; // recorded time (ms) of the last pulse
int servo;          // which servo to pulse
int servo1[2];      // servo #1 array{pin, pulsewidth}
int servo2[2];      // servo #2 array{pin, pulsewidth}
int pin;            // digital pin for pulse() function
int puls;           // pulsewidth for pulse() function
int posout;
int pos[2];

void setup()
{
  WiFi.init();
  // loop through all 4 servo pins
  // and set them as OUTPUT
  for (i=0;i<2;i++) {
    pinMode(pinArray[i], OUTPUT);
  } 
  // servo starting point (center)
  pulseRange  = maxPulse - minPulse;
  centerServo = maxPulse - ((pulseRange)/2);
  pulseWidth  = centerServo;
  // map pins to servos
  servo1[0] = pinArray[0];  // servo #1 is pin 3
  servo2[0] = pinArray[1];  // servo #2 is pin 4
  // center all servos
  servo1[1] = pulseWidth;
  servo2[1] = pulseWidth;
}

void loop()
{
  WiFi.run(); 
 
  if(buffer[0] == 'A'){
    //Get position from string
    pos[0] = servopos(buffer[1]);
    pos[1] = servopos(buffer[2]);
    //Calculate Pulse Position
    servo1[1] = calpulse(pos[0]);
    servo2[1] = calpulse(pos[1]);
       
  memset(buffer, 0x00, sizeof(buffer));
  }
   
   //pulse each servo
  if (millis() - lastPulse >= refreshTime) {
    pulse(servo1[0], servo1[1]);
    pulse(servo2[0], servo2[1]);
   // save the time of the last pulse
    lastPulse = millis();
  }
}

void pulse(int pin, int puls) {
    digitalWrite(pin, HIGH); // start the pulse
    delayMicroseconds(puls); // pulse width
    digitalWrite(pin, LOW);  // stop the pulse
}

int calpulse(int srvpos) {
  if (srvpos == 255) { servo = 255; }
  // compute pulseWidth from servoPosition
  pulseWidth = minPulse + (srvpos * (pulseRange/180));
  // stop servo pulse at min and max
  if (pulseWidth > maxPulse) { pulseWidth = maxPulse; }
  if (pulseWidth < minPulse) { pulseWidth = minPulse; }
  return pulseWidth;
}

int servopos(int srvpos){
  if(srvpos == 'A'){posout = 0;}
  else if(srvpos == 'B'){posout = 0;}
  else if(srvpos == 'C'){posout = 10;}
  else if(srvpos == 'D'){posout = 20;}
  else if(srvpos == 'E'){posout = 30;}
  else if(srvpos == 'F'){posout = 40;}
  else if(srvpos == 'G'){posout = 50;}
  else if(srvpos == 'H'){posout = 60;}
  else if(srvpos == 'I'){posout = 70;}
  else if(srvpos == 'J'){posout = 80;}
  else if(srvpos == 'K'){posout = 90;}
  else if(srvpos == 'L'){posout = 100;}
  else if(srvpos == 'M'){posout = 110;}
  else if(srvpos == 'N'){posout = 120;}
  else if(srvpos == 'O'){posout = 130;}
  else if(srvpos == 'P'){posout = 140;}
  else if(srvpos == 'Q'){posout = 150;}
  else if(srvpos == 'R'){posout = 160;}
  else if(srvpos == 'S'){posout = 170;}
  else if(srvpos == 'T'){posout = 180;}
  else if(srvpos == 'U'){posout = 180;}
  return posout;
}


then here is the socket app. Here is were the modification is.

Code: Select all

/******************************************************************************

  Filename:      socketapp.c
  Description:   Simple socket programming example for the WiShield 1.0

******************************************************************************

  TCP/IP stack and driver for the WiShield 1.0 wireless devices

  Copyright(c) 2009 Async Labs Inc. All rights reserved.

  This program is free software; you can redistribute it and/or modify it
  under the terms of version 2 of the GNU General Public License as
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc., 59
  Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  Contact Information:
  <[email protected]>

   Author               Date        Comment
  ---------------------------------------------------------------
   AsyncLabs         06/06/2009   Initial version

*****************************************************************************/

/*
* This is a short example of how to write uIP applications using
* protosockets.
*/

/*
* We define the application state (struct socket_app_state) in the
* socketapp.h file, so we need to include it here. We also include
* uip.h (since this cannot be included in socketapp.h) and
* <string.h>, since we use the memcpy() function in the code.
*/
#include "socketapp.h"
#include "uip.h"
#include <string.h>

/*
* Declaration of the protosocket function that handles the connection
* (defined at the end of the code).
*/
static int handle_connection(struct socket_app_state *s);
/*---------------------------------------------------------------------------*/
/*
* The initialization function. We must explicitly call this function
* from the system initialization code, some time after uip_init() is
* called.
*/
void socket_app_init(void)
{
  /* We start to listen for connections on TCP port 1000. */
  uip_listen(HTONS(1000));
}
/*---------------------------------------------------------------------------*/
/*
* In socketapp.h we have defined the UIP_APPCALL macro to
* socket_app_appcall so that this function is uIP's application
* function. This function is called whenever an uIP event occurs
* (e.g. when a new connection is established, new data arrives, sent
* data is acknowledged, data needs to be retransmitted, etc.).
*/
void socket_app_appcall(void)
{
  /*
   * The uip_conn structure has a field called "appstate" that holds
   * the application state of the connection. We make a pointer to
   * this to access it easier.
   */
  struct socket_app_state *s = &(uip_conn->appstate);

  /*
   * If a new connection was just established, we should initialize
   * the protosocket in our applications' state structure.
   */
  if(uip_connected()) {
    PSOCK_INIT(&s->p, s->inputbuffer, sizeof(s->inputbuffer));
  }

  /*
   * Finally, we run the protosocket function that actually handles
   * the communication. We pass it a pointer to the application state
   * of the current connection.
   */
  handle_connection(s);
}
/*---------------------------------------------------------------------------*/
/*
* This is the protosocket function that handles the communication. A
* protosocket function must always return an int, but must never
* explicitly return - all return statements are hidden in the PSOCK
* macros.
*/
extern char buffer[20];
static int handle_connection(struct socket_app_state *s)
{
  PSOCK_BEGIN(&s->p);

  PSOCK_READTO(&s->p, '\n');
  memcpy(buffer,s->inputbuffer,PSOCK_DATALEN(&s->p));
  memset(s->inputbuffer, 0x00, sizeof(s->inputbuffer));

  PSOCK_END(&s->p);
}
/*---------------------------------------------------------------------------*/



hi rj
i am using your code but it was not working properly
can you tell me what was the problem in running the program.

Clarkden
clarkden
 
Posts: 1
Joined: Tue Oct 26, 2010 5:10 pm

Previous

Return to Sketches and Applications

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron